question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proper way to intercept stdlib logging

See original GitHub issue

The 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:closed
  • Created 4 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
matthewlloydcommented, Jul 21, 2020

If you are using Sentry and seeing sentry_sdk.integrations.logging:sentry_patched_callhandlers:83 as the caller in your logs, just change the while line above to:

import sentry_sdk.integrations.logging

# ...

        while frame.f_code.co_filename in (logging.__file__, sentry_sdk.integrations.logging.__file__):

This is needed even if you haven’t explicitly enabled Sentry’s logging integration, because Sentry enables it by default.

1reaction
Delgancommented, Nov 9, 2019

I updated the InterceptHandler() snippet in the Readme:

class InterceptHandler(logging.Handler):
    def emit(self, record):
        # Get corresponding Loguru level if it exists
        try:
            level = logger.level(record.levelname).name
        except ValueError:
            level = record.levelno

        # Find caller from where originated the logged message
        frame, depth = logging.currentframe(), 2
        while frame.f_code.co_filename == logging.__file__:
            frame = frame.f_back
            depth += 1

        logger.opt(depth=depth, exception=record.exc_info).log(level, record.getMessage())

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found