Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python logzero and stdout

logzero sends all entry logs to a file, but also to stdout. Is there a way to avoid polluting the stdout?

I want to do this in Python. I don't want to call my script and redirect its output to /dev/null, because if I do so I won't be able to print useful messages to stdout.

EDIT: As @blues pointed out logzero sends lines to stderr, not stdout.

like image 905
Federico Razzoli Avatar asked Dec 15 '25 07:12

Federico Razzoli


1 Answers

logzero does not actually send any logs to stdout, they are being sent to stderr. So you could redirect them to /dev/null without affecting your regular output.

But there is also a kwarg for logzero to not use this handler at all:

import logzero

logger = logzero.setup_default_logger(disableStderrLogger=True)
logzero.logfile('zero.log')
logger.info('appears in file but not on terminal')

I would however recommend instead to just use the native python logging module, as it is actually easier to setup for just filelogging:

import logging

logging.basicConfig(filename='logging.log', level='DEBUG')
logging.info('will be logged to file')
like image 114
blues Avatar answered Dec 16 '25 23:12

blues



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!