赞
踩
#以时间命名输出日志
def init_logging(): t = str(time.strftime("%Y%m%d_%H%M", time.localtime())) filename = 'log_%s.txt' % t logging.basicConfig(level=logging.DEBUG, filename=filename, filemode='w', format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
#实现日志按大小分割,
def init_log(): handler = logging.handlers.RotatingFileHandler('log/test1.log', maxBytes=1 * 1024 * 1024, backupCount=3) # 按1M大小分割,共保留10个文件 formatter = logging.Formatter('%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s') handler.setFormatter(formatter) my_logger = logging.getLogger('') my_logger.setLevel(logging.DEBUG) my_logger.addHandler(handler)
两种初始化的方法打印时候都是logging.info()
init_log() #先初始化 logging.error("456") logging.info("你好")
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。