由于 python
频繁打印会导致IDE或者系统奔溃,因此将其打印信息写入日志文件中:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import logging
# 设置日志存储路径
# CRITICAL,ERROR,WARNING,INFO,DEBUG,NOTSET
# 当前时间asctime:2017-03-20 17:51:13,721
# 日志等级levelname:INFO
# 执行的py文件名filename:test.py
# 记录日志的代码行位置lineno:11
# 日志的信息message:日志功能 1234
logging.basicConfig(filename='mylog.log', format="[%(asctime)s][%(levelname)s][%(filename)s:%(lineno)d] %(message)s",
level=logging.INFO)
logging.info("日志功能 %s" % "1234")
# 记录结果
# [2017-03-20 17:51:13,721][INFO][test.py:11] 日志功能 1234
默认的参数为:
CRITICAL = 50
FATAL = CRITICAL
ERROR = 40
WARNING = 30
WARN = WARNING
INFO = 20
DEBUG = 10
NOTSET = 0