How to Log to Stdout with Python?

Better Stack Team
Updated on February 1, 2023

If you are new to logging in Python, please feel free to start with our Introduction to Python logging to get started smoothly. Otherwise, here is how to log to Stdout with Python:

Using Basic Configuration

Python, by default, logs to a console. You can call the function on the module:

import logging

logging.warning("Warning.")

OUTPUT
WARNING:root:Warning.
Copied!

Python already provides default formatting.

Using Provided Classes

You can also use the provided classes:

import logging

logger = logging.getLogger("nameOfTheLogger")
ConsoleOutputHandler = logging.StreamHandler()

logger.addHandler(ConsoleOutputHandler)

logger.warning("Warning.")
Copied!

Create a new logger and a new handler. Assign the class StreamHandler to the handler and assign the handler to the logger. The output will be the following:

Output
Warning.

If you would like to have more options when it comes to logging your apps, have a look at our Guide to logging with Loguru, which is the most popular third-party logging framework for Python.

🔭 Want to centralize and monitor your python logs?

Go to Logtail and start your log management in 5 minutes.

Better Uptime Dashboard

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

We are hiring.

Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.

Explore all positions →

Reliability is the
ultimate feature

Delightful observability tools that turn your logs & monitoring into a secret weapon for shipping better software faster.

Explore Better Stack