Mayil-AI-Sandbox / loguru-Jan2023

MIT License
0 stars 0 forks source link

Loguru suppresses an instance of logger from `logging.getLogger()` (hashtag1141) #165

Open vikramsubramanian opened 2 months ago

vikramsubramanian commented 2 months ago

Hi, I would to begin by thanking you for the amount of work you've put into making me not want to paint the walls in red whenever I decide to perform any form of logging this tremendous library.

I've recently encountered an issue which I'm sure is happening by design and the answer to which I have unfortunately not been able to find in the past issues.

I'm currently using a library ([sentence-transformers]( if it's of any consequence), which happens to be using the default logger from logging library. In a script where I'm making use of this library I'm also logging my own events which is handled by loguru. However, it appears that whenever an event is logged in the external library (see [this file]( for reference), the log is suppressed by loguru and nothing is actually written to stdout.

Is there anyway I can circumvent this without having to alter the external library?

Thanks in advance! )

mayil-ai[bot] commented 2 months ago

Some past issues to consider:

Here are some code snippets that may help you get started:

To ensure that Loguru does not suppress logs from the standard logging module, follow these steps:

  1. Remove Loguru's Default Handler and Add a New One:

    import sys
    from loguru import logger
    
    # Remove the default handler
    logger.remove()
    
    # Add a new handler that writes to sys.stderr
    logger.add(lambda msg: sys.stderr.write(msg), colorize=True)
  2. Intercept and Redirect Standard Logging to Loguru:

    import logging
    from loguru import logger
    import sys
    
    class InterceptHandler(logging.Handler):
       def emit(self, record):
           try:
               level = logger.level(record.levelname).name
           except ValueError:
               level = record.levelno
    
           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())
    
    # Configure the standard logging to use the InterceptHandler
    logging.basicConfig(handlers=[InterceptHandler()], level=0)
    
    # Configure Loguru to log to sys.stderr
    format_ = '<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | <level>{level:}</level> | <cyan>{file}</cyan>:<cyan>{function}</cyan>:<cyan>[line:{line}]</cyan>  - <level>{message}</level>'
    logger.remove(None)
    logger.add(sys.stderr, format=format_, level="DEBUG")
    
    # Example usage
    import sentence_transformers
    # Your code that uses sentence-transformers

By implementing these changes, Loguru will no longer suppress logs from the standard logging module, and both Loguru and standard logging will work harmoniously.

💡 To rerun Mayil, comment mayil-ai rerun. Mayil will incorporate any new context added to the ticket. Include details in your rerun comment to guide Mayil!