0

I have a class that creates a self.logger (self.logger = logging.getLogger(str(uuid.uuid4()))) with a unique id in order to avoid my loggers to be mixed when multiple API calls are creating class instances at the same time. However, I have another file with my utility functions that do not belong to the class, and those functions are supposed to produce logs too. My question is : how to I make those functions use the same logger without having to give them the id of the main logger as an argument ? Is there a way to tell a logger created in a function to use the parent logger ?

In my utility file, since I don't want to pass the logger as argument to all the functions, I simply do a logging.info to create new logs, but it is using the root logger.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Carbs
  • 1
  • 1
  • These answers might answer your question: https://stackoverflow.com/questions/4150148/logging-hierarchy-vs-root-logger – Codesidian Dec 09 '22 at 14:43
  • Thank you for the tip but it does not do what I want. I don't have access to the parent logger name, so I can't give it to the functions of the other modules (except by adding an argument, which I want to avoid). – Carbs Dec 12 '22 at 09:07
  • The point being that they're all apart of the same hierarchy. You'll be better off having your root logger set up with contextual information for each log entry. You can find information specifically for that in the docs: https://docs.python.org/3/howto/logging-cookbook.html#adding-contextual-information-to-your-logging-output – Codesidian Dec 13 '22 at 10:35

0 Answers0