Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get file name of logging.FileHandler in Python?

A logging.FileHandler is constructed with a file name, so is there any way to get the file name from the logging.FileHandler object?

I tried dir(logging.FileHandler) but didn't see any possible solutions.

like image 212
Derrick Zhang Avatar asked Oct 10 '12 06:10

Derrick Zhang


People also ask

What is logger logging getLogger (__ name __)?

The logger name hierarchy is analogous to the Python package hierarchy, and identical to it if you organise your loggers on a per-module basis using the recommended construction logging.getLogger(__name__) . That's because in a module, __name__ is the module's name in the Python package namespace.

What is FileHandler in logging?

FileHandler. The FileHandler class, located in the core logging package, sends logging output to a disk file. It inherits the output functionality from StreamHandler .


2 Answers

>>> import logging
>>> fh = logging.FileHandler('/Users/defuz/test.txt')
>>> fh.baseFilename
'/Users/defuz/test.txt'
>>> fh.stream.name
'/Users/defuz/test.txt'
like image 83
defuz Avatar answered Oct 05 '22 05:10

defuz


Please find dir(logging.FileHandler)

 ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', 
  '__getattribute__', '__hash__', '__init__', '__module__', '__new__', 
  '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', 
  '__str__', '__subclasshook__', '__weakref__', '_name', '_open', 'acquire', 
  'addFilter', 'baseFilename', 'close', 'createLock', 'emit', 'encoding', 'filter', 
  'filters', 'flush', 'format', 'formatter', 'get_name', 'handle', 'handleError', 
  'level', 'lock', 'mode', 'name', 'release', 'removeFilter', 'setFormatter', 'setLevel', 
  'set_name', 'stream']

You have option obj.baseFilename to get the file name.

like image 21
user2805885 Avatar answered Oct 05 '22 05:10

user2805885