Search code examples
pythonloggingmodule

How to set the logging level for the elasticsearch library differently to my own logging?


How can I set the logging level for the elasticsearch library differently to my own logging? To illustrate the issue, I describe the module scenario. I have a module lookup.py which uses elasticsearch like this:

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'])
    .
    .
.

Then in my main file I do

import logging
import lookup.py

logging.root.setLevel(loglevel(args))
get_docs()
.
.
.

I get lots of debug messages from inside the Elasticsearch object. How can I suppress them with some code in lookup.py without suppressing the debug messages in lookup.py itself? The Elasticsearch class seems to have a logger object; I I tried to set it to None, but this didn't change anything.


Solution

  • The following two lines have done the trick for me to suppress excessive logging from the es library.

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