2020-03-29 01:22:46 +08:00
|
|
|
import datetime
|
2020-07-05 00:54:21 +08:00
|
|
|
import logging
|
2020-04-08 00:50:39 +08:00
|
|
|
import os
|
|
|
|
import sys
|
2020-04-16 14:05:37 +08:00
|
|
|
from collections import deque
|
2020-03-29 01:22:46 +08:00
|
|
|
|
2021-04-05 21:12:28 +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
|
2020-08-06 23:49:45 +08:00
|
|
|
logging.raiseExceptions = True # Set True if wanna see encode errors on console
|
2020-06-03 16:30:14 +08:00
|
|
|
|
2020-07-05 00:54:21 +08:00
|
|
|
# Logger init
|
2020-08-06 23:49:45 +08:00
|
|
|
logger_debug = False
|
2021-04-05 21:12:28 +08:00
|
|
|
logger = logging.getLogger('alas')
|
2020-08-06 23:49:45 +08:00
|
|
|
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')
|
|
|
|
|
2020-07-05 00:54:21 +08:00
|
|
|
# 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__), '../'))
|
2020-07-05 00:54:21 +08:00
|
|
|
|
|
|
|
# Add file logger
|
|
|
|
pyw_name = os.path.splitext(os.path.basename(sys.argv[0]))[0]
|
2021-10-16 12:59:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2021-10-16 12:59:29 +08:00
|
|
|
logger.set_file_logger = set_file_logger
|
|
|
|
logger.log_file: str
|
2020-03-29 01:22:46 +08:00
|
|
|
|
2021-10-16 12:59:29 +08:00
|
|
|
logger.set_file_logger()
|
2020-03-29 01:22:46 +08:00
|
|
|
logger.hr('Start', level=0)
|