60

Somewhere in the bowels of my code I have something like:

logger = logging.getLogger('debug0.x')

The way I understand it, this should only respond when I have previously done something like:

logging.basicConfig(filename='10Nov2010a.txt',level=logging.DEBUG, name='debug0')

note that name has been defined as debug0. However, I have discovered that if do

logging.basicConfig(filename='10Nov2010a.txt',level=logging.DEBUG)

without the name keyword, then the debug0.x logger defined above reacts, and writes to the log file. I was thinking it would only react in the first case, when the logger had been named.

I'm confused.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
reckoner
  • 2,861
  • 3
  • 33
  • 43
  • 6
    [`logging.basicConfig()`](http://docs.python.org/library/logging.html#logging.basicConfig) has no `name` keyword argument. – Sven Marnach Nov 10 '10 at 23:46

2 Answers2

124

The Python logging module organizes loggers in a hierarchy. All loggers are descendants of the root logger. Each logger passes log messages on to its parent.

New loggers are created with the getLogger() function. The function call logging.getLogger('debug0.x') creates a logger x which is a child of debug0 which itself is a child of the root logger. When logging to this logger, it will pass on the message to its parent, and its parent will pass the message to the root logger. You configured the root logger to log to a file by the basicConfig() function, so your message will end up there.

codeforester
  • 39,467
  • 16
  • 112
  • 140
Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • 1
    So what's the best practice for getting a logger in Python? In Java you just do a `getLogger(getClass().getName())` (or some convenience method `getLogger(getClass())`, which delegates to the other method I mentioned). What should we use in Python? Surely we don't have to make up some logger ID and manually plug it in for every module we create. – Garret Wilson Oct 08 '16 at 00:49
  • 14
    @Garret The most common approach in Python is to use`logger = logging.getLogger(__name__)` at the top of each module. The variable `__name__` contains the dotted name of the current module. – Sven Marnach Oct 08 '16 at 08:27
  • 1
    Any reason why the best practice isn't to use `_logger` so that the variable isn't exposed when the module is imported? – Garret Wilson Oct 17 '16 at 23:32
  • @GarretWilson Some people use `_logger` or `LOGGER` as well, so that's really up to you. You can also set `__all__` to declare the list of exported names. – Sven Marnach Oct 18 '16 at 12:39
18

If you check out the code or the doc:

>>> print logging.basicConfig.__doc__

    Do basic configuration for the logging system.

    This function does nothing if the root logger already has handlers
    configured. ...............
    A number of optional keyword arguments may be specified, which can alter
    the default behaviour.

    filename  Specifies that a FileHandler be created, using the specified
              filename, rather than a StreamHandler.
    filemode  Specifies the mode to open the file, if filename is specified
              (if filemode is unspecified, it defaults to 'a').
    format    Use the specified format string for the handler.
    datefmt   Use the specified date/time format.
    level     Set the root logger level to the specified level.
    stream    Use the specified stream to initialize the StreamHandler. Note
              that this argument is incompatible with 'filename' - if both
              are present, 'stream' is ignored.

logging.basicConfig does not use name argument at all. It initializes the root logger. While getLogger takes a "name" argument

>>> print logging.getLogger.__doc__

    Return a logger with the specified name, creating it if necessary.

    If no name is specified, return the root logger.
pyfunc
  • 65,343
  • 15
  • 148
  • 136