大神同事提供的一个python 日志模块,就是日志文件最大20M,超过时就会覆盖之前的,很不错,代码如下:
import os import logging import logging.handlers
def init_logger(log_file): dir_path = os.path.dirname(log_file) try: if not os.path.exists(dir_path): os.makedirs(dir_path) except Exception as e: pass handler = logging.handlers.RotatingFileHandler(log_file, maxBytes=20 * 1024 * 1024, backupCount=10) fmt = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s' formatter = logging.Formatter(fmt) handler.setFormatter(formatter) logger_instance = logging.getLogger('logs') logger_instance.addHandler(handler) logger_instance.setLevel(logging.DEBUG) return logger_instance
使用如下:
mylog = init_logger('/home/#/result.log')
mylog.info('asfafaf')