Does Loguru have built-in support for running code before/after logging a message?
See original GitHub issueDoes Loguru have built-in support for running code before/after logging a message? What I’m trying to do is get loguru.logger.warning
to also emit warnings.warn
when called.
The use case is that I have a lot of calls to loguru.logger.warning
throughout the project. I’ve now been asked to update things so that the exit code is non-zero if a warning is emitted. For example:
def cli():
exit_code = 0 # success
with warnings.capture_warnings(record=True) as w:
try:
main.run()
except Exception:
exit_code = 1 # error
if len(w) > 0:
exit_code = 3 # success-with-warnings
sys.exit(exit_code)
What I could do is go find all instances of logger.warning(msg)
and add a line warnings.warn(msg)
afterwards, but it seems like there’s gotta be a better way.
If there’s nothing built-in, I’ll end up doing something like:
# -*- coding: utf-8 -*-
# __init__.py
import warnings
# Normally I'd just have from `loguru import logger` here
from loguru._logger import Logger as _Logger
from loguru._logger import Core as _Core
# Monkeypatch the loguru logger so that logger.warning also emits warning.warn
class CustomLogger(_Logger):
def warning(__self, __message, *args, **kwargs):
r"""Log ``message.format(*args, **kwargs)`` with severity ``'WARNING'``."""
__self._log("WARNING", None, False, __self._options, __message, args, kwargs)
warnings.warn(__message)
# basically the same as loguru/__init__.py
logger = CustomLogger(
core=_Core(),
exception=None,
depth=0,
record=False,
lazy=False,
colors=False,
raw=False,
capture=True,
patcher=None,
extra={},
)
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Logger - loguru documentation - Read the Docs
Once the logger is imported, it can be used to write messages about events happening in your code. By reading the output logs...
Read more >A quick guide to using Loguru - Medium
Logging is essential in the field of software development. It helps programmers and system administrators understand the state of an ...
Read more >A Complete Guide to Logging in Python with Loguru
Learn how to install, configure, and use the Loguru framework for logging in Python applications.
Read more >loguru Documentation - Read the Docs
Logger right after import as it comes pre-configured (logs are emitted to sys.stderr by default). Messages can be logged with different severity ...
Read more >Logging Made Easy With Loguru - Florian Dahlitz
The code used in this article can be found on GitHub. It was written for Python 3.9 (CPython). Get Started With Loguru¶. To...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m closing this issue now that I added the
warnings.warn()
example to the documentation. 👍Yeah, I think sinks make a good solution for this purpose. Unless there are use cases for which they are not enough, I prefer not to add a new method to the
logger
. Maybe it’s a bit indirect and tricky, but I think I’ll just add this snippet in the documentation. 👍