?
?
1. loguru安装
pip install logurug
?官方文档:Table of contents — loguru documentation
?github:https://github.com/Delgan/loguru
2.?loguru支持的输出颜色
3.?loguru时间格式化输出
?
?
4. 使用loguru
from loguru import logger
# 终端日志输出格式
stdout_fmt = '[<cyan>{time:YYYY-MM-DD HH:mm:ss}</cyan>] ' \
'[<level>{level: <5}</level>] ' \
'<blue>{module}</blue>:<cyan>{line}</cyan> - ' \
'<level>{message}</level>'
# 日志文件记录格式
logfile_fmt = '<light-green>{time:YYYY-MM-DD HH:mm:ss,SSS}</light-green> ' \
'[<level>{level: <5}</level>] ' \
'<cyan>{process.name}({process.id})</cyan>:' \
'<cyan>{thread.name: <18}({thread.id: <5})</cyan> | ' \
'<blue>{module}</blue>.<blue>{function}</blue>:' \
'<blue>{line}</blue> - <level>{message}</level>'
logger.remove() #重置自带的输出格式
#自定义级别和输出样式
logger.level(name='TRACE', color='<cyan><bold>', icon='??')
logger.level(name='DEBUG', color='<blue><bold>', icon='🐞 ')
logger.level(name='INFOR', no=20, color='<green><bold>', icon='??')
logger.level(name='CONFIRM', no=20, color='<green><bold>', icon='??')
logger.level(name='QUITE', no=25, color='<green><bold>', icon='🤫 ')
logger.level(name='ALERT', no=30, color='<yellow><bold>', icon='??')
logger.level(name='ERROR', color='<red><bold>', icon='??')
logger.level(name='MISINFORM', no=50, color='<yellow><bold>', icon='??')
logger.level(name='NONES', no=50, color='<yellow><bold>', icon='??')
logger.level(name='FAILS', no=50, color='<yellow><bold>', icon='??')
# confirm CONFIRM
# 命令终端日志级别默认为INFOR
logger.add(sys.stderr, level='INFOR', format=stdout_fmt, enqueue=True)
# 日志文件默认为级别为DEBUG
logger.add(log_path, level='DEBUG', format=logfile_fmt, enqueue=True, encoding='utf-8')
#测试函数
if __name__ == "__main__":
# logger.log('CONFIRM', 'You must provide either target or targets parameter')
id=1
keyword=1
status_data=1
logger.log('CONFIRM',f'{id}{keyword}{status_data}')
参考开源代码:OneForAll?
|