问题描述
我正在尝试将日志记录添加到我的django项目中这是我的记录器配置:
i'm trying to add logging to my django projecthere's my logger configuration:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'standard': {
'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s',
},
},
'handlers': {
'default': {
'class': 'logging.FileHandler',
'filename': os.path.join(*[BASE_DIR, 'logfiles', 'debug.log']),
'formatter': 'standard',
},
'apps_errors': {
'level': 'ERROR',
'class': 'logging.FileHandler',
'filename': os.path.join(*[BASE_DIR, 'logfiles', 'apps_errors.log']),
'formatter': 'standard',
},
'dev_logger': {
'level': 'INFO',
'class': 'logging.FileHandler',
'filename': os.path.join(*[BASE_DIR, 'logfiles', 'apps_logs.log']),
'formatter': 'standard',
},
},
'loggers': {
'': {
'handlers': ['default'],
'level': 'DEBUG',
'propagate': False,
},
'gui': {
'handlers': ['dev_logger', 'apps_errors'],
'propagate': True,
},
'crawler': {
'handlers': ['dev_logger', 'apps_errors'],
'propagate': True,
},
},
}
如您所见,我想将所有内容记录到 debug.log
并将应用程序(爬网程序和gui)上的错误记录到apps_errors.log
并将有关应用程序(爬网程序和gui)的信息记录到apps_logs.log
As you can see, I want to log everything to debug.log
and log errors on apps (crawler and gui) to apps_errors.log
and log info on apps (crawler and gui) to apps_logs.log
debug.log和apps_errors.log正常工作,所有内容都记录到 debug.log
,只有我2个应用程序中的错误记录到 apps_errors.log
,但是当涉及到 apps_logs.log
时,当它仅应作为信息时,我会不断收到错误和信息
debug.log and apps_errors.log are working normally, everything is being logged to debug.log
and only errors in my 2 apps, are being logged to apps_errors.log
, but when it comes to apps_logs.log
i keep getting both errors and info when it should be only info
顺便说一句,当我想记录我正在做的事情时
by the way when i want to log something i'm doing
import logging
logger = logging.getLogger(__name__)
我正在获取信息: logging.info(my_info)
我正在执行的错误: logging.exception(my_exception)
我也尝试过 logging.error(my_exception)
for errors i'm doing: logging.exception(my_exception)
i also tried logging.error(my_exception)
PS:
我尝试定义两个记录器,每个处理程序一个,但是使用错误记录器/处理程序仅记录错误,信息不起作用
PS:
i have tried defining two loggers, one for each handler, but doing that logs only errors using the errors logger/handler, the info one doesn't work
'gui': {
'handlers': ['dev_logger'],
'level': 'INFO',
'propagate': True,
},
'gui': {
'handlers': ['apps_errors'],
'level': 'ERROR',
'propagate': True,
推荐答案
如果出于某些原因,您仅只希望INFO消息显示在处理程序的输出中,但没有更高的严重性,则需要将过滤器附加到该处理程序.这不是常见的要求-尽管隔离错误和日志记录较多是很常见的,但仅隔离 INFO消息并不常见.使用过滤器应该可以:
If for some reason you only want INFO messages to show up in a handler's output, but nothing of higher severity, you would need to attach a filter to that handler. This is not a common requirement - though it's common to isolate errors and greater in logs, it's not common to isolate only INFO messages. Using a filter should work:
import logging
class InfoFilter(logging.Filter):
def filter(self, record):
return record.level == logging.INFO
,然后将该过滤器分配给您的处理程序 dev_logger
.不知道为什么将其称为 dev_logger
-也许您需要查看对记录器和处理程序的了解.高级教程的顶部进行了总结.
and then assign that filter to your handler dev_logger
. Not sure why you've called it dev_logger
- perhaps you need to review your understanding of loggers and handlers. The top of the advanced tutorial gives a summary.
这篇关于Django logger在各个级别之间混合:错误和信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!