Open vikramsubramanian opened 6 months ago
To capture warnings issued by the warnings.warn
function and log them using Loguru, you can use the warnings
module's showwarning
function to override the default behavior and redirect the warnings to the Loguru logger. Here's how you can do it:
import warnings
from loguru import logger
def loguru_warning(message, category, filename, lineno, file=None, line=None):
logger.warning(f"{filename}:{lineno}: {category.__name__}: {message}")
# Override the default showwarning function
warnings.showwarning = loguru_warning
# Now, when you use warnings.warn, it will be logged by Loguru
warnings.warn('This is a warning message.')
With this setup, you don't need to log the warning message twice. The warnings.warn
call will automatically be logged by Loguru.
Hi, I have some code that does warnings.warn. I wonder if there is some setting in loguru that would detect warnings.warn raised in other part of the code and record it in the log file? Thanks. Or I have to do both? Like
)