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.
By default, the root log level is WARN, so every log with lower level (for example via logging.info("info") ) will be ignored. Another particularity of the root logger is that its default handler will be created the first time a log with a level greater than WARN is logged.
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)
I have been using this:
from elasticsearch import logger as es_logger
LOGLEVEL = 50
es_logger.setLevel(LOGLEVEL)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With