AzurLaneAutoScript/module/logger.py

87 lines
2.4 KiB
Python
Raw Normal View History

2020-03-29 01:22:46 +08:00
import datetime
import logging
import os
import sys
from collections import deque
2020-03-29 01:22:46 +08:00
def empty_function(*args, **kwargs):
pass
# cnocr will set root logger in cnocr.utils
# Delete logging.basicConfig to avoid logging the same message twice.
logging.basicConfig = empty_function
logging.raiseExceptions = True # Set True if wanna see encode errors on console
# Logger init
logger_debug = False
logger = logging.getLogger('alas')
logger.setLevel(logging.DEBUG if logger_debug else logging.INFO)
2020-03-29 01:22:46 +08:00
formatter = logging.Formatter(fmt='%(asctime)s.%(msecs)03d | %(levelname)s | %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
# Add console logger
2021-09-08 12:50:10 +08:00
console = logging.StreamHandler(stream=sys.stdout)
2020-03-29 01:22:46 +08:00
console.setFormatter(formatter)
2021-09-08 12:50:10 +08:00
console.flush = sys.stdout.flush
2020-03-29 01:22:46 +08:00
logger.addHandler(console)
2020-12-16 18:04:01 +08:00
# Ensure running in Alas root folder
os.chdir(os.path.join(os.path.dirname(__file__), '../'))
# Add file logger
pyw_name = os.path.splitext(os.path.basename(sys.argv[0]))[0]
def set_file_logger(name=pyw_name):
if '_' in name:
name = name.split('_', 1)[0]
log_file = f'./log/{datetime.date.today()}_{name}.txt'
try:
file = logging.FileHandler(log_file, encoding='utf-8')
except FileNotFoundError:
os.mkdir('./log')
file = logging.FileHandler(log_file, encoding='utf-8')
file.setFormatter(formatter)
logger.handlers = [h for h in logger.handlers if not isinstance(h, logging.FileHandler)]
logger.addHandler(file)
logger.log_file = log_file
2020-03-29 01:22:46 +08:00
def hr(title, level=3):
title = str(title).upper()
if level == 1:
logger.info('=' * 20 + ' ' + title + ' ' + '=' * 20)
if level == 2:
logger.info('-' * 20 + ' ' + title + ' ' + '-' * 20)
if level == 3:
logger.info('<' * 3 + ' ' + title + ' ' + '>' * 3)
if level == 0:
middle = '|' + ' ' * 20 + title + ' ' * 20 + '|'
border = '+' + '-' * (len(middle) - 2) + '+'
logger.info(border)
logger.info(middle)
logger.info(border)
def attr(name, text):
logger.info('[%s] %s' % (str(name), str(text)))
2020-07-07 19:54:30 +08:00
def attr_align(name, text, front='', align=22):
2020-07-06 00:46:36 +08:00
name = str(name).rjust(align)
2020-07-07 19:54:30 +08:00
if front:
name = front + name[len(front):]
2020-07-06 00:46:36 +08:00
logger.info('%s: %s' % (name, str(text)))
2020-03-29 01:22:46 +08:00
logger.hr = hr
logger.attr = attr
2020-07-06 00:46:36 +08:00
logger.attr_align = attr_align
logger.set_file_logger = set_file_logger
logger.log_file: str
2020-03-29 01:22:46 +08:00
logger.set_file_logger()
2020-03-29 01:22:46 +08:00
logger.hr('Start', level=0)