rkh*_*rkh 5 python logging python-2.7
我正在尝试使用以下内容启用我的python日志记录:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import logging.config
import os
test_filename = 'my_log_file.txt'
try:
logging.config.fileConfig('loggingpy.conf', disable_existing_loggers=False)
except Exception as e:
# try to set up a default logger
logging.error("No loggingpy.conf to parse", exc_info=e)
logging.basicConfig(level=logging.WARNING, format="%(asctime)-15s %(message)s")
test1_log = logging.getLogger("test1")
test1_log.critical("test1_log crit")
test1_log.error("test1_log error")
test1_log.warning("test1_log warning")
test1_log.info("test1_log info")
test1_log.debug("test1_log debug")
Run Code Online (Sandbox Code Playgroud)
我想使用loggingpy.conf文件来控制日志记录,如下所示:
[loggers]
keys=root
[handlers]
keys=handRoot
[formatters]
keys=formRoot
[logger_root]
level=INFO
handlers=handRoot
[handler_handRoot]
class=FileHandler
level=INFO
formatter=formRoot
args=(test_filename,)
[formatter_formRoot]
format=%(asctime)s:%(name)s:%(process)d:%(lineno)d %(levelname)s %(message)s
datefmt=
class=logging.Formatter
Run Code Online (Sandbox Code Playgroud)
在这里,我试图将日志记录路由到本地"test_filename"命名的文件.当我运行这个时,我得到:
ERROR:root:No loggingpy.conf to parse
Traceback (most recent call last):
File "logging_test.py", line 8, in <module>
logging.config.fileConfig('loggingpy.conf', disable_existing_loggers=False)
File "/usr/lib/python2.7/logging/config.py", line 85, in fileConfig
handlers = _install_handlers(cp, formatters)
File "/usr/lib/python2.7/logging/config.py", line 162, in _install_handlers
args = eval(args, vars(logging))
File "<string>", line 1, in <module>
NameError: name 'test_filename' is not defined
CRITICAL:test1:test1_log crit
ERROR:test1:test1_log error
WARNING:test1:test1_log warning
Run Code Online (Sandbox Code Playgroud)
读取文档时,似乎配置中的"args"值在日志包命名空间的上下文中被评估,而不是在调用fileConfig时的上下文.是否有任何可行的方法来尝试让日志记录通过配置文件以这种方式运行,这样我就可以配置动态日志文件名(通常类似于"InputFile.log"),但仍然可以灵活地使用日志配置文件来更改它?
即使这是一个老问题,我认为这仍然具有相关性.上述解决方案的替代方案是使用logging.config.dictConfig(...)和操纵字典.
MWE:
log_config.yml
version: 1
disable_existing_loggers: false
formatters:
default:
format: "%(asctime)s:%(name)s:%(process)d:%(lineno)d %(levelname)s %(message)s"
handlers:
console:
class: logging.StreamHandler
formatter: default
stream: ext://sys.stdout
level: DEBUG
file:
class: logging.FileHandler
formatter: default
filename: "{path}/service.log"
level: DEBUG
root:
level: DEBUG
handlers:
- file
- console
Run Code Online (Sandbox Code Playgroud)
example.py
import logging.config
import sys
import yaml
log_output_path = sys.argv[1]
log_config = yaml.load(open("log_config.yml"))
log_config["handlers"]["file"]["filename"] = log_config["handlers"]["file"]["filename"].format(path = log_output_path)
logging.config.dictConfig(log_config)
logging.debug("test")
Run Code Online (Sandbox Code Playgroud)
可执行如下:
python example.py .
结果:
两者都陈述如下:
2016-06-06 20:56:56,450:root:12232:11 DEBUG test
小智 5
您可以使用以下命令将文件名放在日志记录名称空间中:
logging.test_filename = 'my_log_file.txt'
Run Code Online (Sandbox Code Playgroud)
然后,您现有的loggingpy.conf文件应该可以工作
您应该能够在您的模块中使用您喜欢的任何内容污染日志记录命名空间(原因是-我不会尝试logging.config ='something'),这应该使它可以被config文件引用。
args语句使用处的eval进行解析logging.config.py _install_handlers。因此,您可以将代码添加到中args。
[handler_handRoot]
class=FileHandler
level=INFO
formatter=formRoot
args=(os.getenv("LOG_FILE","default_value"),)
Run Code Online (Sandbox Code Playgroud)
现在,您只需要填充环境变量。