Bottle wsgilog

Python has awesome tools for logging, including standard logging module. Also, through WSGI protocol we have the wsgilog middleware. Using it is easy with any Python web framework.

You can easily integrate it in bottle environment.

import bottle
from wsgilog import log

app = bottle.Bottle(catchall=False)
logger_middleware = log(tohtml=True, tofile='wsgi.log', tostream=True, toprint=True)

def index():
    print 'STDOUT is logged.'
    bottle.request.environ['wsgilog.logger'].info('This information is logged.')
    # Exception will be logged and sent to the browser formatted as HTML only if app catchall=False.
    raise Exception()

app = logger_middleware(app)

If you need to log each call to bottle, you can use application hooks.

def before_request():
    logger = bottle.request.environ['wsgilog.logger']'before_request %s %s' % (request.method, request.path))

def after_request():
    logger = bottle.request.environ['wsgilog.logger']'after_request %s %s' % (request.method, request.path))

Comments !