[Tutor] Python logging

Kent Johnson kent37 at tds.net
Sun Apr 19 12:34:24 CEST 2009


On Sat, Apr 18, 2009 at 5:07 PM, Scott SA <pydev at rscorp.ab.ca> wrote:

> 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/

Looking at the code for the logging module, I think it will work to
create the RotatingFileHandler with the parameter delay=True, then in
your code get the handler and change its baseFilename attribute to be
the path you want, e.g.
"/home/user/workspace/logged_app/test_warn.log".

Kent


More information about the Tutor mailing list