What is the correct way of configuring Python'

2019-02-20 12:19发布

问题:

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).