Python logging - check location of log files?
Asked Answered
P

5

55

What is the methodology for knowing where Python log statements are stored?

i.e. if i do:

import logging
log = logging.getLogger(__name__)
log.info('Test')

Where could I find the logfile? Also, when I call:

logging.getLogger(__name__)

Is that somehow related to how the logger will behave/save?

Perpetuity answered 18/1, 2013 at 19:36 Comment(0)
R
43

The logging module uses handlers attached to loggers to decide how, where, or even if messages ultimately get stored or displayed. You can configure logging by default to write to a file as well. You should really read the docs, but if you call logging.basicConfig(filename=log_file_name) where log_file_name is the name of the file you want messages written to (note that you have to do this before anything else in logging is called at all), then all messages logged to all loggers (unless some further reconfiguration happens later) will be written there. Be aware of what level the logger is set to though; if memory serves, info is below the default log level, so you'd have to include level=logging.INFO in the arguments to basicConfig as well for your message to end up in the file.

As to the other part of your question, logging.getLogger(some_string) returns a Logger object, inserted in to the correct position in the hierarchy from the root logger, with the name being the value of some_string. Called with no arguments, it returns the root logger. __name__ returns the name of the current module, so logging.getLogger(__name__) returns a Logger object with the name set to the name of the current module. This is a common pattern used with logging, as it causes the logger structure to mirror your code's module structure, which often makes logging messages much more useful when debugging.

Ramayana answered 18/1, 2013 at 19:39 Comment(2)
how do you reconfigure the logger after some logs have been written already?Particulate
Per this SO answer, basicConfig sets the handler on the root logger object (or the logger object configured with your given module example_logger = logging.getLogger('example')). Thus, you could remove the existing handler and call basic config again. Alternatively, you could create a different logger object example2_logger = logging.getLogger('example') and set different configs on this object.Rubricate
B
24

To get the log location of a simple file logger, try

logging.getLoggerClass().root.handlers[0].baseFilename
Bander answered 11/7, 2015 at 6:33 Comment(5)
It throws exception to meExpressage
What's the exception? You could try using a debugger and poking around those objects until you find what you needBander
This assumes that the logger had a FileHandler, which is not always the case. Better make sure that the handlers contain a FileHandler object.Rivi
It throws an exception to me as well: "AttributeError: 'StreamHandler' object has no attribute 'baseFilename'"Bowery
In my case it is working logging.handlers[0].baseFilenameNell
H
5

Some good answers on this, but top answer didn't work for me because I was using a different type of file handler, and the handler.stream doesn't provide the path, but file handle, and getting the path out of that is somewhat non-obvious. Here's my solution:

import logging
from logging import FileHandler

# note, this will create a new logger if the name doesn't exist, 
# which will have no handlers attached (yet)
logger = logging.getLogger('<name>')

for h in logger.handlers:
    # check the handler is a file handler 
    # (rotating handler etc. inherit from this, so it will still work)
    # stream handlers write to stderr, so their filename is not useful to us
    if isinstance(h, FileHandler):
        # h.stream should be an open file handle, it's name is the path
        print(h.stream.name)
Himelman answered 15/2, 2021 at 15:43 Comment(2)
ImportError: cannot impart name 'FileHandler' from 'logging.handlers'Delphine
@CodePrinz, good catch, the base class lives in the logging module, not the handlers module. Fixed now.Himelman
A
4

To find the logfile location, try instantiating your log object in a Python shell in your environment and looking at the value of:

log.handlers[0].stream

Apologete answered 11/2, 2014 at 17:29 Comment(2)
I get an IndexError, since my object has no handlers.Delphine
Or an AttributeErrror:'_LiveLoggingNullHandler' object has no attribute 'stream'Delphine
T
0

Excellent question @zallarak. Unfortunately, while they're easy to create, Loggers are difficult to inspect. This gets the filenames of all Handlers for a logger:

filenames = []
for handler in logger.handlers:
    try:
        filenames.append(handler.fh.name)
    except:
        pass

The try block handles exceptions that occur when the filename lookup fails.

Telangiectasis answered 2/5, 2018 at 16:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.