美文网首页
自定义python日志log类

自定义python日志log类

作者: Challis | 来源:发表于2019-10-21 15:48 被阅读0次
    import logging
    import logging.handlers
    import os
    
    pwd = os.getcwd()
    
    class LogMgr:
        def __init__(self, logname):
            self.LOG = logging.getLogger('log')
            # loghdlr1 = logging.handlers.RotatingFileHandler(logpath, "a", encoding='utf-8')
            log_dir = os.path.join(pwd, "static", "log", logname)
            if not os.path.exists(log_dir):
                os.makedirs(log_dir)
            logpath = os.path.join(log_dir, logname)
            loghdlr1 = logging.handlers.TimedRotatingFileHandler(filename=logpath, when="D", interval=1, encoding="utf-8")
            loghdlr1.suffix = "%Y-%m-%d.log"
            fmt = logging.Formatter("%(asctime)s %(levelname)s %(message)s", "%H:%M:%S")
            loghdlr1.setFormatter(fmt)
    
            sh = logging.StreamHandler()
            sh.setFormatter(fmt)
    
            self.LOG.addHandler(loghdlr1)
            self.LOG.addHandler(sh)
            self.LOG.setLevel(logging.INFO)
    
        def error(self, msg):
            if self.LOG is not None:
                self.LOG.error(msg)
    
        def info(self, msg):
            if self.LOG is not None:
                self.LOG.info(msg)
    
        def debug(self, msg):
            if self.LOG is not None:
                self.LOG.debug(msg)
    

    相关文章

      网友评论

          本文标题:自定义python日志log类

          本文链接:https://www.haomeiwen.com/subject/pcbtvctx.html