How to turn sqlalchemy logging off completely
Asked Answered
T

4

44

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?

Teriteria answered 19/9, 2013 at 17:5 Comment(1)
Can you post your engine configuration and logging configuration?Cryoscopy
P
97

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.

Printery answered 19/9, 2013 at 18:0 Comment(1)
or it can be SQLALCHEMY_ECHO env var in flask configApollinaire
D
15

You can turn off the sqlalchemy logger using:

import logging

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

For more info, see the docs.

Demandant answered 16/2, 2016 at 2:9 Comment(11)
Can someone explain the downvote? Setting logging verbosity at the config level is much better practice than having to pass the echo=False to all functions that may ever create an engine.Demandant
For me I downvoted because I tried this technique and it didn't worked.Turpentine
Changed the anser from logging.getLogger('sqlalchemy.engine') to logging.getLogger('sqlalchemy'), which should cover more cases. Other than that, don't know why it wouldn't work...Demandant
Thank you for updating the code, but on my end it still doesn't work. Here's the error: /usr/lib64/python2.7/site-packages/sqlalchemy/engine/default.py:450: Warning: Duplicate entry 'some-key-duplicate' for key 'title' cursor.execute(statement, parameters)Turpentine
@CyrilN. this does not look like it's related to logging. There is probably an error in your statement query. Maybe you're trying to insert into the same column multiple times or something..Demandant
The warning are because I'm trying to insert an entry that fails because of unique constraints. The query contains an "INSERT IGNORE" so it works nonetheless (no entries are added), but so should not display a warning. My issue here is that in the end, SQLAlchemy show me a warning I'd like to silent, which is the subject of this question.Turpentine
I've removed my downvote since your answer works in general (tried to change to debug and got more details). But I don't know why the warning keeps printing.Turpentine
@Demandant - Even after setting the log level for sqlalchemy I have detailed SQL output plus these startup messages: SHOW VARIABLES LIKE 'sql_mode' SELECT VERSION() SELECT DATABASE() SELECT @@tx_isolation show collation where Charset = 'utf8' and Collation = 'utf8_bin'Sachet
I also downvoted. While this should work, SQL alchemy goes to some length to bypass this. Contrary to all common sense and best practices.Bengurion
Logging and Warnings are different systems in Python, consult the docs.Madalena
I want to downvote as well. Actually the code doesn't wrong but i think fixing at echo=False is much straightforward. and it fixed.Chapbook
H
3

A more drastic solution:

import logging

logging.disable(logging.WARNING)
Hydrocele answered 23/9, 2022 at 8:31 Comment(2)
Tried everything above, this is the only thing that worked with INFO:sqlalchemy.engine.Engine logging. logging.disable(logging.INFO)Certain
This worked for me. I added it to the top of my flask's app.py file.Repast
M
-1

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.

Mountbatten answered 16/4 at 15:37 Comment(2)
it was not me who downvoted, but to let you know, this is not working for me, sqlalchemy=2.0.30Guffaw
@Guffaw This is strange, because I use it on 2.0.30. Perhaps there's a difference in environments—it works flawlessly on dockerized python:3.11-slim image.Mountbatten

© 2022 - 2024 — McMap. All rights reserved.