Format Logfile Name with logging.ini

DL Neil PythonList at DancesWithMice.info
Sat May 30 23:56:59 EDT 2020


On 30/05/20 4:52 AM, connor.r.novak at gmail.com wrote:
> In an effort to clean up my python logging practices when creating libraries, I have begun reading into "Advanced Logging" and converting my logging practices into logging configuration `.ini` files:
> 
> [link](https://docs.python.org/3.4/howto/logging.html#configuring-logging)
> 
> My question is: When defining a FileHandler in a `.ini` file, all of the examples that I've seen hardcode the name and location of the log file. In the code snippet below, `python.log` is hardcoded into the `.ini` file:
> 
> ```
> [handler_hand02]
> class=FileHandler
> level=DEBUG
> formatter=form02
> args=('python.log', 'w')
> ```
> [code reference link](https://docs.python.org/3.4/library/logging.config.html#logging-config-fileformat)
> 
> Desired Behavior:
> On each run of the program, a new logfile is created in the `logs/` directory named "<timestamp>_program.log".
> 
> Current Behavior:
> The format in the example above overwrites a single file called "python.log" on each run, which is not the desired behavior.
> 
> Question: Is there a standard procedure for using .ini files to create a new logfile prepended with the current Unix timestamp on each run of the program?


There is nothing in the PSL to do this (to my knowledge) - 
correction/education welcome...

However, it is a fairly standard pattern to take a base-name from a 
config file (.ini in MSFT wording) and then to post-process to suit the 
application's purposes.

In this case, adding a timestamp makes sense - although perhaps a more 
human-readable option unless file-name length is a concern.

The pattern is to:
Compute the (log) file-name.
Check to see if it name already exists (in this directory).
Re-compute if necessary - rinse-and-repeat.

Something like: Make unique file name 
https://code.activestate.com/recipes/577200-make-unique-file-name/
-- 
Regards =dn


More information about the Python-list mailing list