Proper way to intercept stdlib logging
See original GitHub issueThe method to intercept stdlib logging in the readme sometimes gives wrong originator of the log message. Currenty in the readme it is recommended to set depth=6:
class InterceptHandler(logging.Handler):
def emit(self, record):
# Retrieve context where the logging call occurred, this happens to be in the 6th frame upward
logger_opt = logger.opt(depth=6, exception=record.exc_info)
logger_opt.log(record.levelno, record.getMessage())
But this fails for simple program:
import logging
logging.basicConfig(level='INFO')
logging.getLogger(None).addHandler( InterceptHandler())
logging.warning('123')
The originator of the message is wrongly displayed as logging:warning:1979
:
2019-04-02 14:24:04.558 | WARNING | logging:warning:1979 - 123
Similar issue: #69
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
Standard Library Logging - structlog 22.3.0 documentation
The quickest way to get started with structlog and logging is structlog.stdlib.recreate_defaults() . It will recreate the default configuration on top of ...
Read more >Python logging - best way to intercept ERROR and CRITICAL ...
Can anyone suggest the best way to intercept (that is, perform some custom functionality first, then perform the normal log method) for code ......
Read more >Logging Cookbook — Python 3.11.1 documentation
An easy way in which you can pass contextual information to be output along with logging event information is to use the LoggerAdapter...
Read more >Python Logging Functionality - Facts vs. Myths - Plumber Jack
You can do exactly this with stdlib logging, where from the earliest release there's been a MemoryHandler class which buffers up records in ......
Read more >go-hclog - Go Packages
That way, a major subsystem can use this to decorate all it's own logs // without losing context. NamedIntercept(name string) InterceptLogger // Create...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If you are using Sentry and seeing
sentry_sdk.integrations.logging:sentry_patched_callhandlers:83
as the caller in your logs, just change thewhile
line above to:This is needed even if you haven’t explicitly enabled Sentry’s logging integration, because Sentry enables it by default.
I updated the
InterceptHandler()
snippet in the Readme:It should now retrieve the correct frame information and level name. This is not very elegant, but this is the best I could do. See also my comment: https://github.com/Delgan/loguru/issues/154#issuecomment-548761213