Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Python logging write to stdout or stderr by default?

Tags:

python

The logging docs don't mention what the default logger obtained from basicConfig writes to: stdout or stderr.

What is the default behavior?

like image 899
bluenote10 Avatar asked Jun 07 '19 14:06

bluenote10


People also ask

Does Python logging go to stdout?

logging - Making Python loggers output all messages to stdout in addition to log file - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.

Does Python logging go to stderr?

Python Logging Best Practices If you want to catch error messages from libraries you use, make sure to configure the root logger to write to a file, for example, to make the debugging easier. By default, the root logger only outputs to stderr , so the log can get lost easily.

Should logs go to stderr or stdout?

Only error logs should go to stderr. This is a pretty common convention supported by the 12factor design principles and the original intent behind io streams in operating systems. With all logs dumped to stderr, it's significantly more difficult to sift through output from using pipes.

What is the default logging level in Python?

The default level is WARNING , which means that only events of this level and above will be tracked, unless the logging package is configured to do otherwise. Events that are tracked can be handled in different ways. The simplest way of handling tracked events is to print them to the console.


1 Answers

Apparently the default is stderr.

A quick check: Using a minimal example

import logging
logger = logging.getLogger(__name__)

logging.basicConfig(level=logging.INFO)
logger.info("test")

and running it with python test.py 1> /tmp/log_stdout 2> /tmp/log_stderr results in an empty stdout file, but a non-empty stderr file.

like image 119
bluenote10 Avatar answered Oct 03 '22 05:10

bluenote10