python logging使用模板

文章目录

        • 1.代码
        • 运行

1.代码

‘# ––coding:utf-8–
import os,sys
import logging
from logging.handlers import TimedRotatingFileHandler
script_path = os.path.abspath(os.path.dirname(sys.argv[0]))
if not os.path.isdir(os.path.join(script_path, ‘log’)):
os.system(‘mkdir -p %s’ % os.path.join(script_path, ‘log’))
logger = logging.getLogger(‘logging_test’)
logger.setLevel(logging.DEBUG)
fh = TimedRotatingFileHandler(os.path.join(script_path, ‘log’) + ‘/logging_test.log’, “D”, 1, 10)
fh.suffix = “%Y%m%d.log”
ch = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter(’%(asctime)s %(filename)s %(funcName)s [line:%(lineno)d] %(levelname)s %(message)s’)
fh.setFormatter(formatter)
ch.setFormatter(formatter)
logger.addHandler(fh)
logger.addHandler(ch)
logger.error(“测试error”)
logger.info(“测试info”)

运行

在这里插入图片描述
python logging使用模板_第1张图片

你可能感兴趣的:(python)