0

I've got a python server that was using logging module. So far it was just:

logging.basicConfig(filename='server.log',level=logging.INFO,format='%(asctime)s\t%(levelname)s\t%(message)s')

Now I want to add few handlers to the logger. This is about 15 lines of code. I don't want to put it into the main server.py file to make it hold the most important server stuff. The question is - what is the suggested way to do this in python.

I have moved my logging handlers definitions into another module and have imported it:

import logdefs

but then this import is never used (I never use logdefs.something). Is that ok? Another question is - how does it work that logging module is loaded from a server.py's submodule and all the logging settings are available for the rest of its application (modifications are global instead of local)? Is all logging module content available as some kind of a singleton?

1 Answer 1

1

Instead of calling logging.basicConfig, you could call

logging.config.fileConfig(config_file)

and place the configuration in a file.


For you second question, if you look at the logging/__init__.py source code, you'll see

root = RootLogger(WARNING)
Logger.root = root
Logger.manager = Manager(Logger.root)

This code gets called when logging is first imported. So when a submodule modifies or access the root logger, it is affecting or accessing the same root logger as the module that first imported logging.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.