import logging
from logging import Logger
class MyLogger(Logger):
def __int__(self,name,level=logging.INFO,file = None):
#1、设置日志收集级别
super.__init__(name,level)
#2、将日志输出到文件和控制台
fmt_str = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s'
#实例化日志格式类
formatter = logging.Formatter(fmt_str)
# 实例化渠道(Handle)
# 控制台渠道(StreamHandle)
handle1 = logging.StreamHandler()
handle1.setFormatter(formatter)
#渠道与日志收集器绑定
self.addHandler(handle1)
# 文件渠道(FileHandle)
if file:
handle2 = logging.FileHandler(file,encoding='utf-8')
handle2.setFormatter(formatter)
self.addHandler(handle2)
logger = MyLogger("adc",file = "aa.log")