sentry-python
sentry-python copied to clipboard
Add support of loguru logger.
Please add support for loguru. I can make pull request but I don't know how to correctly integrate structure loggers.
https://github.com/getsentry/sentry-python/issues/228 is very relevant for this, it's possible you will run into similar issues here. We are happy to accept and merge PRs for those things, generally speaking
Hi! I just wanted to chime in with my solution here. I solved this by following advice given in #228 as @untitaker pointed to :) ( Thanks @untitaker ! )
Basically, all I did was this:
from loguru import logger
from sentry_sdk.integrations.logging import (
LoggingIntegration,
BreadcrumbHandler,
EventHandler,
)
.
.
.
if settings.sentry_dsn:
_ = logger.add(
BreadcrumbHandler(level=logging.DEBUG),
diagnose=settings.loguru_diagnose,
level=logging.DEBUG,
)
_ = logger.add(
EventHandler(level=logging.ERROR),
diagnose=settings.loguru_diagnose,
level=logging.ERROR,
)
integrations = [
LoggingIntegration(level=None, event_level=None),
]
I also work with a lib where I don't have much control over their use of the built-in logger, and so I have this snippet as well to intercept the built-in logger and route everything through loguru ( from the excellent loguru docs! ):
class InterceptHandler(logging.Handler):
"""Intercepts builtin logging messages and routes them to Loguru"""
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()
)
# Enable interceptor
logging.basicConfig(handlers=[InterceptHandler()], level=0)
Hello guys, we been using loguru for a while and I believe it would be a good option to integrate with Sentry
@jtfidje where the settings
are coming from in your example ?
@jtfidje where the
settings
are coming from in your example ?
settings is an instance of a Settings-class that inherits from pydantic.BaseSettings
:
from pydantic import BaseSettings
class Settings(BaseSettings):
"""My settings!"""
.
.
.
I use this a lot to configure applications
This issue has gone three weeks without activity. In another week, I will close it.
But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog
or Status: In Progress
, I will leave it alone ... forever!
"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀
I also work with a lib where I don't have much control over their use of the built-in logger, and so I have this snippet as well to intercept the built-in logger and route everything through loguru ( from the excellent loguru docs! ):
@jtfidje I'm seeing some issues with what I believe how InterceptHandler is working - I posted a description here (https://github.com/Delgan/loguru/issues/344#issuecomment-1013900054). In short, I'm seeing sentry_sdk.integrations.logging:86 (logging) | Test info msg
insted of __main__:42 (t) | Test info msg
.
Hey guys!
Sorry for not getting back to you. This is amazing work so far:
- So we have two PR on logurus Repo by @TheZavitaev and @Delgan
- And we have code here in this thread from @jtfidje
- And there is also discussions about JSON logs that are related over here: https://github.com/getsentry/sentry-python/issues/228
My question now is, how do we get this into a PR of the sentry-python repo? If we have a PR I am happy to test it and help bringing it into a shape we can merge.
We do hat a checklist on how an Integration could/should look like: https://github.com/getsentry/sentry-python/blob/master/CONTRIBUTING.md#adding-a-new-integration-checklist
And also one could use the logging
integration as kind of a template:
https://github.com/getsentry/sentry-python/blob/master/sentry_sdk/integrations/logging.py
So anyone motivated? (I think if we pull this off, I can organize Sentry swag for everyone)
@antonpirker Sounds fun! I'll try to help where I can :)
Swag sounds very nice! O:-)
Hey @jtfidje ! Great to here you are in to help!
Could you look at all the information we have in this thread and see if the PRs and your code here can be merged into one thing that addresses all the problems?
As the Loguru developer, I would like Loguru and Sentry to be seamlessly compatible! :+1:
Basically the InterceptHandler
is supposed to be attached to some standard logger
object (possibly using logging.basicConfig()
). It works fine most of the time, but there are some difficulties with Sentry. As discussed, it requires playing with default_integrations=False
, BreadcrumbHandler
and EventHandler
which is not very intuitive. I don't know how it could be improved, but let me know if you need details about logging integration with Loguru. ;)
Hey @Delgan!
Thanks for the info and the offer to help.
So you already created an sort of Integration for Loguru with Sentry? Is this anything that you could put into a pull request?
I have put this issue on the low prio internal backlog and also into a milestone. I don't yet know when we (Sentry) will have time to help because we are currently packed with other stuff.
This issue has gone three weeks without activity. In another week, I will close it.
But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog
or Status: In Progress
, I will leave it alone ... forever!
"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀
Hey @antonpirker, sorry for the late answer. Time flies...
Honestly I would not know which PR to open. I'm not a Sentry user myself, so I'm not in the best position to know how to improve integration with Loguru. I've seen that for now, it requires touching different Sentry configuration (default_integrations=False
, BreadcrumbHandler
and EventHandler
) but I do not know the logic behind it.
I'm still willing to help if anyone have interest in investigating this. :+1:
Ok, thanks for the feedback. I will put it on the internal backlog in low prio (so it will take some time until we get a chance to look at this)
Hi, any update? We just deployed sentry in our project and thinking about using loguru as logger.