Python 日志模块实例
废话少说,先上代码
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 日志模块实例的更多相关文章
- python日志模块logging
python日志模块logging 1. 基础用法 python提供了一个标准的日志接口,就是logging模块.日志级别有DEBUG.INFO.WARNING.ERROR.CRITICAL五种( ...
- python日志模块
许多应用程序中都会有日志模块,用于记录系统在运行过程中的一些关键信息,以便于对系 统的运行状况进行跟踪.在.NET平台中,有非常著名的第三方开源日志组件log4net,c++中,有人们熟悉的log4c ...
- Python日志模块logging用法
1.日志级别 日志一共分成5个等级,从低到高分别是:DEBUG INFO WARNING ERROR CRITICAL. DEBUG:详细的信息,通常只出现在诊断问题上 INFO:确认一切按预期运行 ...
- python日志模块的使用
学习一下python的日志模块logging,可以参考如下博客,写得很详细 https://www.cnblogs.com/yyds/p/6901864.html https://www.cnblog ...
- Python日志模块logging&JSON
日志模块的用法 json部分 先开一段测试代码:注意 str可以直接处理字典 eval可以直接将字符串转成字典的形式 dic={'key1':'value1','key2':'value2'} ...
- python日志模块logging学习
介绍 Python本身带有logging模块,其默认支持直接输出到控制台(屏幕),或者通过配置输出到文件中.同时支持TCP.HTTP.GET/POST.SMTP.Socket等协议,将日志信息发送到网 ...
- python日志模块笔记
前言 在应用中记录日志是程序开发的重要一环,也是调试的重要工具.但却很容易让人忽略.之前用flask写的一个服务就因为没有处理好日志的问题导致线上的错误难以察觉,修复错误的定位也很困难.最近恰好有时间 ...
- Python 日志模块详解
前言 我们知道查看日志是开发人员日常获取信息.排查异常.发现问题的最好途径,日志记录中通常会标记有异常产生的原因.发生时间.具体错误行数等信息,这极大的节省了我们的排查时间,无形中提高了编码效率.所以 ...
- Python日志模块的管理(二)
日志模块可以通过封装一个类,也可以通过配置文件取管理 新建1个log.ini文件 [loggers] keys=root [handlers] keys=fileHandler,streamHandl ...
随机推荐
- 一道关于 precision、recall 和 threshold关系的机器学习题
Suppose you have trained a logistic regression classifier which is outputing hθ(x). Currently, you p ...
- tp框架中的静态验证
//制定命名空间在Home 模块下Model文件夹下 如:namespace Home\Model; //引用父类 如:use Think\Model; //实例化表 如:class ZhuCeMod ...
- 总结分享十大iOS开发者最喜爱的库 分类: ios相关 app相关 2015-04-03 16:43 320人阅读 评论(0) 收藏
该10大iOS开发者最喜爱的库由"iOS辅导团队"成员Marcelo Fabri组织投票选举而得,参与者包括开发者团队,iOS辅导团队以及行业嘉宾.每个团队都要根据以下规则选出五个 ...
- 使用 System.Transactions 进行事物管理
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- partial类修饰符
partial是一个类修饰符,用于把类定义拆分为几个部分,便于代码管理,如class ClassA{void A(){;}void B(){;}}与partial class ClassA{void ...
- 用编程的方式定义UI界面
package com.example.administrator.test_hello_world; import android.app.Activity; import android.os.B ...
- codeforces 755C. PolandBall and Forest
C. PolandBall and Forest time limit per test 1 second memory limit per test 256 megabytes input stan ...
- delphi 获得memo,Richedit焦点所在行
procedure TForm1.Button1Click(Sender: TObject); var i:Integer; begin i:=SendMessage(Richedit1.handle ...
- http的Max-Forwards头的作用(转)
请求头的Max-Forwards用来请求特定代理.当代理收到一个允许URI转发的OPTIONS请求,则检查Max-Forwards.如果Max-Forwards值为0,则不能转发该消息:相反,代理会将 ...
- 我用Cocos2d-x模拟《Love Live!学院偶像祭》的Live场景(三)
[前言和思路整理] 千呼万唤Shi出来啊(好像也没人呼唤),最近公司项目紧,闲暇时间少更得慢,请见谅. 上一章我们分析并实现了打击物件类BeatObject,和它的父节点BeatObjectColum ...