Logging handler logic

8 views
Skip to first unread message

jca...@gmail.com

unread,
Jun 13, 2021, 1:14:03 PM6/13/21
to sqlalchemy
Hello everyone,
After reviewing the handler logic, I am not clear that it implements logging
within the pattern guidelines defined by the base logging implementation.

In log.py#L103, the level and existence of at least one handler are checked.

I only have a handler added to a root logger that I don't control, and we rely
on ancestral propagation.

The concern I have is due to any IO emitted to the console streams breaks
the calling code that I don't control. With this pattern, I have to preemptively
add null handlers to every logger, and if I miss one and it manifests in production,
I'll have a problem.

Anyone have any thoughts around this?

Thanks,
jlc


Mike Bayer

unread,
Jun 13, 2021, 3:06:11 PM6/13/21
to noreply-spamdigest via sqlalchemy
the line you refer towards involves a flag called "echo", which is described at https://docs.sqlalchemy.org/en/14/core/engines.html#more-on-the-echo-flag and is entirely optional.  if you don't use the echo flag, SQLAlchemy's logging behavior is completely traditional, with the caveat that the Engine, Connection and Pool objects will check the level of loggers before opting to send out logging.info() and logging.debug() calls as there is some additional formatting expense for some of these calls.

I'm not familiar with what you mean by "IO emitted to the console streams breaks calling code", SQLAlchemy doesn't emit any IO that it isn't instructed to nor does it set up any handlers if you aren't using the "echo" flag.
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.

jca...@gmail.com

unread,
Jun 13, 2021, 10:24:40 PM6/13/21
to sqlalchemy
Hi,
Thank you for the response. The line I mentioned calls _add_default_handler if it evaluates to true which attaches a stream handler set to sys.stdout.

That's the crux of the concern, it checks its own logging namespace that in most cases won't have a handler so it most likely (but not always) will add a console stream handler all the time.

I do want to use echo as I want the query and bind parameters on occasion, I just don't think the stream handler is appropriate.

The docs mentioned that you should adjust 'sqlalchemy.engine', however that's not the namespace that gets a console stream handler, it is actually 'sqlalchemy.engine.Engine'. After seeing this discrepancy, I was concerned about what I may overlook and end up finding out the hard way.

Thanks,
jlc

Mike Bayer

unread,
Jun 14, 2021, 7:33:41 AM6/14/21
to noreply-spamdigest via sqlalchemy
the echo flag doesn't support customization like this.   if you want to use your own handlers then you should use traditional logging steps; there's nothing the echo flag does that can't be accomplished using the logging API directly.

jca...@gmail.com

unread,
Jun 14, 2021, 9:38:50 AM6/14/21
to sqlalchemy
Ok,
Thanks for the patience, I should have looked at this closer. This now has reliable behavior for my use case by not using the echo flag.

Thank you very much,
jlc

Reply all
Reply to author
Forward
0 new messages