python log 設定
阿新 • • 發佈:2019-08-03
# -*- 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',
sh.setFormatter(formatter)
fh.setFormatter(formatter)
logger.addHandler(sh)
logger.addHandler