Skip to content

Add support of loguru logger. #653

Closed

Description

@heckad

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

Activity

  1. untitaker commented on Mar 24, 2020

    @untitaker
    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

  2. jtfidje commented on Mar 2, 2021

    @jtfidje

    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)
  3. sandrovw64 commented on Aug 16, 2021

    @sandrovw64

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

  4. fratambot commented on Sep 15, 2021

    @fratambot

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

  5. jtfidje commented on Sep 16, 2021

    @jtfidje

    @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

  6. github-actions commented on Dec 23, 2021

    @github-actions
    Contributor

    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 🥀

  7. kbakk commented on Jan 16, 2022

    @kbakk

    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.

  8. antonpirker commented on Feb 24, 2022

    @antonpirker
    Contributor

    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)

  9. 1 remaining item

  10. jtfidje commented on Mar 2, 2022

    @jtfidje

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

    Swag sounds very nice! O:-)

  11. antonpirker commented on Mar 7, 2022

    @antonpirker
    Contributor

    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?

  12. Delgan commented on Mar 7, 2022

    @Delgan

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

  13. added this to the milestone on Mar 21, 2022
  14. antonpirker commented on Mar 25, 2022

    @antonpirker
    Contributor

    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.

  15. github-actions commented on Apr 16, 2022

    @github-actions
    Contributor

    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 🥀

  16. Delgan commented on Apr 23, 2022

    @Delgan

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

  17. antonpirker commented on Apr 25, 2022

    @antonpirker
    Contributor

    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)

  18. SonGokussj4 commented on Feb 9, 2023

    @SonGokussj4

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

  19. added a commit that references this issue on May 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions