废话少说,先上代码

  • 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()
问题一:测试过程中,只能打印出test_logger function一条语句
问题二:明明只在data_logger中打印出语句,但是logger的日志中也出现了相关的日志。
 
问题一解决方案:利用python -m pdb logger.py 语句对脚本进行调试发现,在执行instance = Test()语句后,通过print '\n'.join(['%s:%s' % item for item in self.data_logger.__dict__.items()])调试语句看到data_logger的disable属性值由0变成了True,此时logger的对应属性也发生了相同的变化。这种变化导致了logger对象停止记录日志。参考python  logging模块的相关手册发现“The 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函数重新加载一遍了。
 
问题二解决方案:logger对象有个属性propagate,如果这个属性为True,就会将要输出的信息推送给该logger的所有上级logger,这些上级logger所对应的handlers就会把接收到的信息打印到关联的日志中。logger.conf配置文件中配置了相关的root logger的属性,这个root logger就是默认的logger日志。
 
修改后的如下:

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模块无法正常输出日志的更多相关文章

  1. python logging模块详解[转]

    一.简单将日志打印到屏幕: import logging logging.debug('debug message') logging.info('info message') logging.war ...

  2. python logging模块使用

    近来再弄一个小项目,已经到收尾阶段了.希望加入写log机制来增加程序出错后的判断分析.尝试使用了python logging模块. #-*- coding:utf-8 -*- import loggi ...

  3. Python logging模块详解

    简单将日志打印到屏幕: import logging logging.debug('debug message') logging.info('info message') logging.warni ...

  4. 读懂掌握 Python logging 模块源码 (附带一些 example)

    搜了一下自己的 Blog 一直缺乏一篇 Python logging 模块的深度使用的文章.其实这个模块非常常用,也有非常多的滥用.所以看看源码来详细记录一篇属于 logging 模块的文章. 整个 ...

  5. (转)python logging模块

    python logging模块 原文:http://www.cnblogs.com/dahu-daqing/p/7040764.html 1 logging模块简介 logging模块是Python ...

  6. [转载]Python logging模块详解

    原文地址: http://blog.csdn.net/zyz511919766/article/details/25136485 简单将日志打印到屏幕: import logging logging. ...

  7. python logging—模块

    python logging模块 python logging提供了标准的日志接口,python logging日志分为5个等级: debug(), info(), warning(), error( ...

  8. 0x03 Python logging模块之Formatter格式

    目录 logging模块之Formatter格式 Formater对象 日志输出格式化字符串 LogRecoder对象 时间格式化字符串 logging模块之Formatter格式 在记录日志是,日志 ...

  9. 0x01 Python logging模块

    目录 Python logging 模块 前言 logging模块提供的特性 logging模块的设计过程 logger的继承 logger在逻辑上的继承结构 logging.basicConfig( ...

随机推荐

  1. 【Codeforces Round #301 (Div. 2) C】 Ice Cave

    [链接] 我是链接,点我呀:) [题意] 给你一个n*m的地图. 每个地图为0的时候可以安全走过,且走过后变成1. (一定要离开之后才会变成1) 而为1的则走过之后会掉入下一层. 你一开始在初始位置( ...

  2. C++——多态性实现机制

    C++的多态性实现机制剖析 1. 多态性和虚函数 #include <iostream.h> class animal { public: void sleep() { cout<& ...

  3. oracle高效分页存储过程(百万数据级)

    create or replace procedure Pager( page in number,--数据页数,从1开始 pageSize in number,--每页大小 tableName nv ...

  4. UVA 11388 - GCD LCM 水~

    看题传送门 题目大意: 输入两个数G,L找出两个正整数a 和b,使得二者的最大公约数为G,最小公倍数为L,如果有多解,输出a<=b且a最小的解,无解则输出-1 思路: 方法一: 显然有G< ...

  5. 【SPOJ 694】Distinct Substrings

    [链接]h在这里写链接 [题意]     给你一个长度最多为1000的字符串     让你求出一个数x,这个x=这个字符串的不同子串个数; [题解]     后缀数组题.     把原串复制一份,加在 ...

  6. 结合Wireshark捕获分组深入理解TCP/IP协议栈

    摘要:     本文剖析了浏览器输入URL到整个页面显示的整个过程,以百度首页为例,结合Wireshark俘获分组进行详细分析整个过程,从而更好地了解TCP/IP协议栈.   一.俘获分组 1.1 准 ...

  7. Vim技巧之四大模式_插入模式

    Vim技巧之四大模式_插入模式 在插入模式中及时更正错误 插入-普通模式 在插入模式模式以下直接粘贴指定寄存器的内容 插入模式中做运算 用字符编码插入很常使用字符 替换已有的文本 Vim技巧之四大模式 ...

  8. 那些移动端web踩过的坑

    原文链接:https://geniuspeng.github.io/2017/08/24/mobile-issues/ 扔了N久,还是捡回来了.好好弄一下吧.刚工作的时候挺忙的,后来不那么忙了,但是变 ...

  9. Hadoop文件的基本操作 分类: A1_HADOOP 2014-12-05 11:36 392人阅读 评论(0) 收藏

    Hadoop提供了大量的API对文件系统中的文件进行操作,主要包括: (1)读取文件 (2)写文件 (3)读取文件属性 (4)列出文件 (5)删除文件 1、读取文件 以下示例中,将hdfs中的一个文件 ...

  10. Android 节日短信送祝福(功能篇:1-数据库操作类与自定义ContentProvider)

    首先,还是展示一下部分目录结构:  在节日短信送祝福的功能实现方面,为了能够方便直观展示实现过程,小编我以Java文件为基础,一个一个来展示,免得到时候这个java文件写点,一下又跳到另外一个java ...