python的日志处理
来源:互联网 发布:怎么修改英雄联盟网络 编辑:程序博客网 时间:2024/05/16 18:41
简单的日志处理示例
import loggingLOG_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 logginglogging.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=DEBUGhandlers=consoleHandler[logger_simpleExample]level=DEBUGhandlers=consoleHandlerqualname=simpleExamplepropagate=0[handler_consoleHandler]class=StreamHandlerlevel=DEBUGformatter=simpleFormatterargs=(sys.stdout,)[formatter_simpleFormatter]format=%(asctime)s - %(name)s - %(levelname)s - %(message)sdatefmt=
输出:
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 loggingimport logging.configimport yaml#logging.config.fileConfig("logging.conf")input = yaml.load(file('dict.conf'))logging.config.dictConfig(input)# create loggerlogger = logging.getLogger("simpleExample")# "application" codelogger.debug("debug message")logger.info("info message")logger.warn("warn message")logger.error("error message")logger.critical("critical message")
dict.conf文件 :
version: 1formatters: simple: format: format=%(asctime)s - %(name)s - %(levelname)s - %(message)shandlers: console: class: logging.StreamHandler level: DEBUG formatter: simple stream: ext://sys.stdoutloggers: simpleExample: level: DEBUG handlers: [console] propagate: no root: level: DEBUG handlers: [console]
输出:
format=2016-02-02 17:09:54,104 - simpleExample - DEBUG - debug messageformat=2016-02-02 17:09:54,104 - simpleExample - INFO - info messageformat=2016-02-02 17:09:54,104 - simpleExample - WARNING - warn messageformat=2016-02-02 17:09:54,104 - simpleExample - ERROR - error messageformat=2016-02-02 17:09:54,104 - simpleExample - CRITICAL - critical message
0 0
- python的日志处理
- Python处理日志
- python 日志处理
- python处理日志
- django/python日志logging 的配置以及处理
- python日志处理脚本模板
- -------日志文件的处理---------
- 日志的处理
- 日志文件的处理
- java日志的处理
- service的日志处理
- 小试python - 日志文件归并处理
- python中的异常处理+日志例子
- Python 封装使用日志处理类
- python使用装饰器作日志处理
- Python日志处理之logging模块
- python 日志的使用
- python的日志系统
- mongodb修改一列的值作为另一列的值/mongodb常用js、常用语句留底
- SDL2源代码分析2:窗口(SDL_Window)
- Application Framework层介绍
- HTML5 构建Web Form初试
- 10大最具价值开源软件 Ubuntu和MySQL上榜
- python的日志处理
- Ubuntu下Cron Log的启用设置
- 10个最有帮助的在线协同工具
- 第十一章 缓存机制——《跟我学Shiro》
- 外包能“敏捷”起来吗?
- 浅谈企业应该如何推广自己的网站
- 重装windows xp后,ubuntu linux无法启动的解决办法
- NodeJS折腾记--------------------------------------- Mac OSX 1. 删除/usr/local/lib中的所有node和node_modules 2
- Ubuntu加挂新硬盘全套解决方案