Skip to content
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

Add support of loguru logger. #653

Closed
heckad opened this issue Mar 21, 2020 · 16 comments · Fixed by #1994
Closed

Add support of loguru logger. #653

heckad opened this issue Mar 21, 2020 · 16 comments · Fixed by #1994
Labels
Integration: Logging New Integration Integrating with a new framework or library

Comments

@heckad
Copy link

heckad commented Mar 21, 2020

Please add support for loguru. I can make pull request but I don't know how to correctly integrate structure loggers.

@untitaker
Copy link
Member

#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

@jtfidje
Copy link

jtfidje commented Mar 2, 2021

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)

@sandrovw64
Copy link

Hello guys, we been using loguru for a while and I believe it would be a good option to integrate with Sentry

@fratambot
Copy link

@jtfidje where the settings are coming from in your example ?

@jtfidje
Copy link

jtfidje commented Sep 16, 2021

@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

@github-actions
Copy link

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 🥀

@kbakk
Copy link

kbakk commented Jan 16, 2022

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 (Delgan/loguru#344 (comment)). In short, I'm seeing sentry_sdk.integrations.logging:86 (logging) | Test info msg insted of __main__:42 (t) | Test info msg.

@sl0thentr0py sl0thentr0py reopened this Jan 21, 2022
@antonpirker antonpirker added the New Integration Integrating with a new framework or library label Feb 24, 2022
@antonpirker
Copy link
Member

Hey guys!

Sorry for not getting back to you. This is amazing work so far:

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)

@jtfidje
Copy link

jtfidje commented Mar 2, 2022

@antonpirker Sounds fun! I'll try to help where I can :)

Swag sounds very nice! O:-)

@antonpirker
Copy link
Member

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?

@Delgan
Copy link

Delgan commented Mar 7, 2022

As the Loguru developer, I would like Loguru and Sentry to be seamlessly compatible! 👍

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. ;)

@antonpirker antonpirker added this to the Better Support for Structured Log Messages milestone Mar 21, 2022
@antonpirker
Copy link
Member

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.

@github-actions
Copy link

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 🥀

@Delgan
Copy link

Delgan commented Apr 23, 2022

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. 👍

@antonpirker
Copy link
Member

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)

@SonGokussj4
Copy link

Hi, any update? We just deployed sentry in our project and thinking about using loguru as logger.

antonpirker added a commit that referenced this issue May 9, 2023
* Add `loguru` integration

Actually, this is the solution in comments under #653 adapted to
codebase and tested as well.
#653 (comment)

I also changed `logging` integration to use methods instead of
functions in handlers, as in that way we can easily overwrite parts
that are different in `loguru` integration. It shouldn't be a problem,
as those methods are private and used only in that file.

---------

Co-authored-by: Anton Pirker <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Integration: Logging New Integration Integrating with a new framework or library
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants