python Logger 高级教程
来源:互联网 发布:单片机型号大全 编辑:程序博客网 时间:2024/05/22 16:45
高级教程
logging library 四个模块
- lLoggers暴露的接口,给代码使用
- Handlers 把log 记录送到合适的destination
- Filter 过滤器,决定哪个log记录被输出
- Formatters ,最终输出的log记录格式
Logevent information 在loggers ,handlers ,filters 和formatters 中传递。
获得Logger:
我们通过Logger 类的实例来调用方法来记录日志。每个实例有不同的名字,并且被记录在namesapce里。
如果用于模块级别的日志记录,一个惯例是用以下模式来命名Logger:
ger= logging.getLogger(__name__)
可用来追踪包/模块 ,从logger名字直观上找到事件发生处。
Log的Destination:
日志记录可以被写入 files,HTTPGET/POST ,通过SMTP写入email,socket,或者系统级别日志,比如Window NT的事件日志。
Destinationsare served by handler classes. 如果内置的Handler 无法满足你的需求,可以定义自己的handlerClass。
下图是log 记录的传递过程
Logger类
三个作用:
1. 给代码暴露接口
2. 基于filter Object决定展示哪些log messages 被展示
3. Logger object 传递相关logmessages 给相关handler
Loggerl类有两类方法:
- 用来配置的方法
1. Logger.setLevel(lvl):说明会被处理的最低Level的log message
下面是每个logging level 对应的值
Level
Numeric value
CRITICAL
50
ERROR
40
WARNING
30
INFO
20
DEBUG
10
NOTSET
0
2. Logger.addHandler(hdlr)¶和Logger.removeHandler(hdlr)
Logger 中增加或者删除Handler
3. Logger.addFilter(filt) 和Logger.removeFilter(filt )
在Logger中增加或者删除 Filter
2.记录message的方法:
Logger.debug(msg, *args, **kwargs)
Logger.info(msg, *args, **kwargs)¶
Logger.warning(msg, *args, **kwargs)
Logger.error(msg, *args, **kwargs)
Logger.critical(msg, *args, **kwargs)
Logger.exception(msg, *args, **kwargs)¶在Logger.error层级
Logger.log(lvl, msg, *args, **kwargs)
Handlers
负责将Log message 分派到指定的destination
Handler.setLevel(lvl)
Handler.setFormatter(form) 他使用的Formatter
标准库包括比如StreamHandler(像sys.stout咿呀将logging输出)和FileHandler(将logging 输出于硬盘文件中)
Formatters
负责配置log message 的最终顺序,结构,及内容
多模块记录
多次调用logging.getLogger('someLogger')会返回同一个logger object的引用。不仅在一个module里这样,多个module也是这样。除此之外还可以在一个Module里生成父模块,在另一Module生成子 logger
main module:
importlogging
importauxiliary_module
# create logger with 'spam_application'
logger= logging.getLogger('spam_application')
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh= logging.FileHandler('spam.log')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch= logging.StreamHandler()
ch.setLevel(logging.ERROR)
# create formatter and add it to the handlers
formatter= logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(fh)
logger.addHandler(ch)
logger.info('creating an instance of auxiliary_module.Auxiliary')
a= auxiliary_module.Auxiliary()
logger.info('created an instance of auxiliary_module.Auxiliary')
logger.info('calling auxiliary_module.Auxiliary.do_something')
a.do_something()
logger.info('finished auxiliary_module.Auxiliary.do_something')
logger.info('calling auxiliary_module.some_function()')
auxiliary_module.some_function()
logger.info('done with auxiliary_module.some_function()')
auxiliary module:
importlogging
# create logger
module_logger= logging.getLogger('spam_application.auxiliary')
classAuxiliary:
def__init__(self):
self.logger= logging.getLogger('spam_application.auxiliary.Auxiliary')
self.logger.info('creating an instance of Auxiliary')
defdo_something(self):
self.logger.info('doing something')
a =1+1
self.logger.info('done doing something')
defsome_function():
module_logger.info('received a call to "some_function"')
输出
The output looks like this:
2005-03-23 23:47:11,663 - spam_application - INFO -
creating an instance of auxiliary_module.Auxiliary
2005-03-23 23:47:11,665 - spam_application.auxiliary.Auxiliary - INFO -
creating an instance of Auxiliary
2005-03-23 23:47:11,665 - spam_application - INFO -
created an instance of auxiliary_module.Auxiliary
2005-03-23 23:47:11,668 - spam_application - INFO -
calling auxiliary_module.Auxiliary.do_something
2005-03-23 23:47:11,668 - spam_application.auxiliary.Auxiliary - INFO -
doing something
2005-03-23 23:47:11,669 - spam_application.auxiliary.Auxiliary - INFO -
done doing something
2005-03-23 23:47:11,670 - spam_application - INFO -
finished auxiliary_module.Auxiliary.do_something
2005-03-23 23:47:11,671 - spam_application - INFO -
calling auxiliary_module.some_function()
2005-03-23 23:47:11,672 - spam_application.auxiliary - INFO -
received a call to 'some_function'
2005-03-23 23:47:11,673 - spam_application - INFO -
done with auxiliary_module.some_function()
链接:
https://docs.python.org/2/howto/logging.html#logging-advanced-tutorial
https://docs.python.org/2/howto/logging-cookbook.html?highlight=multiple%20log
- python Logger 高级教程
- python+selenium高级教程
- Python高级教程
- python logger
- Python高级教程之私有化
- Python正则表达式(高级教程)
- Python CGI编程(高级教程)
- Python 多线程(高级教程)
- Logger 使用教程
- python中的简单logger
- Python 之 Logger
- Python Logger module
- python logger 重写
- Python day14 logger复习
- python logger学习
- 循序渐进学习 Python logging (2) - 高级教程
- Python高级教程(一)面向对象
- Python高级教程之装饰器
- Head First PHP&MySQL学习笔记(四)
- VS2012 win32控制台调试总结
- 2D的水效果
- 提供给开发者 10 款最好的 Python IDE
- 初学DB2
- python Logger 高级教程
- Android APK反编译就这么简单 详解(附图)
- Python 代码性能优化技巧
- [LeetCode]179.Largest Number
- ubuntu 安装 花屏
- 内存溢出 内存泄漏
- 嵌入式资源网站
- git中配置autocrlf来正确处理crlf
- 新的10大数据挖掘、机器学习算法