How to get filename from a python logger

Assuming the job_logger object has only one handler for now.

>>> handler = job_logger.handlers[0]
>>> filename = handler.baseFilename
>>> print(filename)
'/tmp/test_logging_file'

And when there're multiple handlers, design your logic to get them all or get the very last one.


You can get a list of the handlers used in a logger using:

>>>handlers = job_logger.handlers
>>>handlers
[<FileHandler ./mypath/job_logger.log (NOTSET)>]

As in your case you only have one, @starrify solution should be enough:

>>>handlers[0].baseFilename
'./mypath/job_logger.log'

In case you have more handlers and some are not FileHandlers you can filter them with a list comprehension:

>>> log_paths = [handler.baseFilename for handler in job_logger.handlers if isinstance(handler, logging.FileHandler)]
>>> next(iter(log_paths))
'./mypath/job_logger.log'