如何在模块之间共享文件以登录python [英] How to share a file between modules for logging in python

查看:107
本文介绍了如何在模块之间共享文件以登录python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将消息从python中的不同模块记录到文件中.另外,我需要打印一些消息到控制台以进行调试.我为此使用了记录器模块.但是记录器模块会将具有指定严重性及以上严重性的所有日志记录到文件或控制台中.

I wanted to log messages from different module in python to a file. Also I need to print some messages to console for debugging purpose. I used logger module for this purpose . But logger module will log all the logs with given severity and above to file or console.

我只希望将某些消息记录到文件中,并且不应该包括来自控制台的消息.

I wanted only some messages logged to file and it should not include the messages from the console.

类似地,控制台消息中不应包含记录到文件中的消息.

Similarly the console messages should not contain messages logged to file.

我的方法是拥有一个单例类,该类在各个模块之间共享文件写操作.

My approach would be to have a singleton class which shares file write operation between various modules.

在python中,有没有比这更简单的方法了?

Is there any easier approach than this in python ?

我是Python的新手.我尝试过的示例程序

I am new to Python. Sample program I tried

logger = logging.getLogger('simple_example')
logger.setLevel(logging.INFO)
# create file handler which logs even debug messages
fh = logging.FileHandler('spam.log')
fh.setLevel(logging.CRITICAL)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
fh.setFormatter(formatter)
# add the handlers to logger
logger.addHandler(ch)
logger.addHandler(fh)

# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')

控制台打印:

2015-02-03 15:36:00,651 - simple_example - ERROR - error message
2015-02-03 15:36:00,651 - simple_example - CRITICAL - critical message
#I don't want critical messages in console.

推荐答案

这里是一个脚本,它创建两个记录器,并使用您希望记录到文件或stdout的记录器.问题是:您选择从哪个条件登录到stdout或文件,知道(从您的问题中得知)您不希望该条件成为日志级别(调试,错误,严重...)

Here is a script that creates two loggers, use the one you wish to log to a file or stdout. The question is : on which criteria do you choose to log to stdout or file, knowing that (from your question) you don't want the criteria to be the log level (debug, error, critical...)

#!/usr/bin/python

import logging

logger_stdout = logging.getLogger('logger_stdout')
logger_stdout.setLevel(logging.DEBUG)
sh = logging.StreamHandler()
sh.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
logger_stdout.addHandler(sh)
logger_stdout.debug('stdout debug message')

logger_file = logging.getLogger('logger_file')
logger_file.setLevel(logging.DEBUG)
fh = logging.FileHandler("foo.log")
fh.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
logger_file.addHandler(fh)
logger_file.debug('file debug message')

当我运行此脚本时:

D:\jrx\jrxpython                                                          
λ python essai.py                                                         
2015-02-03 11:12:07,210 - logger_stdout - DEBUG - stdout debug message    

D:\jrx\jrxpython                                                          
λ cat foo.log                                                             
2015-02-03 11:12:07,224 - logger_file - DEBUG - file debug message        

D:\jrx\jrxpython                                                          
λ   

这篇关于如何在模块之间共享文件以登录python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆