python log 设置
# -*- coding: utf-8 -*-
import logging
from logging.handlers import TimedRotatingFileHandler # 按时间处理的log处理类
logger = logging.getLogger("xy")
sh = logging.StreamHandler()
sh.setLevel(logging.DEBUG) # sh 用于终端输出 debug等级
# 一天一个log文件, 午夜更换, 最多保存60天
fh = TimedRotatingFileHandler("/var/log/parse_eml/parser.log", "MIDNIGHT", 1, 60, encoding='utf-8')
#fh = TimedRotatingFileHandler("/var/log/parse_eml/parser.log", "S", 2, 3, encoding='utf-8')
fh.setLevel(logging.WARNING) # fh 用于日志文件记录
formatter = logging.Formatter('%(name)s %(asctime)s %(pathname)s '
'%(lineno)d %(process)d %(thread)d %(threadName)s %(levelno)s\n%(message)s',
'%Y-%m-%d %H:%M') # 日志详细输出内容格式: log名称, 时间, 文件路径, 行号, 进程, 线程, 线程名称, 等级, 信息
sh.setFormatter(formatter)
fh.setFormatter(formatter)
logger.addHandler(sh)
logger.addHandler(fh)