如何使用日志与python的fileConfig和配置日志文件文件名 [英] How to use logging with python's fileConfig and configure the logfile filename

查看:975
本文介绍了如何使用日志与python的fileConfig和配置日志文件文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于记录到控制台的日志配置文件和一个具有不同格式和级别的文件。在我的python脚本中,我可以加载这个配置,基本上控制台和文件输出是确定。



我在配置文件中设置文件名,如下所示。

是否可以在python脚本中设置该文件名?



python代码:

 #set up logging 
logging.config.fileConfig(loginipath)
logger = logging.getLogger 'sLogger')

#log something
logger.debug('debug message')
logger.info('info message')
logger.warn警告消息')
logger.error('错误消息')
logger.critical('关键消息')

记录配置文件

  [loggers] 
keys = root,sLogger

[handlers]
keys = consoleHandler,fileHandler

[formatters]
keys = fileFormatter,consoleFormatter

[logger_root]
level = DEBUG
handlers = consoleHandler

[logger_sLogger]
level = DEBUG
handlers = consoleHandler,fileHandler
qualname = sLogger
propagate = 0

[handler_consoleHandler]
class = StreamHandler
level = WARNING
formatter = consoleFormatter
args = (sys.stdout,)

[handler_fileHandler]
class = FileHandler
level = DEBUG
formatter = fileFormatter
args =('logfile.log' ,)

[formatter_fileFormatter]
format =%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt =

[formatter_consoleFormatter]
format =%(levelname)s - %(message)s
datefmt =


解决方案

更改 handler_fileHandler 部分,如下所示:

  [handler_fileHandler] 
class = FileHandler
level = DEBUG
formatter = fileFormatter
args = )s',)

,然后添加 defaults fileConfig调用的参数

  logging.fileConfig(loginipath,defaults = {'logfilename':'/ var / log / mylog.log'})


I have a logging configuration file for logging to console and a file with different formats and levels. In my python script I can load this configuration and basically console and file output are ok.

I set the file name in the config file as shown below.

Is it possible to set that file name in the python script itself?

python code:

# set up logging
logging.config.fileConfig(loginipath)
logger = logging.getLogger('sLogger')

# log something
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')

logging config file:

[loggers]
keys=root,sLogger

[handlers]
keys=consoleHandler,fileHandler

[formatters]
keys=fileFormatter,consoleFormatter

[logger_root]
level=DEBUG
handlers=consoleHandler

[logger_sLogger]
level=DEBUG
handlers=consoleHandler,fileHandler
qualname=sLogger
propagate=0

[handler_consoleHandler]
class=StreamHandler
level=WARNING
formatter=consoleFormatter
args=(sys.stdout,)

[handler_fileHandler]
class=FileHandler
level=DEBUG
formatter=fileFormatter
args=('logfile.log',)

[formatter_fileFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=

[formatter_consoleFormatter]
format=%(levelname)s - %(message)s
datefmt=

解决方案

Change your handler_fileHandler section like so:

[handler_fileHandler]
class=FileHandler
level=DEBUG
formatter=fileFormatter
args=('%(logfilename)s',)

and then add a defaults argument to the fileConfig call

logging.fileConfig(loginipath, defaults={'logfilename': '/var/log/mylog.log'})

这篇关于如何使用日志与python的fileConfig和配置日志文件文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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