March 6, 2010

A Simple Write-to-a-file Turbogears 2 Logger

Filed under: General Information,Turbogears — Ryan Wilcox @ 1:22 am

Turbogears uses Python’s logger module to perform logging operations. This has an advantage (it uses Python’s logger module) and an disadvantage (it uses Python’s logger module).

Be that as it may, here is the simplest configuration possible for you to log to a file (by default TG logs to your STDOUT device, which might not actually be what you want).

In (environment).ini (aka: development.ini)

Note: The format of logging section of this file follows the format laid down in the logging configuration file format section of the standard library documentation.

[handlers]
keys = console, file # added file (line was: "keys = console")

[logger_MY_TG_APP_NAME] # will be named whatever your TG app is named
level = DEBUG
handlers = file
qualname = MY_TG_APP_NAME

[handler_file] # added this section
class = FileHandler
args = ('MY_TG_APP_NAME/logs/foobar-debug.log', 'a+')
level = NOTSET
formatter = generic

Accessing this in your code


import logging
log = logging.getLogger("MY_TG_APP_NAME")

# .... code here ....

log.info("hi!")