I have the following application structure:
./utils.py
def do_something(logger=None):
if not logger:
logger = logging.getLogger(__name__)
print(
Variable logger being a global, can be accessed from inside do_something() function like this:
logger = logging.getLogger(__name__)
def do_something():
x = logger
After reading this carefully:
What's the common solution for this type of recipe, when one function is used all over the code base but the loggers should be of those calling the function?
In simple english would be:
How to access global variable
loggerfrom imported functiondo_something()?
I conclude that there is no other way!
You have to pass logger as an argument for this particular case.
from utils import do_something
logger = logging.getLogger(__name__)
do_something() # logger global is not visible in do_something()
do_something(logger=logger) # the only way