如何将elasticsearch库的日志级别设置为与我自己的日志不同?

19 人关注

我如何为elasticsearch库设置与自己的日志不同的日志级别?为了说明这个问题,我描述一下模块的情况。我有一个模块 lookup.py ,它像这样使用 elasticsearch

import logging
logger = logging.getLogger(__name__)
import elasticsearch
def get_docs():
    logger.debug("search elastic")
    es = elasticsearch.Elasticsearch('http://my-es-server:9200/')
    res = es.search(index='myindex', body='myquery')
    logger.debug("elastic returns %s hits" % res['hits']['total'])

然后在我的主文件中,我做

import logging
import lookup.py
logging.root.setLevel(loglevel(args))
get_docs()

我得到了很多来自以下方面的调试信息insideElasticsearch对象。我怎样才能用lookup.py中的一些代码抑制它们,而不抑制lookup.py本身的调试信息?Elasticsearch类似乎有一个logger对象;我我试着把它设置为None,但这并没有改变什么。

python
logging
module
halloleo
halloleo
发布于 2016-08-09
3 个回答
Thom
Thom
发布于 2021-02-03
已采纳
0 人赞同

以下两行对我来说已经完成了抑制来自es库的过多日志记录的工作。

es_logger = logging.getLogger('elasticsearch') es_logger.setLevel(logging.WARNING)

A. P.
A. P.
发布于 2021-02-03
0 人赞同

我一直在使用这个。

from elasticsearch import logger as es_logger
LOGLEVEL = 50
es_logger.setLevel(LOGLEVEL)
    
Cookie
Cookie
发布于 2021-02-03
0 人赞同

作为补充回答,如果你的项目中有几个记录器,你可以这样做。

import elasticsearch
es_logger = elasticsearch.logger
es_logger.setLevel(elasticsearch.logging.WARNING)

The pros:

  • You have full control and make sure that the elastic search logger won't collide with other loggers
  • You may set WARNING/ERROR or DEBUG levels without remembering what number will mean
  •