What is the difference between logging.info and logging.getLogger().info?
Question:
If logging.info()
is enough for logging, why we have to instantiate a logger with getLogger()
method?
Answers:
Calling getLogger()
without a name returns the root logger:
Return a logger with the specified name or, if no name is specified, return a logger which is the root logger of the hierarchy.
Calling the module-level info()
function logs directly to the root logger:
Logs a message with level INFO on the root logger.
If you have no use for specifically named loggers (for example in order to identify the emitting module of the log), the two calls are exactly equivalent.
If logging.info()
is enough for logging, why we have to instantiate a logger with getLogger()
method?
Calling getLogger()
without a name returns the root logger:
Return a logger with the specified name or, if no name is specified, return a logger which is the root logger of the hierarchy.
Calling the module-level info()
function logs directly to the root logger:
Logs a message with level INFO on the root logger.
If you have no use for specifically named loggers (for example in order to identify the emitting module of the log), the two calls are exactly equivalent.