python的logging模块简单应用

阅读更多

Python3 标准库自带logging模块,使用的时候直接引用

 

写log

import logging  # 引入logging模块
from logging.handlers import TimedRotatingFileHandler

import os.path
import time
# 第一步,创建一个logger
logger = logging.getLogger()
logger.setLevel(logging.INFO)  # Log等级总开关
# 第二步,创建一个handler,用于写入日志文件
rq = time.strftime('%Y%m%d%H%M', time.localtime(time.time()))
log_path = os.path.dirname(os.getcwd()) + '/log/Logs/'
log_name = log_path + rq + '.log'
logfile = log_name
fh = logging.FileHandler(logfile, mode='w')
fh.setLevel(logging.DEBUG)  # 输出到file的log等级的开关
# 第三步,定义handler的输出格式
formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
#创建TimedRotatingFileHandler对象
log_file_handler = TimedRotatingFileHandler(filename="ds_update", when="D", interval=2, backupCount=2)
fh.setFormatter(formatter)
# 第四步,将logger添加到handler里面
logger.addHandler(fh)
# 日志
# logger.debug('this is a logger debug message')
# logger.info('this is a logger info message')
# logger.warning('this is a logger warning message')
# logger.error('this is a logger error message')
# logger.critical('this is a logger critical message')

# filename:日志文件名的prefix;
#
# when:是一个字符串,用于描述滚动周期的基本单位,字符串的值及意义如下:
# “S”: Seconds
# “M”: Minutes
# “H”: Hours
# “D”: Days
# “W”: Week day (0=Monday)
# “midnight”: Roll over at midnight
#
# interval: 滚动周期,单位有when指定,比如:when=’D’,interval=1,表示每天产生一个日志文件
#
# backupCount: 表示日志文件的保留个数

 

如何引用logger方法?

from app.log.log import logger

data={
    "bdCode":"GSXA0580"
}
# 使用python自带的字符串格式化,不推荐
logger.info("%s data的值为:%s"%("hello world!!",data))
# 使用logger的格式化,推荐
logger.info("%s data的值为:%s","hello world!!",data)
logger.info("data:",data)
# 使用format函数,推荐
logger.info("{}data的值是:{}".format("Hello,world!!!",data))
for i in range(1,20):
    print(i)
    logger.info("i的值为:%s,入参为:%s",i,data)

 

打印日志如下:

2018-06-10 15:52:37,817 - showlog.py[line:11] - INFO: hello world!! data的值为:{'bdCode': 'GSXA0580'}
2018-06-10 15:52:37,817 - showlog.py[line:13] - INFO: hello world!! data的值为:{'bdCode': 'GSXA0580'}
2018-06-10 15:52:37,817 - showlog.py[line:14] - INFO: data:
2018-06-10 15:52:37,818 - showlog.py[line:16] - INFO: Hello,world!!!data的值是:{'bdCode': 'GSXA0580'}
2018-06-10 15:52:37,818 - showlog.py[line:19] - INFO: i的值为:1,入参为:{'bdCode': 'GSXA0580'}
2018-06-10 15:52:37,818 - showlog.py[line:19] - INFO: i的值为:2,入参为:{'bdCode': 'GSXA0580'}
2018-06-10 15:52:37,818 - showlog.py[line:19] - INFO: i的值为:3,入参为:{'bdCode': 'GSXA0580'}
2018-06-10 15:52:37,818 - showlog.py[line:19] - INFO: i的值为:4,入参为:{'bdCode': 'GSXA0580'}
2018-06-10 15:52:37,818 - showlog.py[line:19] - INFO: i的值为:5,入参为:{'bdCode': 'GSXA0580'}
2018-06-10 15:52:37,818 - showlog.py[line:19] - INFO: i的值为:6,入参为:{'bdCode': 'GSXA0580'}
2018-06-10 15:52:37,818 - showlog.py[line:19] - INFO: i的值为:7,入参为:{'bdCode': 'GSXA0580'}
2018-06-10 15:52:37,818 - showlog.py[line:19] - INFO: i的值为:8,入参为:{'bdCode': 'GSXA0580'}
2018-06-10 15:52:37,818 - showlog.py[line:19] - INFO: i的值为:9,入参为:{'bdCode': 'GSXA0580'}
2018-06-10 15:52:37,819 - showlog.py[line:19] - INFO: i的值为:10,入参为:{'bdCode': 'GSXA0580'}
2018-06-10 15:52:37,819 - showlog.py[line:19] - INFO: i的值为:11,入参为:{'bdCode': 'GSXA0580'}
2018-06-10 15:52:37,819 - showlog.py[line:19] - INFO: i的值为:12,入参为:{'bdCode': 'GSXA0580'}
2018-06-10 15:52:37,819 - showlog.py[line:19] - INFO: i的值为:13,入参为:{'bdCode': 'GSXA0580'}
2018-06-10 15:52:37,820 - showlog.py[line:19] - INFO: i的值为:14,入参为:{'bdCode': 'GSXA0580'}
2018-06-10 15:52:37,820 - showlog.py[line:19] - INFO: i的值为:15,入参为:{'bdCode': 'GSXA0580'}
2018-06-10 15:52:37,820 - showlog.py[line:19] - INFO: i的值为:16,入参为:{'bdCode': 'GSXA0580'}
2018-06-10 15:52:37,820 - showlog.py[line:19] - INFO: i的值为:17,入参为:{'bdCode': 'GSXA0580'}
2018-06-10 15:52:37,820 - showlog.py[line:19] - INFO: i的值为:18,入参为:{'bdCode': 'GSXA0580'}
2018-06-10 15:52:37,821 - showlog.py[line:19] - INFO: i的值为:19,入参为:{'bdCode': 'GSXA0580'}

 

 

参考文档:http://python.jobbole.com/86887/

你可能感兴趣的:(python的logging模块简单应用)