Open vikramsubramanian opened 2 months ago
contextlib.redirect_stderr does not work for loguru (hashtag764) | Highlights handling of Loguru's default handler, which may relate to the suppression issue.
how to config for printing requests DEBUG log (hashtag629) | Discusses intercepting logs from standard logging, similar to the current issue with external library logs.
To ensure that Loguru does not suppress logs from the standard logging
module, follow these steps:
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)
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!
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 loggingthis 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 byloguru
. 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! )