Python 函数——logging.basicConfig
2017-02-04 14:21
471 查看
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename='myapp.log',
filemode='w')
logging.debug('This is debug message')
logging.info('This is info message')
logging.warning('This is warning message')
例子:
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(thread)d %(threadName)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename=logfile_path,
filemode='w')结果:
Sat, 04 Feb 2017 13:53:15 4296 MainThread ScriptRecorder.py[line:38] DEBUG Start the application.
Sat, 04 Feb 2017 13:53:15 4296 MainThread ScriptRecorder.py[line:25] DEBUG Begin class constructor.
Sat, 04 Feb 2017 13:53:15 4296 MainThread ScriptRecorder.py[line:29] DEBUG Show the init window.
Sat, 04 Feb 2017 13:53:15 4296 MainThread init_window.py[line:18] DEBUG Init window constructor.
Sat, 04 Feb 2017 13:53:15 4296 MainThread init_window.py[line:62] DEBUG Get devices count.
Sat, 04 Feb 2017 13:53:15 4296 MainThread init_window.py[line:55] DEBUG Get devices list.
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename='myapp.log',
filemode='w')
logging.debug('This is debug message')
logging.info('This is info message')
logging.warning('This is warning message')
logging.basicConfig函数各参数: filename: 指定日志文件名 filemode: 和file函数意义相同,指定日志文件的打开模式,'w'或'a' format: 指定输出的格式和内容,format可以输出很多有用信息,如上例所示: %(levelno)s: 打印日志级别的数值 %(levelname)s: 打印日志级别名称 %(pathname)s: 打印当前执行程序的路径,其实就是sys.argv[0] %(filename)s: 打印当前执行程序名 %(funcName)s: 打印日志的当前函数 %(lineno)d: 打印日志的当前行号 %(asctime)s: 打印日志的时间 %(thread)d: 打印线程ID %(threadName)s: 打印线程名称 %(process)d: 打印进程ID %(message)s: 打印日志信息 datefmt: 指定时间格式,同time.strftime() level: 设置日志级别,默认为logging.WARNING stream: 指定将日志的输出流,可以指定输出到sys.stderr,sys.stdout或者文件,默认输出到sys.stderr,当stream和filename同时指定时,stream被忽略
例子:
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(thread)d %(threadName)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename=logfile_path,
filemode='w')结果:
Sat, 04 Feb 2017 13:53:15 4296 MainThread ScriptRecorder.py[line:38] DEBUG Start the application.
Sat, 04 Feb 2017 13:53:15 4296 MainThread ScriptRecorder.py[line:25] DEBUG Begin class constructor.
Sat, 04 Feb 2017 13:53:15 4296 MainThread ScriptRecorder.py[line:29] DEBUG Show the init window.
Sat, 04 Feb 2017 13:53:15 4296 MainThread init_window.py[line:18] DEBUG Init window constructor.
Sat, 04 Feb 2017 13:53:15 4296 MainThread init_window.py[line:62] DEBUG Get devices count.
Sat, 04 Feb 2017 13:53:15 4296 MainThread init_window.py[line:55] DEBUG Get devices list.
相关文章推荐
- python logging.basicConfig不生效的原因
- python的logging.config使用详解
- python的logging.config使用详解
- 使用Python的logging.config
- Python的logging.config
- 使用Python的logging.config
- python loggingconfig.dictConfig
- python中使用sys模板和logging模块获取行号和函数名的方法
- python常用模块补充hashlib configparser logging,subprocess模块
- python的logging.config使用详解
- python中使用sys模板和logging模块获取行号和函数名的方法
- Python print函数用法,print 格式化输出 (转自http://www.pythonclub.org/python-basic/print)
- 2.26 学python logging模块补充 configParser模块 re正则表达式
- python中使用sys模板和logging模块获取行号和函数名的方法
- python 中 logging 模块的 log 函数以及坑
- python-logging.config【日志模块】
- python3之xml&ConfigParser&hashlib&Subprocess&logging模块
- 使用Python的logging.config
- Python中logging.config配置文件解读
- python几个有意思的函数