加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
log.py 1.76 KB
一键复制 编辑 原始数据 按行查看 历史
执明神君 提交于 2022-01-28 21:11 . Initial commit
# -*- coding: utf-8 -*-
"""全局日志
Note: 仅在入口文件中导入一次
"""
import os
import datetime
import logging
import sys
def get_submod_log(submod_name):
return logging.getLogger('Main' + '.' + submod_name)
class LoggerStream(object):
"""假 stream,将 stdout/stderr 流重定向到日志,避免win上无头模式运行时报错。
ref: https://docs.python.org/3/library/sys.html#sys.__stdout__
"""
def __init__(self, name, level, fileno):
"""
:param logger: 日志实例
:param level: 日志等级
"""
self.logger = get_submod_log(name)
self.level = level
self.fileno = fileno
def fileno(self):
return self.fileno
def write(self, lines):
for line in lines.splitlines():
self.logger.log(self.level, line)
def flush(self):
for handler in self.logger.handlers:
handler.flush()
def init_log(application_path):
log_path = os.path.join(application_path, r'logs/log-%s.txt' % (datetime.datetime.today().strftime('%Y-%m-%d')))
if sys.stderr:
get_submod_log('log').addHandler(logging.StreamHandler())
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] %(message)s",
handlers=[
logging.FileHandler(log_path,'w','utf-8'),
logging.StreamHandler()
]
)
else:
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] %(message)s",
filename=log_path,
filemode='a'
)
sys.stdout = LoggerStream('STDOUT', logging.INFO, 1)
sys.stderr = LoggerStream('STDERR', logging.ERROR, 2)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化