python日志:如何将参数传递给日志配置文件

时间:2017-11-09 19:40:54

标签: python logging

我在下面的配置文件中记录了我的日志,我有一个“文件日志”,使用以下命令将日志信息写入文件:

flog = logging.getLogger(__file__)
flog.info('write!')

但是,我想在每次运行时指定一个不同的文件来记录。现在我只能写C:/record.log

如何传入参数以指示我要写入哪个文件?

[loggers]
keys: root, file

[formatters]
keys: detailed,simple

[handlers]
keys: console,filelog

[formatter_simple]
format: %(asctime)s-%(name)s-%(levelname)s:  %(message)s

[formatter_detailed]
format: %(asctime)s-%(name)s-%(levelname)s-%(lineno)d:  %(message)s

[handler_console]
class: StreamHandler
args: []
formatter: detailed

[handler_filelog]
class: handlers.RotatingFileHandler
args: ('C:/record.log','a', 1000000, 5)
formatter: detailed

[logger_root]
level: DEBUG
handlers: console

[logger_file]
level: INFO
qualname: file
handlers: filelog

1 个答案:

答案 0 :(得分:0)

一个老问题,但无论如何: 您应该将默认参数传递给配置解析器。参见SO:https://stackoverflow.com/a/49589760/9153403https://docs.python.org/2/library/logging.config.html