一个简单的python日志小模块

 

 

基本代码如下

import logging
import logging.handlers
import datetime

class CreteLog(object):
    def __init__(self):
        self.logger = logging.getLogger('w3logger')
        self.logger.setLevel(logging.INFO)
    
    def statup(self,path,stream=True):
        write_handler = logging.handlers.TimedRotatingFileHandler(path, when='midnight', interval=1, backupCount=7, atTime=datetime.time(0, 0, 0, 0))
        write_handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(module)s[:%(lineno)d] - %(message)s"))
        
        if stream:
            stream_handler = logging.StreamHandler()
            stream_handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(module)s[:%(lineno)d] - %(message)s"))
            self.logger.addHandler(stream_handler)
        self.logger.addHandler(write_handler)
        
        return self.logger

if __name__ == '__main__':
    createlog = CreteLog()
    logger = createlog.statup("account2sep.log",stream=False)
    logger.debug('debug message')
    logger.info('info message')
    logger.warning('warning message')
    logger.error('error message')

 

posted @ 2021-12-28 22:30  Linuxbugs  阅读(59)  评论(0编辑  收藏  举报