Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How split sinks by behaviour? #1163

Open
mesteruh opened this issue Jun 26, 2024 · 1 comment
Open

How split sinks by behaviour? #1163

mesteruh opened this issue Jun 26, 2024 · 1 comment
Labels
question Further information is requested

Comments

@mesteruh
Copy link

mesteruh commented Jun 26, 2024

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
@Delgan
Copy link
Owner

Delgan commented Jun 29, 2024

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,
)

@Delgan Delgan added the question Further information is requested label Jun 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants