-1

I want to initialise the logging config using a config file(json or yaml) only once when I call my main module. Is there a concept of context in python like we have in Java where I can take the logger from config whenever I need.

Something like this in the main module -

logging.config.fileConfig('log-conf.json') 

I want to use the loaded config in my entire application without having to load the config in each module.

Also, should I do log = logging.getLogger(__name__) at module level or method level. What is the advantage of doing at method level.

Gagan
  • 1
  • 3
  • Did you read e.g. https://docs.python.org/3/howto/logging.html#configuring-logging? – jonrsharpe May 29 '20 at 07:48
  • @jonrsharpe : I did read that. I am new to Python so I am bit confused as to how and when to load the logging config. Whether to create a class with `init_logging()` and `get_logger()` methods and call `init_logging()` only in the main module. – Gagan May 29 '20 at 08:11

1 Answers1

-1

This blog post of mine contains major answers to your question (YAML).

http://glenfant.github.io/the-zen-of-logging-and-yaml.html

You might also have inspiration from this recipe, if you prefer a pure Python config file.

http://glenfant.github.io/simple-customizable-configuration.html

glenfant
  • 1,298
  • 8
  • 9
  • Please see https://meta.stackexchange.com/q/225370/248731. Also, given that the other castle in this case is your blog, see https://stackoverflow.com/help/promotion. – jonrsharpe May 29 '20 at 15:34
  • Sorry, but I'm not a rockstar and I don't get paid by clicks on my blog. It's that copy / paste / reformat that blog post is just a waste of time IMO. – glenfant May 30 '20 at 11:29