user2755526
user2755526

Reputation: 197

Accessing a logger across multiple modules in Python logging

I have a little question regarding the python logging module.

I have a simple logger

Logger=basicConfig()

How do I access the same logger using getLogger()?

Or does getLogger() give me a logging object which I can access?

If so how do I access the same logger in another program?

Apologies if it is the wrong place to ask this.

Upvotes: 0

Views: 1603

Answers (1)

Mikko Ohtamaa
Mikko Ohtamaa

Reputation: 83788

The Python logging.getLogger(name) returns always the same logger object with that name within the process.

The Python best practice of using of loggers is that your each Python module defines it own logger at the beginning of the .py file.:

  import logging

  logger = logging.getLogger(__name__)

  # Do something with the logger
  def foobar():
       logger.debug("In foobar")

This allows you to later turn on and off and adjust the levels of individual loggers using Python's logging configuration. Generally, you do not want to share the logger across modules unless you have some very specific use case.

Upvotes: 6

Related Questions