Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic filepath & filename for FileHandler in logger config file in python

Tags:

python

logging

I have a python log config file with a filehandler of the below form.

[handler_filelog]
class: FileHandler
args = ('/var/tmp/log/client.log','a')

Instead, I need it in the below form (dynamically generated path).

[handler_filelog]
class: FileHandler
args = ('/var/tmp/log_<unique_string>/client.log','a')

Multiple instances of the program may be running and hence non-conflicting log paths and files are to be used. The logger once setup need not change till end of program execution.

Is there a way to handle this using the config file approach? I am not keen on resorting to creating the loggers/handlers/formatters by myself since my log config file has many of these and config file based approach is much nicer.

(Update: I am using python 2.4)

like image 605
Gns Avatar asked Feb 23 '12 01:02

Gns


People also ask

How do you make a source dynamic in Power Query?

Name the cells (click in the cell, and add a cell label into the name box); Create custom objects at the start of the Power Query which define the file paths using the named cell references, using the Advanced Editor; Update the file path and file name details in the named cells to refresh the Power Query data source.

How do I create a dynamic path in VBA?

In VBA the path is built using backslash... So, I would suggest to use "\" instead of "/". You might want to check whether mypath contains the text you're expecting... And you probably need to add a folder separator ( / or ` after complete .


2 Answers

This does what you need. You should first extend the FileHandler class. Place this in a file, say myHandler.py in your config file's directory:

import logging
import random
import os
class myFileHandler(logging.FileHandler):
    def __init__(self,path,fileName,mode):
        r = random.randint(1,100000)
        path = path+"/log_"+str(r)
        os.mkdir(path)
        super(myFileHandler,self).__init__(path+"/"+fileName,mode)

And then in the config file, you can use this custom FileHandler like this

class: myHandler.myFileHandler
args = ('/var/tmp','client.log','a')

I tested this one on my machine

like image 152
Phani Avatar answered Sep 21 '22 06:09

Phani


If you're using Python 2.7 or 3.2, you can use dictionary-based configuration, which allows you to specify arbitrary callables to return handlers - you could e.g. use the process PID to construct the file name.

Update: If you are using 2.4, you can use the logutils package, which should work with Python 2.4 (apart from the LoggerAdapter class, which requires 2.5 or later). The logutils package contains the dictionary-based configuration functionality.

like image 20
Vinay Sajip Avatar answered Sep 20 '22 06:09

Vinay Sajip