How To Use Logging In Multiple Modules?

Better Stack Team
Updated on May 4, 2022

It's recommended to have a logger defined in each module like this:

 
import logging
logger = logging.getLogger(__name__)

Then in your main program, do the following:

 
import logging.config
logging.config.fileConfig('/path/to/logging.conf')

If you want to use one logger, you can create a variable for the logging module:

 
import logging

logger = logging
logger.basicConfig()

Then in other files, you will import the logger and use it from now on:

 
from base_logger import logger

Every logger is a child of the parent's package logger. Meaning that all you need to do is configure the root logger.

đź”­ 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