Python logger 全局使用,单例模式

Python logger 全局使用,单例模式,第1张

概述importloggingfromloggingimporthandlersclassPyLogger(object):level_relations={'debug':logging.DEBUG,'info':logging.INFO,'warning':logging.WARNING,'error':logging.ER
import loggingfrom logging import handlers class PyLogger(object):    level_relations = {        'deBUG':logging.DEBUG,        'info':logging.INFO,        'warning':logging.WARNING,        'error':logging.ERROR,        'crit':logging.CRITICAL    }   # Log level relationship mapPing        _instance = None        def __new__(cls, *args, **kwargs):        '''        Parameters        ----------        Comment : Singleton mode        if you want to change or add new logfile, just input new filename                For example:                        example start:                        log = PyLogger('all_test456.log',level='deBUG')            log.logger.deBUG('deBUG')            log.logger.info('info')            log.logger.warning('警告')            log.logger.error('报错')            log.logger.critical('严重')            log.logger.info(log)            PyLogger('error.log', level='error').logger.error('error') # PyLogger object at 0x00000222F177EF48>                        log_new = PyLogger('all_test456789.log',level='deBUG')            log_new.logger.deBUG('deBUG')            log_new.logger.info('info')            log_new.logger.warning('警告')            log_new.logger.error('报错')            log_new.logger.critical('严重')            log_new.logger.info(log_new)            PyLogger('error.log', level='error').logger.error('error') # PyLogger object at 0x00000222F177EF48>                    example end:                cls : TYPE            DESCRIPTION.        *args : TYPE            DESCRIPTION.        **kwargs : TYPE            DESCRIPTION.        Returns        -------        TYPE            DESCRIPTION.        '''        if cls._instance is None:            cls._instance = object.__new__(cls)        return  cls._instance     def __init__(self, filename,level='info',when='D',backCount=3,fmt='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'):        self.logger = logging.getLogger(filename)        format_str = logging.Formatter(fmt) #S et log format        self.logger.setLevel(self.level_relations.get(level)) # Set log level        if not self.logger.handlers:            sh = logging.StreamHandler() # Output to the screen            sh.setFormatter(format_str) # Set the format displayed on the screen                        # Write to file #The processor that automatically generates the file at the specifIEd interval            th = handlers.TimedRotatingfileHandler(filename=filename,when=when,backupCount=backCount,enCoding='utf-8')                        # Instantiate TimedRotatingfileHandler            # interval is the time interval, backupCount is the number of backup files, if it exceeds this number, it will be automatically deleted, when is the time unit of the interval, the units are as follows:            # S seconds            # M points            # H hours,            # D day,            # W Every week (interval==0 means Monday)            # mIDnight Every morning            th.setFormatter(format_str) # Set the format written in the file            self.logger.addHandler(sh) # Add object to logger            self.logger.addHandler(th)            if __name__ == '__main__':    log = PyLogger('all_test456.log',level='deBUG')    log.logger.deBUG('deBUG')    log.logger.info('info')    log.logger.warning('警告')    log.logger.error('报错')    log.logger.critical('严重')    log.logger.info(log)    PyLogger('error.log', level='error').logger.error('error') # PyLogger object at 0x00000222F177EF48>        log_new = PyLogger('all_test456789.log',level='deBUG')    log_new.logger.deBUG('deBUG1')    log_new.logger.info('info1')    log_new.logger.warning('警告1')    log_new.logger.error('报错1')    log_new.logger.critical('严重1')    log_new.logger.info(log_new)    PyLogger('error.log', level='error').logger.error('error') # PyLogger object at 0x00000222F177EF48>
总结

以上是内存溢出为你收集整理的Python logger 全局使用,单例模式全部内容,希望文章能够帮你解决Python logger 全局使用,单例模式所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1188785.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存