python---RotatingFileHandlerl 日志

import logging
import os.path
from logging.handlers import RotatingFileHandler


#写入日志的模式
mode = 'a'
#文件最大字节数,int,目前配置为200Mb
maxBytes = 20971200
#备份数
backCount = 0
encoding = 'utf-8'

class Logger(object):
    def __init__(self, logger):
        """
            指定保存日志的文件路径,日志级别,以及调用文件
            将日志存入到指定的文件中
        """

        # 创建一个logger
        self.logger = logging.getLogger(logger)
        self.logger.setLevel(logging.DEBUG)

        log_name = 'name' + '.log'
        # vals = ParseNrTest().getSectionVals('LOG')
        fh = RotatingFileHandler(filename=log_name, mode=mode, maxBytes=int(maxBytes), backupCount=int(backCount),
                                 encoding=encoding)
        fh.setLevel(logging.INFO)

        # 创建一个handler,用于控制台输出
        sh = logging.StreamHandler()
        sh.setLevel(logging.INFO)

        # 定义handler的输出格式
        formatter = logging.Formatter('[%(asctime)s] %(levelname)s: %(name)s - %(funcName)s [line:%(lineno)d]-> %(message)s',
                                      '%Y-%m-%d %H:%M:%S')
        fh.setFormatter(formatter)
        sh.setFormatter(formatter)

        # 给logger添加handler
        self.logger.addHandler(fh)
        self.logger.addHandler(sh)

    @staticmethod
    def get_module(file):
        dirname = os.path.dirname(os.path.abspath(file))
        return dirname[dirname.find('src') + 4:] + '/'

    def getlog(self):
        return self.logger


if __name__ == '__main__':
    logger = Logger(Logger.get_module(__file__)).getlog()
    logger.error('日志测试!!')
    # def __init__(self):
    #     self.logger = Logger(Logger.get_module(__file__)).getlog()
    logger.debug('\031This is debug message')
    logger.info('This is info message')
    logger.warning('This is warning message')
    print("\033[1;36;m", "*** 蓝字")
    print("\033[0m", "*** 默认")
    print("\033[1;31;m", "*** 红字!")

 

你可能感兴趣的:(django框架2.0,python,log日志)