Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging in a Python project with multiple points of entry

Tags:

python

logging

In a multi-module Python environment, the docs recommend that one run the following snippet in the first point of entry into the Python project to define project-wide logging.

# ABC.py
import logging

logging.basicConfig(filename='master.log', level=logging.INFO)
logging.info('Start Logging.')

If some other file is another potential first point of entry into the project, do I add the same snippet to the top of that file as well? What I am trying to achieve is to specify logging in a single central location regardless of which file is the first point of entry.

like image 591
Spinor8 Avatar asked Sep 17 '25 18:09

Spinor8


1 Answers

You could create a dedicated python script file just for setting up the logging. Basically, that file would contain exactly what your ABC.py file contains now.

Then just import this file in every script that's a potential "entry point". Even if the file is imported twice, the code in the file will be run exactly once.

like image 134
tobias_k Avatar answered Sep 19 '25 10:09

tobias_k