Skip to main content

Include pyramid request attributes in your log messages

Project description


This package includes a Python logging formatter which makes Pyramid_
request attributes available for use in its format string.

Getting Started

To log the request method and path with all log messages::

import logging
from pyramid_log import Formatter

fmt = Formatter(
'%(asctime)s %(request.method)s %(request.path_qs)s: %(message)s')

for handler in logging.getLogger().handlers:

Now, if, in one of your views, you do::

log = logging.getLogger()
log.warning("I say %s", "howdy!")

you’ll get a log message like::

2014-10-01 17:55:02,001 GET /path?arg=foo: I say howdy!

All attributes of the current pyramid request are available for use in
the format string (using “dotted” keys starting with the prefix
``'request.'``. (Admittedly, for logging purposes, some request
attributes are more useful than others.) Adding extra dots to the key
will get you attributes of request attributes. For example the
matched route name is available as ``%(``.

See the `pyramid.request`_ documentation for more details on what request
attributes might be available.

Configuring Logging in a File

If you configure logging in your app config (or some other) file you can
do something like::

key = root

keys = console

keys = pyramid

level = INFO
handlers = console

class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = pyramid

class = pyramid_log.Formatter
format = %(asctime)s %(request.method)s %(request.path_qs)s
%(levename)-5.5s [%(name)s][%(threadName)s] %(message)s

See the `pyramid chapter on logging`_ and the docs for the Python
logging_ module’s `configuration file format`_ for more details on how
this works.


Development happens at

See Also

The `pyramid_logging`_ package provides similar functionality.

.. _pyramid_logging:


Jeff Dairiki <>

.. _pyramid:
.. _pyramid.request:
.. _pyramid chapter on logging:
.. _logging:
.. _configuration file format:


Next Release

- Initial release

Project details

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page