[Tutor] Python logging

Scott SA pydev at rscorp.ab.ca
Sat Apr 18 23:07:55 CEST 2009


Hi,

For some reason, I'm having a bit of trouble figuring this one out. I  
know it has to be relatively easy but it just isn't "working" for me  
at the moment.

I want to re-direct the logs from python's logging library and while  
using a config file rather than instantiating all of the logging  
within my actual code.

So, lets say I have a file log.conf that contains the various  
directives for the logging module. Amongst all of the other settings a  
handler might look like this:

	[handler_warn_logfile]
	class=handlers.RotatingFileHandler
	level=WARNING
	formatter=DatetimeLevelMessage
	args=('test_warn.log', 'a', 125829120, 5)
	filename=test_warn.log
	mode=a

then loaded like this

         logging.config.fileConfig(log_config_path)
         logging.debug('Logging enabled')


... which basically says append WARNING messages to the rotating file  
handler in DatetimeLevelMessage format to a log-file named  
test_warn.log splitting the log-file when it exceeds 125829120 bytes.

Because I don't have any full paths listed, the logger uses the  
current working directory i.e. "/home/user/workspace/logged_app/ 
test_warn.log"

I could change the name to "logs/test_warn.log" for a sub-directory  
but I'd like to put a data on some of the logs and not necessarily in  
the root of the scripts but maybe elsewhere on the system i.e. /home/ 
user/logs/someapp/200904_logs/

I've done a bunch of inspection of dictionaries, methods and  
attributes but nothing seems to be surfacing that would allow me to re- 
direct the paths in a dynamic way -- other than using  
os.cwd('some_path') before instantiating the logging... which is quite  
hack-ish IMO.

The only thing that comes to mind, after reviewing the logging via  
help(logging...), is that I may need to dynamically re-write the .conf  
file using ConfigParser.


Any ideas on what I'm missing within the logging for redirecting the  
files? Or, should I bump this up to the main python list?

Thanks in advance,

Scott



More information about the Tutor mailing list