Mayil-SB-Internal-Prod / loguru-II

MIT License
0 stars 0 forks source link

loguru not working with a custom class inheriting logging.Handler (hashtag1018) #75

Open vikramsubramanian opened 6 months ago

vikramsubramanian commented 6 months ago

Hi, first of all, I love using loguru.

recently I needed to add another logger to my code called Coralogix , here's the [documnetation]( and here's the [source code ]( when I try to use logger.add(coralogix_handler, level=logging.INFO, colorize=False)

I hit an exception originating from the coralogix handler Severity name is invalid!

when I debug this, I see that it happens on this line (loguru) elif hasattr(sink, "write") and callable(sink.write): leading to [this line]( (coralogix_handler)

    def __getattr__(self, severity):
        """
        Send log with some severity by it name
        :param severity: Log record severity(level)
        :type severity: str
        :return: Log sending with severity procedure wrapper
        """
        severity = severity.upper()
        if hasattr(Coralogix.Severity, severity):
            def wrapper(message, category=None, classname='', methodname='', thread_id=''):

can you please advice what should I do ? )

mayil-ai[bot] commented 6 months ago

Possible Solution

Based on the provided information, the issue seems to be related to the custom Coralogix handler not being recognized as a valid sink by Loguru due to the absence of a write method or an issue with the severity levels. To resolve the issue:

class CoralogixHandler(logging.Handler):
    # ... other methods ...

    def write(self, message):
        # Implementation for writing log messages
        pass

By ensuring that the CoralogixHandler has a write method and that all severity levels are defined and accessible, the issue should be resolved.

Code snippets to check

loguru → _logger.py This snippet contains the 'add' method definition, which is directly related to the issue as the user is trying to add a custom handler to the logger. https://github.com/Mayil-SB-Internal-Prod/loguru-II/blob/dcf42d962567723c0195bb8b2bb6fd764ecf41ae/loguru/_logger.py#L237-L1296 This snippet provides detailed documentation on how to use the 'add' method, including the parameters and their meanings, which could be relevant to understanding the issue with the custom handler. https://github.com/Mayil-SB-Internal-Prod/loguru-II/blob/dcf42d962567723c0195bb8b2bb6fd764ecf41ae/loguru/_logger.py#L253-L2094 This snippet includes the conditional check for a sink with a 'write' method, which is mentioned in the issue description as the point where the exception is triggered. https://github.com/Mayil-SB-Internal-Prod/loguru-II/blob/dcf42d962567723c0195bb8b2bb6fd764ecf41ae/loguru/_logger.py#L788-L863 This snippet shows the creation of the handler object, which is relevant because the issue may be related to how the handler is initialized or configured. https://github.com/Mayil-SB-Internal-Prod/loguru-II/blob/dcf42d962567723c0195bb8b2bb6fd764ecf41ae/loguru/_logger.py#L779-L1011