日志–format参数中可能用到的格式化信息
import logging
from logging.handlers import TimedRotatingFileHandler
def config_log(file_path, is_debug=False, when="D"):
"""
日志方法
:param file_path: 日志路径及文件
:param is_debug: 级别
:param when: 按照规则切分
:return:
"""
if is_debug:
fmt = '[%(asctime)s] - %(threadName)s - %(filename)s - %(lineno)d - %(message)s'
else:
fmt = '[%(asctime)s] - %(threadName)s - %(filename)s - %(message)s'
log = logging.getLogger('')
fileTimeHandler = TimedRotatingFileHandler(file_path, when, 1, 3)
fileTimeHandler.suffix = "%Y%m%d"
fileTimeHandler.setFormatter(logging.Formatter(fmt))
logging.basicConfig(level=logging.DEBUG if is_debug else logging.INFO, format=fmt)
log.addHandler(fileTimeHandler)
if __name__ == '__main__':
config_log('./my_log.log')
logging.info('response code: {}'.format(200))
import logging
from logging.handlers import TimedRotatingFileHandler
logger = logging.getLogger('logg')
formatter = logging.Formatter('%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
time_rotating_file_handler = TimedRotatingFileHandler(filename='uni_log.log', when='D')
time_rotating_file_handler.setLevel(logging.DEBUG)
time_rotating_file_handler.setFormatter(formatter)
logger.addHandler(time_rotating_file_handler)