Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why both, import logging and import logging.config are needed?

Should not it be handled by a single import? i.e. import logging. If I do not include import logging.config in my script, it gives :

AttributeError: 'module' object has no attribute 'config'

like image 868
Shefali Avatar asked Feb 10 '10 06:02

Shefali


People also ask

Why we use import logging in Python?

It is used by most of the third-party Python libraries, so you can integrate your log messages with the ones from those libraries to produce a homogeneous log for your application. With the logging module imported, you can use something called a “logger” to log messages that you want to see.

What is logging config in Python?

The logging configuration functionality tries to offer convenience, and in part this is done by offering the ability to convert text in configuration files into Python objects used in logging configuration - for example, as described in User-defined objects.

What is logger config?

The config() method of a Logger class used to Log an config message. This method is used to pass config types logs to all the registered output Handler objects. Config Level: Configuration Information may be like what CPU the application is running on, how much is the disk and memory space.

What is the use of logging getLogger?

The getLogger() function accepts a single argument - the logger's name. It returns a reference to a logger instance with the specified name if provided, or root if not. Multiple calls to getLogger() with the same name will return a reference to the same logger object.


2 Answers

logging is a package. Modules in packages aren't imported until you (or something in your program) imports them. You don't need both import logging and import logging.config though: just import logging.config will make the name logging available already.

like image 65
Thomas Wouters Avatar answered Oct 12 '22 16:10

Thomas Wouters


Just add an addtional explanation for Thomas's answer.

logging is a package, a directory.

enter the logging dir and list what files there is:

config.py handlers.py __init__.py __pycache__

so, There is a config.py file in logging directory, but why it can't import logging.config. That's because there is no config namespace in logging/__init__.py

like image 40
DennisLi Avatar answered Oct 12 '22 16:10

DennisLi