简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM