Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure/initialize logging using logger for multiple modules only once in Python for entire project?

Tags:

python

I have python project with multiple modules with logging. I perform initialization (reading log configuration file and creating root logger and enable/disable logging) in every module before start of logging the messages. Is it possible to perform this initialization only once in one place (like in one class may be called as Log) such that the same settings are reused by logging all over the project?

I am looking for a proper solution to have only once to read the configuration file and to only once get and configure a logger, in a class constructor, or perhaps in the initializer (__init__.py). I don't want to do this at client side (in __main__ ). I want to do this configuration only once in separate class and call this class in other modules when logging is required.

like image 881
user2301 Avatar asked Oct 18 '25 15:10

user2301


1 Answers

From the documentation,

Note that Loggers should NEVER be instantiated directly, but always through the module-level function logging.getLogger(name). Multiple calls to getLogger() with the same name will always return a reference to the same Logger object.

like image 177
MrObjectOriented Avatar answered Oct 20 '25 06:10

MrObjectOriented