标签:dir ini img self ack and python evel image
python的日志模块使用logging,如果想要输出符合自己的预期,需要重新定义,废话不多说,直接贴代码。
# encoding: utf -8 from logging.handlers import TimedRotatingFileHandler import logging import os import datetime #调用方法log.debug(‘logname.log‘,‘输出内容‘) path=os.path.abspath(‘‘) class log(): def __init__(self): if os.path.isdir(‘%s\\log‘%path): #创建log文件夹 pass else: os.mkdir(‘%s\\log‘%path) def debug(self,logname,kwarg): logFilePath = ‘%s\\log\\%s‘%(path,logname) logger = logging.getLogger("yourName") logger.setLevel(logging.DEBUG) handler = TimedRotatingFileHandler(logFilePath, when="midnight", interval=1, backupCount=20) formatter = logging.Formatter(‘%(asctime)s %(levelname)s %(message)s‘) handler.setFormatter(formatter) logger.addHandler(handler) logger.debug(‘%s‘%(kwarg)) logger.removeHandler(handler) def error(self,logname,kwarg): logFilePath = ‘%s\\log\\%s‘%(path,logname) logger = logging.getLogger("yourName") logger.setLevel(logging.ERROR) handler = TimedRotatingFileHandler(logFilePath, when="midnight", interval=1, backupCount=20) formatter = logging.Formatter(‘%(asctime)s %(levelname)s %(message)s‘) handler.setFormatter(formatter) logger.addHandler(handler) logger.error(‘%s‘%(kwarg)) logger.removeHandler(handler)
handler = TimedRotatingFileHandler(logFilePath, when="midnight", interval=1, backupCount=20) 定义写入日志的策略:
1.每天一个日志文件。
2.超过20个,删除最早生成的日志。
formatter = logging.Formatter(‘%(asctime)s %(levelname)s %(message)s‘) 定义日志输出的格式
输出日志:
log().debug(‘test.log‘,‘Hello World...‘)
标签:dir ini img self ack and python evel image
原文地址:https://www.cnblogs.com/shenh/p/9708732.html