question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

stderr from error and above

See original GitHub issue

Hi,

First of all I would like to congratulate for this awesome library, made my logging experience with Python really simple.

I am not sure if I am using the correct way to setup a logger, what I would like to do is to log to stderr from error level above (error, critical, etc).

My current implementation logs error messages to stdout:

def setup_logger():
    logger.remove()
    logger.add('/tmp/ssh.log', format="{time:YYYY-MM-DD HH:mm:ss} | {level} | {message}", level='DEBUG',
               enqueue=True, retention="14 days")
    logger.add(sys.stdout, colorize=True, format="{time:YYYY-MM-DD HH:mm:ss} | {level} | {message}", level='INFO',
               enqueue=True)

Is there a way do to this?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Delgancommented, Jun 26, 2022

Hi @PyAntony.

Defining the level of an handler as the “minimum threshold” for messages to be emitted is a very popular principle among logging libraries. You can look at it this way: by default we don’t want to display debug messages to the user, thus we set the minimal level to “INFO”. However, when something goes wrong, debug messages exist to give more detailed information next to the error messages. If both message are separated in two files, context is lost which makes investigating bugs more challenging. It makes little sense to have the fine grained details without a global view of what is happening.

That’s why in my opinion new arguments like upper_level or levels_allowed should not be added to the add() method. I don’t think this is a good practice when using Loguru.

If you need multiple logs to appear in multiple files (for example), it’s advised to use bind() with a custom filter sink function.

1reaction
Delgancommented, Apr 5, 2021

Indeed. So you want "ERROR" and below to stdout, but "ERROR" and above to stderr?

In this case you may use a function to filter logs emitted to stdout:

logger.add(sys.stdout, filter=lambda record: record["level"].no < 40, level="INFO", ...)
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to redirect standard (stderr) error in bash - nixCraft
Explains how to redirect standard error in bash shell on Linux, macOS, BSD & Unix . You can also redirect both stdout &...
Read more >
Write Error and above to stderr and Verbose and above to stdout
How to set the configuration in NLog to log errors and above to stderr and log verbose and above to stdout ? //...
Read more >
Functions of Stderr in C with Different Examples - eduCBA
Stderr is the standard error message that is used to print the output on the screen or windows terminal. Stderr is used to...
Read more >
Standard error - Wikipedia
The standard error (SE) of a statistic is the standard deviation of its sampling distribution or an estimate of that standard deviation.
Read more >
Writing Error Messages to Standard Error Instead ... - Learn Rust
macro. In most terminals, there are two kinds of output: standard output ( stdout ) for general information and standard error ( stderr...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found