Python logging模块无法正常输出日志
废话少说,先上代码
- File:logger.conf
- [formatters]
- keys=default
- [formatter_default]
- format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
- class=logging.Formatter
- [handlers]
- keys=console, error_file
- [handler_console]
- class=logging.StreamHandler
- formatter=default
- args=tuple()
- [handler_error_file]
- class=logging.FileHandler
- level=INFO
- formatter=default
- args=("logger.log", "a")
- [loggers]
- keys=root
- [logger_root]
- level=DEBUG
- formatter=default
- handlers=console,error_file
- File:logger.py
- #!/bin/env python
- import logging
- from logging.config import logging
- class Test(object):
- """docstring for Test"""
- def __init__(self):
- logging.config.fileConfig("logger.conf")
- self.logger = logging.getLogger(__name__)
- def test_func(self):
- self.logger.error('test_func function')
- class Worker(object):
- """docstring for Worker"""
- def __init__(self):
- logging.config.fileConfig("logger.conf")
- self.logger = logging.getLogger(__name__)
- data_logger = logging.getLogger('data')
- handler = logging.FileHandler('./data.log')
- fmt = logging.Formatter('%(asctime)s|%(message)s')
- handler.setFormatter(fmt)
- data_logger.addHandler(handler)
- data_logger.setLevel(logging.DEBUG)
- self.data_logger = data_logger
- def test_logger(self):
- self.data_logger.error("test_logger function")
- instance = Test()
- self.data_logger.error("test_logger output")
- instance.test_func()
- def main():
- worker = Worker()
- worker.test_logger()
- if __name__ == '__main__':
- main()
fileConfig()
function takes a default parameter, disable_existing_loggers
, which defaults to True
for reasons of backward compatibility. This may or may not be what you want, since it will cause any loggers existing before the fileConfig()
call to be disabled unless they (or an ancestor) are explicitly named in the configuration.” 的说明,即调用fileconfig()函数会将之前存在的所有logger禁用。在python 2.7版本该fileConfig()函数添加了一个参数,logging.config.
fileConfig
(fname, defaults=None, disable_existing_loggers=True),可以显式的将disable_existing_loggers设置为FALSE来避免将原有的logger禁用。将上述代码中的Test类中的logging.config.
fileConfig函数改成logging.config.fileConfig("./logger.conf", disable_existing_loggers=0)就可以解决问题。 不过该代码中由于位于同一程序内,可以直接用
logging.getLogger(LOGGOR_NAME)函数引用同一个logger,不用再调用logging.config.fileConfig函数重新加载一遍了。
- File:logger.conf
- [formatters]
- keys=default, data
- [formatter_default]
- format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
- class=logging.Formatter
- [formatter_data]
- format=%(asctime)s|%(message)s
- class=logging.Formatter
- [handlers]
- keys=console, error_file, data_file
- [handler_console]
- class=logging.StreamHandler
- formatter=default
- args=tuple()
- [handler_error_file]
- class=logging.FileHandler
- level=INFO
- formatter=default
- args=("logger.log", "a")
- [handler_data_file]
- class=logging.FileHandler
- level=INFO
- formatter=data
- args=("data_new.log", "a")
- [loggers]
- keys=root, data
- [logger_root]
- level=DEBUG
- handlers=console,error_file
- [logger_data]
- level=DEBUG
- handlers=data_file
- qualname=data
- propagate=0
- File:logger.py
- #!/bin/env python
- import logging
- from logging.config import logging
- class Test(object):
- """docstring for Test"""
- def __init__(self):
- self.logger = logging.getLogger(__name__)
- def test_func(self):
- self.logger.error('test_func function')
- class Worker(object):
- """docstring for Worker"""
- def __init__(self):
- logging.config.fileConfig("logger.conf")
- self.logger = logging.getLogger(__name__)
- self.data_logger = logging.getLogger('data')
- def test_logger(self):
- self.data_logger.error("test_logger function")
- instance = Test()
- self.data_logger.error("test_logger output")
- instance.test_func()
- def main():
- worker = Worker()
- worker.test_logger()
- if __name__ == '__main__':
- main()
Python logging模块无法正常输出日志的更多相关文章
- python logging模块详解[转]
一.简单将日志打印到屏幕: import logging logging.debug('debug message') logging.info('info message') logging.war ...
- python logging模块使用
近来再弄一个小项目,已经到收尾阶段了.希望加入写log机制来增加程序出错后的判断分析.尝试使用了python logging模块. #-*- coding:utf-8 -*- import loggi ...
- Python logging模块详解
简单将日志打印到屏幕: import logging logging.debug('debug message') logging.info('info message') logging.warni ...
- 读懂掌握 Python logging 模块源码 (附带一些 example)
搜了一下自己的 Blog 一直缺乏一篇 Python logging 模块的深度使用的文章.其实这个模块非常常用,也有非常多的滥用.所以看看源码来详细记录一篇属于 logging 模块的文章. 整个 ...
- (转)python logging模块
python logging模块 原文:http://www.cnblogs.com/dahu-daqing/p/7040764.html 1 logging模块简介 logging模块是Python ...
- [转载]Python logging模块详解
原文地址: http://blog.csdn.net/zyz511919766/article/details/25136485 简单将日志打印到屏幕: import logging logging. ...
- python logging—模块
python logging模块 python logging提供了标准的日志接口,python logging日志分为5个等级: debug(), info(), warning(), error( ...
- 0x03 Python logging模块之Formatter格式
目录 logging模块之Formatter格式 Formater对象 日志输出格式化字符串 LogRecoder对象 时间格式化字符串 logging模块之Formatter格式 在记录日志是,日志 ...
- 0x01 Python logging模块
目录 Python logging 模块 前言 logging模块提供的特性 logging模块的设计过程 logger的继承 logger在逻辑上的继承结构 logging.basicConfig( ...
随机推荐
- IE block my cookie in iframe
---恢复内容开始--- There is a severe bug that a leader figured it out in a published project. In IE11, the ...
- Java 接口中定义抽象方法有什么意义
接口方法声明只能是public abstract的,所以不管你在声明的时候加不加abstract,都是可以的.Java 8开始,接口还引入了默认方法,也就是可以给接口的方法提供默认的实现,默认方法应当 ...
- [WASM] Create and Run a Native WebAssembly Function
In this introduction, we show a simple WebAssembly function that returns the square root of a number ...
- u-boot-2011.06在基于s3c2440开发板的移植之引导内核与加载根文件系统
http://www.linuxidc.com/Linux/2012-09/70510.htm 来源:Linux社区 作者:赵春江 uboot最主要的功能就是能够引导内核启动.本文就介绍如何实现该 ...
- width:100%和width:inherit
前几天遇到过这么一个问题.我想让子盒子的宽度等于父盒子的宽度.父盒子宽度为一个具体值比如说200px.我将子盒子宽度设为了100%.按道理说应该是可以等于父盒子的宽度的,但结果并没有,而是通栏了.然后 ...
- ETL概述 分类: H2_ORACLE 2013-08-23 10:36 344人阅读 评论(0) 收藏
转自:http://blog.csdn.net/leosoft/article/details/4279536 ETL,Extraction-Transformation-Loading的缩写,中文名 ...
- 使用Opencv中matchTemplate模板匹配方法跟踪移动目标
模板匹配是一种在图像中定位目标的方法,通过把输入图像在实际图像上逐像素点滑动,计算特征相似性,以此来判断当前滑块图像所在位置是目标图像的概率. 在Opencv中,模板匹配定义了6种相似性对比方式: C ...
- Android自定义组件系列【6】——进阶实践(3)
上一篇<Android自定义组件系列[5]--进阶实践(2)>继续对任老师的<可下拉的PinnedHeaderExpandableListView的实现>进行了分析,这一篇计划 ...
- Java NIO学习笔记之基本概念
一.缓冲区操作 缓冲区,以及缓冲区如何工作,是所有 I/O 的基础.所谓"输入/输出"讲的无非就是把数据移进或移出缓冲区. 进程使用 read( )系统调用,要求其缓冲区被填满.内 ...
- zookeeper无法启动"Unable to load database on disk"
自己的虚拟机集群.一次强制关机后,发现slave2的zookeeper起不来了 http://blog.csdn.net/ashic/article/details/47088299 下午5点29:5 ...