python logging
`# -- coding:utf-8 --
功能:日志模块封装
import time
import logging
from logging import handlers
from pathlib import Path
class GetLogger(object):
""" 日志封装类 """
@classmethod
def get_logger(cls):
# logger = logging.getLogger(__name__) # 不会打印 HTTP General 信息
log = logging.getLogger()
level_relations = {
'NOTSET': logging.NOTSET,
'DEBUG': logging.DEBUG,
'INFO': logging.INFO,
'WARNING': logging.WARNING,
'ERROR': logging.ERROR,
'CRITICAL': logging.CRITICAL
} # 日志级别关系映射
# 创建日志存放的目录
project_path = Path.cwd() # 获取项目根目录
logs_dir = Path(project_path / "logs")
if Path.exists(logs_dir) and Path.is_dir(logs_dir):
pass
else:
Path.mkdir(logs_dir)
# 日志文件以日期命名
log_file_name = '%s.log' % time.strftime("%Y-%m-%d", time.localtime())
log_file_path = Path(logs_dir / log_file_name)
rotating_file_handler = handlers.TimedRotatingFileHandler(filename=log_file_path,
when='D', # 按天分隔,一天一个文件
interval=30,
encoding='utf-8')
# 日志输出格式
# fmt = "%(asctime)s %(levelname)s %(pathname)s %(lineno)d %(message)s"
# fmt = "%(name)s %(asctime)s %(created)f %(relativeCreated)d %(msecs)d %(levelname)s %(levelno)s %(pathname)s %(filename)s %(module)s %(funcName)s %(lineno)d %(process)d %(thread)d %(threadName)s %(message)s"
fmt = "%(asctime)s %(created)f %(relativeCreated)d %(msecs)d %(levelname)s %(levelno)s %(pathname)s %(filename)s %(module)s %(funcName)s %(lineno)d %(process)d %(thread)d %(threadName)s %(message)s"
formatter = logging.Formatter(fmt)
rotating_file_handler.setFormatter(formatter)
# 加上判断,避免重复打印日志
if not log.handlers:
# 控制台输出
console = logging.StreamHandler()
console.setLevel(level_relations["NOTSET"])
console.setFormatter(formatter)
# 写入日志文件
log.addHandler(rotating_file_handler)
log.addHandler(console)
log.setLevel(level_relations['DEBUG'])
return log
if name == 'main':
logger = GetLogger().get_logger()
logger.debug('调试')
logger.info('信息')
logger.warning('警告')
logger.error('报错')
logger.critical('严重')
`