发布网友 发布时间:2022-03-23 22:47
共1个回答
热心网友 时间:2022-03-24 00:16
loggin模块需要进行很多封装才好用,你这种情况应该是初始化有问题,给你贴一段代码你自己照抄下来用用试试。# -*- coding:UTF8 -*-#import osimport loggingclass Logger(object): ''' @summary:日志处理对象,对logging的封装 ''' def __init__(self,name = 'Logger'): self.logger = logging.getLogger( name ) self.init_logger() def init_logger(self): self.logger.setLevel(logging.DEBUG) # 屏幕输出日志 stream = logging.StreamHandler() stream.setLevel( logging.INFO ) # 日志样式 fm_stream = logging.Formatter("[\033[1;%(colorcode)sm%(levelname)s\033[0m %(asctime)s %(myfn)s:%(mylno)d:%(myfunc)s%(mymole)s] %(message)s", "%m-%d %H:%M:%S") stream.setFormatter( fm_stream ) self.logger.addHandler( stream ) def update_kwargs(self, kwargs, colorcode): try: fn, lno, func = self.logger.findCaller() fn = os.path.basename(fn) except Exception as ddd: fn, lno, func = "(unknown file)", 0, "(unknown function)" if not "extra" in kwargs: kwargs["extra"] = {} kwargs["extra"]["myfn"] = fn kwargs["extra"]["mylno"] = lno kwargs["extra"]["myfunc"] = func kwargs["extra"]["colorcode"] = colorcode kwargs["extra"]["mymole"] = "" def debug(self, msg, *args, **kwargs): self.update_kwargs(kwargs, "0") # 原色 self.logger.debug(msg, *args, **kwargs) def info(self, msg, *args, **kwargs): self.update_kwargs(kwargs, "32") # 绿色 self.logger.info(msg, *args, **kwargs) def warning(self, msg, *args, **kwargs): self.update_kwargs(kwargs, "33") # * self.logger.warning(msg, *args, **kwargs) def error(self, msg, *args, **kwargs): self.update_kwargs(kwargs, "31") # 红色 self.logger.error(msg, *args, **kwargs) def critical(self, msg, *args, **kwargs): self.update_kwargs(kwargs, "31") # 红色 self.logger.critical(msg, *args, **kwargs)使用方法:from logger import LoggerLogger().info('xxxxx')Logger().warning('xxxxx')Logger().error('xxxxx')