Make slow query logging more structured.

Also add support for log.exception, which may be called by gunicorn.