loguru
loguru copied to clipboard
How split sinks by behaviour?
I have 2 sinks. When i use logger.exception('bla bla') its show traceback in both sinks. Why so? I wanna that exceptions messages go to error sink, and all other to info sink
from loguru import logger
import sys
format_string = "{time} {level} {message}"
logger.add(
sink=sys.stdout,
level="INFO",
format=format_string,
colorize=True,
backtrace=False,
enqueue=False,
)
logger.add(
sink=sys.stderr,
level="ERROR",
format=format_string,
colorize=True,
backtrace=True,
enqueue=False
)
try:
2 / 0
except Exception:
logger.exception("axixa")
its raise exception to both sinks
2024-06-26 22:21:43.599 | ERROR | __main__:<module>:27 - axixa
Traceback (most recent call last):
> File "/Users/rkuramagomedov/sin.py", line 25, in <module>
2 / 0
ZeroDivisionError: division by zero
2024-06-26T22:21:43.599632+0300 ERROR axixa
Traceback (most recent call last):
File "/Users/rkuramagomedov/sin.py", line 25, in <module>
2 / 0
ZeroDivisionError: division by zero
2024-06-26T22:21:43.599632+0300 ERROR axixa
Traceback (most recent call last):
> File "/Users/rkuramagomedov/sin.py", line 25, in <module>
2 / 0
ZeroDivisionError: division by zero
The level parameter is a minimum threshold. This is the conventional and recommended way of doing logging: you can adapts the logged details according to your needs during development or in production.
If you want only logs from a specific level, you need to use a filter:
logger.add(
sink=sys.stdout,
level="INFO",
filter=lambda record: record["level"].name == "INFO",
format=format_string,
colorize=True,
backtrace=False,
enqueue=False,
)