import logging
"""
logging is filtered by division of information into different levels
DEBUG is the Basic level, While the CRITICAL is the highest level
"""
# logging.basicConfig(level=logging.DEBUG)
# logging.basicConfig(level=logging.INFO)
""" The output will be same, if WARN replace WARNING """
# logging.basicConfig(level= logging.WRAN)
# logging.basicConfig(level=logging.WARNING)
# logging.basicConfig(level=logging.ERROR)
logging.basicConfig(level=logging.CRITICAL)
logging.debug('this is debug info')
logging.info('info is : hello you')
logging.warning('warning info')
logging.error('this is error info')
logging.critical('critical info')
DEBUG:root:this is debug info
INFO:root:info is : hello you
WARNING:root:warning info
ERROR:root:this is error info
CRITICAL:root:critical info
ERROR:root:this is error info
CRITICAL:root:critical info
格式 描述
%(levelno)s 打印日志级别的数值
%(levelname)s 打印日志级别名称
%(pathname)s 打印当前执行程序的路径
%(filename)s 打印当前执行程序名称
%(funcName)s 打印日志的当前函数
%(lineno)d 打印日志的当前行号
%(asctime)s 打印日志的时间
%(thread)d 打印线程id
%(threadName)s 打印线程名称
%(process)d 打印进程ID
%(message)s 打印日志信息
在logging库中定义如下:
CRITICAL = 50
FATAL = CRITICAL
ERROR = 40
WARNING = 30
WARN = WARNING
INFO = 20
DEBUG = 10
NOTSET = 0
logging.basicConfig(level=logging.DEBUG,filename='../log/runlog.log',format='%(levelno)s %(asctime)s %(threadName)s %(filename)s %(lineno)d %(levelname)s %(message)s')
the output
10 2018-12-05 16:38:00,982 MainThread log_practice.py 25 DEBUG this is debug info
20 2018-12-05 16:38:00,982 MainThread log_practice.py 26 INFO info is : hello you
30 2018-12-05 16:38:00,982 MainThread log_practice.py 27 WARNING warning info
40 2018-12-05 16:38:00,982 MainThread log_practice.py 28 ERROR this is error info
50 2018-12-05 16:38:00,982 MainThread log_practice.py 29 CRITICAL critical info
配置文件的理解!!!!, 学习一下
log.conf
[loggers]
keys=root,infoLogger
[logger_root]
level=DEBUG
handlers=consoleHandler,fileHandler
[logger_infoLogger]
handlers=consoleHandler,fileHandler
qualname=infoLogger
propagate=0
[handlers]
keys=consoleHandler,fileHandler
[handler_consoleHandler]
class=StreamHandler
level=INFO
formatter=form02
args=(sys.stdout,)
[handler_fileHandler]
class=FileHandler
level=INFO
formatter=form01
args=('../logs/runlog.log', 'a')
[formatters]
keys=form01,form02
[formatter_form01]
format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s
[formatter_form02]
format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s
调用
import logging
import logging.config
CON_LOG='log.conf'
logging.config.fileConfig(CON_LOG)
logging=logging.getLogger()