What is the idiomatic way to share a log instance between modules in my Python script?

I created my own simple log class that uses COM interoperability to redirect to the company's standard logger. I would like to instantiate a registrar at the beginning of my script, and then use this registrar to allow all modules in my script to register centrally.

Is there an idiomatic way to share this instance of the logger between all modules without specifically adding the logger parameter to the constructor of each class that needs to be registered?

Should I use a global variable or singleton, or is there another recommended way to access the log between modules?

+4
source share
2 answers

Why don't you use standard Python logging and write a handler to interact with your company’s logging system? Writing in Python is specially designed, so you don’t need to pass registrar instances between layers of your code. Python logs are already singles already.

Stdlib logging handlers are fairly easy to write β€” there are many examples of third-party handlers.

Disclosure: I am the custodian of the Python standard library registration package.

+5
source

I would use a module instead of a class, since the python module is already singleton.

A few approaches here .

+1
source

Source: https://habr.com/ru/post/1343644/


All Articles