-
Notifications
You must be signed in to change notification settings - Fork 719
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Monitoring and centralized logging integration strategy #344
Comments
For Sentry integration I came up to such code: import sys
from loguru import logger
import logging
from sentry_sdk.integrations.logging import LoggingIntegration, EventHandler
SENTRY_DSN = '...'
logger.remove()
sentry_sdk.init(
SENTRY_DSN,
integrations=[LoggingIntegration(level=None, event_level=None)],
)
logger.add(sys.stderr, level='INFO')
logger.add(EventHandler(level=logging.WARNING))
# + InterceptHandler from README.md
# ...
logging.basicConfig(handlers=[InterceptHandler()], level=0) Will test how it works out. |
Hi @rudyryk. As you figured out, I think simply adding a Sentry Combining this with the Please, let me know if this does not work as expected. |
@Delgan unfortunately, I faced weird issue, I got endless messages sent to the Sentry server:
An example of message received:
Seems like some cyclic logging behaviour. |
Sorry, I'm not a Sentry user and consequently I'm not able to reproduce your problem. :( Would this be possible that events emitted by the |
My guess is that's what happening exactly, but I didn't have chance to deep dive. Sentry is a major player in monitoring and crashes tracking in Python world, I think it's crucial to have solid workflow to make them play well with each other. |
Indeed, it is very important that If you have any idea how to create a minimum reproducible example of the issue you're facing, please let me know. I have some doubts about the use of |
@Delgan Yes, it's possible that some other hooks are also involved, I'll try to provide basic reproducible example. |
@rudyryk Have you found a solution? |
Hi @Soures888. Did you encounter the same issue? Are you able to create a minimal reproducible example? |
Hi guys! Just use this solution: |
Hi, I'm seeing some issues with using Sentry and Loguru. Here's a reproducer: t.pyimport logging
import os
import sys
from loguru import logger
import sentry_sdk
class InterceptHandler(logging.Handler):
def emit(self, record):
# Get corresponding Loguru level if it exists
try:
level = logger.level(record.levelname).name
except (ValueError, TypeError):
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()
)
logging.basicConfig(handlers=[InterceptHandler()], level=0)
level_per_module = {"": "DEBUG", "sentry_sdk": "INFO"}
logger.remove()
logger.add(
sys.stderr,
filter=level_per_module,
diagnose=False,
colorize=True,
format="{level: <8} | {name}:{line} ({module}) | <level>{message}</level>",
level="DEBUG",
)
sentry_sdk.init(
os.getenv("SENTRY_SDK")
)
std_logger = logging.getLogger("std_logger")
std_logger.info("Test info msg")
std_logger.debug("Test debug msg - not shown")
logger.debug("Test msg") $ python t.py
INFO | sentry_sdk.integrations.logging:86 (logging) | Test info msg
DEBUG | __main__:45 (t) | Test msg If I comment out the $ python t.py
INFO | __main__:42 (t) | Test info msg
DEBUG | __main__:43 (t) | Test debug msg - not shown
DEBUG | __main__:45 (t) | Test msg It looks like the |
Playing around a bit, I tried changing # Find caller from where originated the logged message
frame, depth = logging.currentframe(), 2
- while frame.f_code.co_filename == logging.__file__:
+ while (
+ frame.f_code.co_filename == logging.__file__
+ or "sentry_sdk/integrations" in frame.f_code.co_filename
+ ):
frame = frame.f_back
depth += 1 It seems to have the desired effect: $ python t.py
INFO | __main__:46 (t) | Test info msg
DEBUG | __main__:47 (t) | Test debug msg - not shown
DEBUG | __main__:49 (t) | Test msg This might introduce some other problems, so not sure I can recommend this. |
The above is similar to what @fratambot writes in #509 |
Hey @kbakk, in the end I managed to integrate nicely loguru and sentry. |
Hey @kbakk. Thanks for the reproducible example. This seems definitely related to how I figured out you could configure Sentry with Hope it might help. |
It seems Sentry now officially supports Loguru: Learn about using Sentry with Loguru. Consequently, I'm closing this ticket. The integration should be quite straightforward. |
I'm trying switching to Loguru for my newer projects and I'm wondering, what is recommended approach to perform integrations with services like Sentry, etc.?
For example, Sentry has built-in standard logging library hooks. But it won't work with the standard logging intercepted by Loguru.
Should we create custom sink? That could be tricky, and actually, could introduce extra bugs, so it needs to have solid design and bunch of tests. Application broken because of bugs monitoring a is kind of "fun but not fun".
The text was updated successfully, but these errors were encountered: