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_fileFile: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( ...
随机推荐
- P2P借款的几种情况
借款,至少出现2种人,借款人和出借人.根据人的性质,企业和个人,分成4种情况. 企业-个人,企业-企业,个人-企业,个人-个人. P2P平台可能出现几种情况: 个人-个人 2种情况: a. 借款人 ...
- Java基础学习总结(50)——Java事务处理总结
一.什么是Java事务 通常的观念认为,事务仅与数据库相关. 事务必须服从ISO/IEC所制定的ACID原则.ACID是原子性(atomicity).一致性(consistency).隔离性(isol ...
- Virtualizing memory type
A processor, capable of operation in a host machine, including memory management logic to support a ...
- Apache的.htaccess项目根文件夹伪静态设置规则
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...
- JMS服务器ActiveMQ的初体验并持久化消息到MySQL数据库中
JMS服务器ActiveMQ的初体验并持久化消息到MySQL数据库中 一.JMS的理解JMS(Java Message Service)是jcp组织02-03年定义了jsr914规范(http://j ...
- [TypeScript] Union Types and Type Aliases in TypeScript
Sometimes we want our function arguments to be able to accept more than 1 type; e.g. a string or an ...
- linux 安装完mysql 密码重置
If you have forgot the MySQL root password, can’t remember or want to break in….. you can reset them ...
- 【20.23%】【codeforces 740A】Alyona and copybooks
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- [RxJS] Reusable multicasting with Subject factories
The way we use publish() (or multicast with an RxJS Subject) makes the shared Observable not reusabl ...
- jquery如何实现点击标题收缩下面的内容
jquery如何实现点击标题收缩下面的内容 一.总结 一句话总结:怎么做复杂前端任务,先把样式(最简单)做出来,然后在写js. 1.如何取jquery集合中的某个索引号的元素? 不是get(),是eq ...