简体   繁体   English

如何根据scrapy中的日志条目来源有选择地设置log_level?

[英]How to selectively set log_level depending on origin of log entry in scrapy?

Is it possible to have the log level for [scrapy.core.engine] and [scrapy.extensions.logstats] to be 'INFO' as well as my custom logger and have everything else set to 'WARNING'? 是否可以将[scrapy.core.engine]和[scrapy.extensions.logstats]的日志级别设置为'INFO'以及我的自定义记录器,并将其他所有设置为'WARNING'? I'd like to do this to remove some of the clutter from my log file. 我想这样做是为了清除日志文件中的一些混乱情况。

Thanks in advance! 提前致谢!

EDIT: 编辑:

I tried doing the following as described in this answer: 我尝试按照答案中的说明进行以下操作:

DEFAULT_LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'root': {'level': 'WARNING'},
    'loggers': {
        'twisted': {
            'level': 'ERROR',
        },
        'scrapy.core.engine': {
            'level': 'INFO',
        },
        'scrapy.extensions.logstats': {
            'level': 'INFO',
        },
    }
}

Unfortunately, this doesn't work for some reason. 不幸的是,由于某种原因,这是行不通的。 The default 'Warning' level doesn't get applied to all loggers and the specified levels for the loggers above seem to have no effect... 默认的“警告”级别不会应用到所有记录器,并且上面的记录器指定的级别似乎无效...

Apparently scrapy uses logging . 显然,scrapy使用logging You could use something like the following: 您可以使用如下所示的内容:

import logging
for key in logging.Logger.manager.loggerDict:
    if "scrapy.core.engine" in key or "scrapy.extensions.logstat" in key:
        logging.getLogger(key).setLevel(logging.WARNING)
logging.basicConfig(level=logging.INFO)

I haven't tested it with scrapy, but I use it for other packages. 我还没有对它进行草率的测试,但是我将它用于其他软件包。 Hope it works. 希望它能工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM