import logging
import os
from Common.handle_path import logs_dir
from Common.handle_config import conf
class MyLogger(logging.Logger):
def __init__(self, file=None):
super().__init__(conf.get("log", "name"), conf.get("log", "level"))
fmt = '%(asctime)s %(name)s %(levelname)s %(filename)s-%(lineno)d line:%(message)s'
formatter = logging.Formatter(fmt)
# 控制台输出
sh = logging.StreamHandler()
sh.setFormatter(formatter)
self.addHandler(sh)
if file:
fh = logging.FileHandler(file, encoding="utf-8")
fh.setFormatter(formatter)
self.addHandler(fh)
# 是否需要将日志写入文件,进行判断,file=None则不写入文件
if conf.getboolean('log', 'file_ok'):
file_name = os.path.join(logs_dir, conf.get('log', 'file_name'))
else:
file_name = None
logger = MyLogger(file_name)
配置文件模块:https://www.cnblogs.com/qiu1219/p/16731336.html
标签:封装,file,Mylogger,log,conf,import,logging,日志,name
From: https://www.cnblogs.com/qiu1219/p/16731322.html