一个简单的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')
转载请标明出处:http://www.cnblogs.com/duzhaoqi/