简单的日志处理示例
import logging
LOG_FILENAME = 'example.log'
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG)
logging.debug('This message should go to the log file')
##输出
DEBUG:root:This message should go to the log file
在上面的示例中,会看到所有的日志信息都包含一个root,root就是一个logger名称,在日志模块中可以设置不同的logger,每个logger可以有不同的配置,但是每个新的logger会“继承”logging模块的
import logging
logging.basicConfig(level=logging.WARNING)
logger1 = logging.getLogger('package1.module1')
logger2 = logging.getLogger('package2.module2')
logger1.warning('This message comes from one module')
logger2.warning('And this message comes from another module')
输出:
WARNING:package1.module1:This message comes from one module
WARNING:package2.module2:And this message comes from another module
python日志的配置方法
python的日志配置有三种方法:
1、 显式创建loggers,handlers,foramtters对象,调用对应的配置方法
2、创建logging配置文件,用fileConfig()函数读取配置文件
3、创建配置信息字典,将其传给dictConfig()函数
方法一:
import logging
# create logger
logger = logging.getLogger("simple_example")
logger.setLevel(logging.DEBUG)
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
logger.addHandler(ch)
# "application" code
logger.debug("debug message")
logger.info("info message")
logger.warn("warn message")
logger.error("error message")
logger.critical("critical message")
输出:
2016-02-02 15:43:44,575 - simple_example - DEBUG - debug message
2016-02-02 15:43:44,578 - simple_example - INFO - info message
2016-02-02 15:43:44,578 - simple_example - WARNING - warn message
2016-02-02 15:43:44,578 - simple_example - ERROR - error message
2016-02-02 15:43:44,578 - simple_example - CRITICAL - critical message
方法二:
import logging
import logging.config
logging.config.fileConfig("logging.conf")
# create logger
logger = logging.getLogger("simpleExample")
# "application" code
logger.debug("debug message")
logger.info("info message")
logger.warn("warn message")
logger.error("error message")
logger.critical("critical message")
logging.conf 文件
[loggers]
keys=root,simpleExample
[handlers]
keys=consoleHandler
[formatters]
keys=simpleFormatter
[logger_root]
level=DEBUG
handlers=consoleHandler
[logger_simpleExample]
level=DEBUG
handlers=consoleHandler
qualname=simpleExample
propagate=0
[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)
[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=
输出:
2016-02-02 16:28:41,075 - simpleExample - DEBUG - debug message
2016-02-02 16:28:41,075 - simpleExample - INFO - info message
2016-02-02 16:28:41,075 - simpleExample - WARNING - warn message
2016-02-02 16:28:41,075 - simpleExample - ERROR - error message
2016-02-02 16:28:41,075 - simpleExample - CRITICAL - critical message
方法三
import logging
import logging.config
import yaml
#logging.config.fileConfig("logging.conf")
input = yaml.load(file('dict.conf'))
logging.config.dictConfig(input)
# create logger
logger = logging.getLogger("simpleExample")
# "application" code
logger.debug("debug message")
logger.info("info message")
logger.warn("warn message")
logger.error("error message")
logger.critical("critical message")
dict.conf文件 :
version: 1
formatters:
simple:
format: format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
handlers:
console:
class: logging.StreamHandler
level: DEBUG
formatter: simple
stream: ext://sys.stdout
loggers:
simpleExample:
level: DEBUG
handlers: [console]
propagate: no
root:
level: DEBUG
handlers: [console]
输出:
format=2016-02-02 17:09:54,104 - simpleExample - DEBUG - debug message
format=2016-02-02 17:09:54,104 - simpleExample - INFO - info message
format=2016-02-02 17:09:54,104 - simpleExample - WARNING - warn message
format=2016-02-02 17:09:54,104 - simpleExample - ERROR - error message
format=2016-02-02 17:09:54,104 - simpleExample - CRITICAL - critical message