Python logging(配置实现)
来源:互联网 发布:mybatis怎么打印sql 编辑:程序博客网 时间:2024/06/06 01:04
定义我们的日志模块
定义统一入口,读取配置文件
#mylog.pyimport loggingimport logging.configdef getLogger(name='root'): CONF_LOG = "/home/stephen/openstack/demo/log/log/logging.conf" logging.config.fileConfig(CONF_LOG) return logging.getLogger(name)
日志配置文件
我们配置了三种类型的日志,root, test1, test2,我们在使用是只需指明我们所使用的类型。
#logging.conf[loggers]keys=root,test1,test2[handlers]keys=rotatingFileHandler,test1Handler,test2Handler[formatters]keys=simpleFmt[logger_root]level=DEBUGhandlers=rotatingFileHandler[logger_test1]level=DEBUGhandlers=test1Handlerqualname=test1propagate=0[logger_test2]level=DEBUGhandlers=test2Handlerqualname=test2propagate=0 [handler_rotatingFileHandler]class=handlers.RotatingFileHandlerlevel=DEBUGformatter=simpleFmtargs=("/tmp/default.log", "a", 20*1024*1024, 10)[handler_test1Handler]class=handlers.RotatingFileHandlerlevel=DEBUGformatter=simpleFmtargs=("/tmp/test1.log", "a", 20*1024*1024, 10) [handler_test2Handler]class=handlers.RotatingFileHandlerlevel=DEBUGformatter=simpleFmtargs=("/tmp/test2.log", "a", 20*1024*1024, 10) [formatter_simpleFmt]format=%(asctime)s %(pathname)s(%(lineno)d): %(levelname)s %(message)s
我们的测试模块
我们在模块中的调用方式如下,就是指明我们的日志名称,也即是指明我们将这个模块的日志打到那个文件。
#test1.pyimport mylog as loggingLOGGER = logging.getLogger("test1")class TestLog1(object): """docstring for TestLog1""" def __init__(self, arg=None): super(TestLog1, self).__init__() self.arg = arg LOGGER.info("test1 info mesage will wirte in test1.log") LOGGER.warning("test1 warning mesage will wirte in test1.log") LOGGER.debug("test1 debug mesage will wirte in test1.log")
#test2.pyimport mylog as loggingLOGGER = logging.getLogger("test2")class TestLog2(object): """docstring for TestLog2""" def __init__(self, arg=None): super(TestLog2, self).__init__() self.arg = arg LOGGER.info("test2 info mesage will wirte in test2.log") LOGGER.warning("test2 warning mesage will wirte in test2.log") LOGGER.debug("test2 debug mesage will wirte in test2.log")
测试入口
测试的入口,我们执行python test.py即可
在这里我们并没有指定日志类型,也就是说没有指明我们要把日志打到那个文件,当然就会到我们定义好的默认文件root日志中:
#test.pyimport mylog as loggingimport test1import test2LOG = logging.getLogger(__name__)def main(): LOG.info("test info message:test1.TestLog1()") test1.TestLog1() LOG.info("test info message: test1.TestLog2()") test2.TestLog2()if __name__ == '__main__': main()
测试结果
default.log
2016-01-04 10:43:02,052 test.py(10): INFO test info message:test1.TestLog1()
2016-01-04 10:43:02,052 test.py(12): INFO test info message: test1.TestLog2()test1.log
2016-01-04 10:43:02,052 /home/stephen/openstack/demo/log/log/test1.py(12): INFO test1 info mesage will wirte in test1.log
2016-01-04 10:43:02,052 /home/stephen/openstack/demo/log/log/test1.py(13): WARNING test1 warning mesage will wirte in test1.log
2016-01-04 10:43:02,052 /home/stephen/openstack/demo/log/log/test1.py(14): DEBUG test1 debug mesage will wirte in test1.logtest2.log
2016-01-04 10:43:02,052 /home/stephen/openstack/demo/log/log/test2.py(12): INFO test2 info mesage will wirte in test2.log
2016-01-04 10:43:02,052 /home/stephen/openstack/demo/log/log/test2.py(13): WARNING test2 warning mesage will wirte in test2.log
2016-01-04 10:43:02,052 /home/stephen/openstack/demo/log/log/test2.py(14): DEBUG test2 debug mesage will wirte in test2.log详请参考:http://python.usyiyi.cn/python_278/library/logging.config.html
- 官方教程:http://python.usyiyi.cn/python_278/howto/logging.html#logging-basic-tutorial
- Python logging(配置实现)
- Python logging(代码实现)
- python logging 日志配置
- python logging 日志配置
- python logging 日志详细配置
- python logging文件配置示例
- python logging 日志详细配置
- Configuring Logging(Python)
- [Python--] Python logging模块实现邮件报警
- Python logging.handlers.SMTPHandler的参数配置
- python日志模块(logging)
- python:logging模块(上)
- Python logging日志(1)
- Python logging日志(2)
- python logging 模块(一)
- python logging 模块(二)
- python logging 模块(三)
- python logging模块(1)
- 【bzoj2049】[Sdoi2008]Cave 洞穴勘测 动态树
- [Linux]管道命令 ”|“
- 语义WEB服务
- php手册-关于true和false
- java查询优化
- Python logging(配置实现)
- 鼠标悬浮 边框动态出来特效
- [深入ReactNative]第一篇 通讯及消息循环代码剖析
- HDU 5606 tree(并查集)
- 自定义UISearchiBar
- Python集合(set)类型的操作
- jqGrid增删改查显示——显示+nodejs后台
- java.lang.IllegalStateException: Fragment(XXFragment) not attached to Activity异常。
- 如何定义XSD并在XML中使用XSD