Forums

bottle.py logging

How do I change the logging format for webapps? I usually do the following in each module to make clear where log messages come from.

import logging
logging = logging.getLogger('fully.qualified.module.name')

This usually creates logs like the following.

INFO:gwf.rcm_lora_v1:protocol 1

Unfortunately the webapp log looks like this...

2017-04-24 13:27:23,883 :protocol 1

Preferably the log would include timestamps (with timezone field, i.e. +00:00) and the identifier.

That's because your logging is actually being routed through the root logger that we have configured for your web app. That's probably not a great idea on our part and we'll consider changing it.

In the meantime, you can do something like this:

h = logging.FileHandler("/tmp/bottlelog.log")
h.setFormatter(logging.Formatter('%(levelname)s:%(module)s %(msg)s'))
logger.addHandler(h)

to log the messages you want to a file you specify.

Thanks Glenn,

I just discovered that I do get the logs I wanted... It seems that .errors.log and .server.log are treated differently. This is from an unconfigured logger, e.g. logging.info(...)

2017-04-24 20:22:31,280 :processing uplink 6f0f1e35-47c3-4780-8267-b53ef517e3c2

A small logging.basicConfig(level=logging.INFO) in the main bottle_app.py and the following showed up in *.server.log:

2017-04-24 20:22:31 INFO:bottle_app:processing uplink 6f0f1e35-47c3-4780-8267-b53ef517e3c2

Is that a convenient bug or by intention? ;-)

For various historical reasons, stderr goes to the error log, whereas stdout goes to the server log...