1.引入库
代码如下:
import os
from datetime import datetime
from loguru import logger as loguru_logger
2.主要内容
代码如下:
class Logger():
"""
Record was: {'elapsed': datetime.timedelta(microseconds=15952), 'exception': None, 'extra': {}, 'file': (name='logger.py', path='D:/桌面/try_test/common_log/logger.py'), 'function': '<module>', 'level': (name='INFO', no=20, icon='ℹ️'), 'line': 23, 'message': '13213123', 'module': 'logger', 'name': '__main__', 'process': (id=17872, name='MainProcess'), 'thread': (id=4632, name='MainThread'), 'time': datetime(2021, 6, 8, 15, 16, 46, 107709, tzinfo=datetime.timezone(datetime.timedelta(seconds=28800), '中国标准时间'))}
"""
def __init__(self, logger_super):
# TODO: This should be set to a path in the config instead of the code path.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
current_f = datetime.now().strftime("%Y%m%d")
log_file_path = os.path.join(BASE_DIR, f'log/runtime_{current_f}.log')
loguru_logger.add(log_file_path, rotation="500 MB", encoding='utf-8',
format="{time:YYYY-MM-DD HH:mm:ss}| {level} |{name}:process:{process}-thread:{thread.id}-filepath:{file.path}-filename:{module}-func:{function}-line:{line}-{message}")
self.loguru_logger = loguru_logger
logger = Logger(loguru_logger).loguru_logger
3.使用
代码如下:
from xxx.logger import logger
logger.info('test')
实例效果:
2021-06-08 15:35:50| INFO |-__main__-:process:9840-thread:19752-filepath:D:/xxx/test.py-test-func:func2-212-test
2021-06-08 15:35:50| INFO |-__main__-:process:9840-thread:19752-filepath:D:/xxx/test.py-test-func:func2-212-test