使用 structlog 设置日志级别
set log level with structlog
我正在尝试设置 structlog 并设置日志级别。我的代码如下所示:
import structlog
import logging
filepath=open("out.log",'a')
logging.basicConfig(
level=logging.INFO
)
structlog.configure(
processors=[structlog.stdlib.filter_by_level],
wrapper_class=structlog.BoundLogger,
context_class=dict,
logger_factory=structlog.PrintLoggerFactory(filepath),
)
logger = structlog.getLogger()
logger.info('test')
这失败了:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.8/site-packages/structlog/_base.py", line 189, in _proxy_to_logger
args, kw = self._process_event(method_name, event, event_kw)
File "/usr/local/lib/python3.8/site-packages/structlog/_base.py", line 149, in _process_event
event_dict = proc(self._logger, method_name, event_dict)
File "/usr/local/lib/python3.8/site-packages/structlog/stdlib.py", line 381, in filter_by_level
if logger.isEnabledFor(_NAME_TO_LEVEL[name]):
AttributeError: 'PrintLogger' object has no attribute 'isEnabledFor'
好的,好的。我不应该将 PrintLogger 与 stdlib 处理器一起使用。但我想按日志级别过滤(因为日志通常是这样工作的,嗯?)
那我该怎么做呢?我假设我需要使用其他一些记录器工厂,但是哪一个?
当然 structlog.stdlib.LoggerFactory 有效,但它不会重定向到文件。
所以我说:好的,我会创建自己的过滤器:
def my_filter_by_level(logger, name, event_dict):
if True:
return event_dict
else:
raise DropEvent
...
processors=[my_filter_by_level],
当我尝试使用记录器时,我得到:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.8/site-packages/structlog/_base.py", line 190, in _proxy_to_logger
return getattr(self._logger, method_name)(*args, **kw)
TypeError: msg() got an unexpected keyword argument 'organization'
这来自
logger = logger.bind(**{"organization": "blah"})
但是……为什么?我的处理器出了什么问题?
好吧,我的过滤器不起作用的原因是:
https://www.structlog.org/en/stable/processors.html#adapting
链中的最后一个过滤器很特殊,不能只是 return 字典。
下面的过滤器起到了作用:
def my_filter_by_level(logger, name, event_dict):
this_level = structlog.stdlib._NAME_TO_LEVEL[name]
set_level = structlog.stdlib._NAME_TO_LEVEL[loglevel]
if this_level >= set_level:
return event_dict
else:
raise structlog.DropEvent
这是设置默认日志级别的另一种方法。 'INFO' 以上的所有级别都将输出。
import logging
import structlog
structlog.configure(
wrapper_class=structlog.make_filtering_bound_logger(logging.INFO),
)
log = structlog.get_logger()
我正在尝试设置 structlog 并设置日志级别。我的代码如下所示:
import structlog
import logging
filepath=open("out.log",'a')
logging.basicConfig(
level=logging.INFO
)
structlog.configure(
processors=[structlog.stdlib.filter_by_level],
wrapper_class=structlog.BoundLogger,
context_class=dict,
logger_factory=structlog.PrintLoggerFactory(filepath),
)
logger = structlog.getLogger()
logger.info('test')
这失败了:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.8/site-packages/structlog/_base.py", line 189, in _proxy_to_logger
args, kw = self._process_event(method_name, event, event_kw)
File "/usr/local/lib/python3.8/site-packages/structlog/_base.py", line 149, in _process_event
event_dict = proc(self._logger, method_name, event_dict)
File "/usr/local/lib/python3.8/site-packages/structlog/stdlib.py", line 381, in filter_by_level
if logger.isEnabledFor(_NAME_TO_LEVEL[name]):
AttributeError: 'PrintLogger' object has no attribute 'isEnabledFor'
好的,好的。我不应该将 PrintLogger 与 stdlib 处理器一起使用。但我想按日志级别过滤(因为日志通常是这样工作的,嗯?) 那我该怎么做呢?我假设我需要使用其他一些记录器工厂,但是哪一个? 当然 structlog.stdlib.LoggerFactory 有效,但它不会重定向到文件。
所以我说:好的,我会创建自己的过滤器:
def my_filter_by_level(logger, name, event_dict):
if True:
return event_dict
else:
raise DropEvent
...
processors=[my_filter_by_level],
当我尝试使用记录器时,我得到:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.8/site-packages/structlog/_base.py", line 190, in _proxy_to_logger
return getattr(self._logger, method_name)(*args, **kw)
TypeError: msg() got an unexpected keyword argument 'organization'
这来自
logger = logger.bind(**{"organization": "blah"})
但是……为什么?我的处理器出了什么问题?
好吧,我的过滤器不起作用的原因是: https://www.structlog.org/en/stable/processors.html#adapting
链中的最后一个过滤器很特殊,不能只是 return 字典。
下面的过滤器起到了作用:
def my_filter_by_level(logger, name, event_dict):
this_level = structlog.stdlib._NAME_TO_LEVEL[name]
set_level = structlog.stdlib._NAME_TO_LEVEL[loglevel]
if this_level >= set_level:
return event_dict
else:
raise structlog.DropEvent
这是设置默认日志级别的另一种方法。 'INFO' 以上的所有级别都将输出。
import logging
import structlog
structlog.configure(
wrapper_class=structlog.make_filtering_bound_logger(logging.INFO),
)
log = structlog.get_logger()