pythoniku
pythoniku

Reputation: 3662

How to turn sqlalchemy logging off completely

sqlalchemy is keep loggin to console even I have the following code

import logging
logger = logging.getLogger()
logger.disabled = True

How to turn off sqlalchemy's logging completely?

Upvotes: 48

Views: 32958

Answers (5)

Takamura
Takamura

Reputation: 475

A more drastic solution:

import logging

logging.disable(logging.WARNING)

(To turn logging completely off you could set it to logging.FATAL, but it’s probably not a good idea…)

> help(logging.disable)
Help on function disable in module logging:
disable(level=50)
    Disable all logging calls of severity 'level' and below.

You can use the same function and choose the next logging level to get back to the previous state. E.g. to add back levels INFO, WARNING, etc. :

logging.disable(logging.DEBUG)

Upvotes: 3

MikeKulls
MikeKulls

Reputation: 1006

For anyone who has issues with none of the current answers helping, for me the logging was coming from a library used by sqlalchemy. Adding this line stopped the logging for me. If you're using a different DB then you will need a different value besides pyhive.

logging.getLogger('pyhive').setLevel(logging.ERROR)

How did I find this? I noticed in the logs that they mentioned hive.py, I misread that because I've used pyhive before and, well, it worked....

Upvotes: 0

Minty
Minty

Reputation: 152

The most complete solution:

sqlalchemy.engine.Engine does something weird (with log levels?), so while logging.getLogger('sqlalchemy.engine.Engine').setLevel(logging.ERROR) doesn't work, logging.disable(logging.ERROR) still can.

Combining the specificity and the method, I managed to disable it with:

logging.getLogger('sqlalchemy.engine.Engine').disabled = True

There. It actually works, and without breaking other or all loggers in the process like some other responses here.

Upvotes: 1

ostrokach
ostrokach

Reputation: 19912

You can turn off the sqlalchemy logger using:

import logging

logging.basicConfig()
logging.getLogger('sqlalchemy').setLevel(logging.ERROR)

For more info, see the docs.

Upvotes: 14

Palasaty
Palasaty

Reputation: 5549

Did you pass echo=True to create_engine()? By default it creates StreamHandler which outputs to console. As documentation says, if you didn't provide any echo=True arguments and didn't configure root sqlalchemy logger, it will not log anything.

Upvotes: 104

Related Questions