What is the correct way of configuring Python's logging.FileHandler?

▼魔方 西西 提交于 2020-03-14 05:34:58

问题


I wrote a small-ish Python script that handles nightly conversions and archiving of audio data. Since there have been some unexpected problems along the way (non-existing files, unreliable connection to the database server and so on...), I added Python's own logging facility to keep track of any encountered problems.

The problem is, the log file is created wherever the script is run from (e.g. current working directory), so I have two logfiles, one in my homedir (which is used when the script is run by cron) and one in the script's own directory (used when I debug it). I'd much prefer keeping the logfile and configuration file in the same directory as the script.

I'm loading logger's configuration here:

logging.config.fileConfig(os.path.join(sys.path[0], 'logger.conf'))

...and here is the relevant portion of my logger.conf:

[handler_fileHandler]
class=FileHandler
level=DEBUG
formatter=fileFormatter
args=('echi_export.log', 'a',)

Absolute paths do work, I'm a bit reluctant to use them though.

In short, what is the proper way to configure file logging using Python's logger module (especially the FileHandler)? Some real-world examples will suffice.


回答1:


So, apparently I can use Python expressions inside the configuration file:

[handler_fileHandler]
  <snip>
args=(os.path.join(sys.path[0],'echi_export.log'), 'a',)

This results in the logfile being created in the same directory the script resides in.

(os.path.dirname(__file__) resolves to /usr/lib/python2.7/logging/ on my system, which is probably where the logging module resides).



来源:https://stackoverflow.com/questions/9484232/what-is-the-correct-way-of-configuring-pythons-logging-filehandler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!