自定义python日志log类

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类)