Python:
Writing to standard error
How to:
Using sys.stderr
Python’s built-in sys
module allows explicit writing to stderr
. This approach is straightforward for simple error messages or diagnostics.
import sys
sys.stderr.write('Error: Something went wrong.\n')
Sample output (to stderr):
Error: Something went wrong.
Using the print
function
Python’s print
function can redirect its output to stderr
by specifying the file
parameter. This method is useful for leveraging print
’s user-friendliness while handling error messages.
from sys import stderr
print('Error: Failure in module.', file=stderr)
Sample output (to stderr):
Error: Failure in module.
Using the logging
module
For a more comprehensive solution, Python’s logging
module can direct messages to stderr
and much more, such as writing to a file or customizing message format. This method is best for applications requiring varying levels of logging, message formatting, or destinations.
import logging
logging.basicConfig(level=logging.WARNING)
logger = logging.getLogger(__name__)
logger.error('Error: Database connection failed.')
Sample output (to stderr):
ERROR:__main__:Error: Database connection failed.
Third-party libraries: loguru
loguru
is a popular third-party library that simplifies logging in Python applications. It automatically directs errors to stderr
, among other features.
To use loguru
, first install it via pip:
pip install loguru
Then, incorporate it into your Python script as follows:
from loguru import logger
logger.error('Error: Failed to open file.')
Sample output (to stderr):
2023-04-05 12:00:00.000 | ERROR | __main__:<module>:6 - Error: Failed to open file.