Skip to main content
Python Software Foundation 20th Year Anniversary Fundraiser  Donate today!

Include pyramid request attributes in your log messages

Project description

Latest Version Supported Python versions Supported Python versions License build status

What It Does

The pyramid_log distribution includes a Python logging formatter which makes Pyramid request attributes available for use in its format string. Specifically, pyramid_log.Formatter is special in the following ways:

  • It sets a .request attribute on the log record (if one doesn’t already exist.)
  • It supports dotted attribute access in its format string. For example, "%(request.method)s" and even "%(" will work in the format string.
  • There is a syntax for explicitly specifying fallback values. For example, a format string of "%(request.method|<no request>)s" will format to "<no request>" if there is no current request (or if the current request has no method attribute.)

The pyramid request has many attributes which can be useful when included in the logs of a web app. These include, but are not limited to:

  • request.method
  • request.url (or request.path, request.path_qs, etc…)
  • request.unauthenticated_userid
  • request.client_addr
  • request.GET (or request.POST or request.params)
  •, request.view_name

See the Pyramid documentation for a more complete list of available request attributes.


The distribution may be downloaded from pypi, but it may be easier to install using pip:

pip install pyramid-log

It has been tested on python 2.7, 3.4–3.6 and pypy.

Development happens at


Configuring Logging in a File

If you configure logging in your application configuration (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

# NB: Here is the interesting part!
class = pyramid_log.Formatter
format = %(asctime)s %(request.method|no request)s %(request.path_qs|)s
         %(levelname)-5.5s [%(name)s] %(message)s

This will result in your log messages looking something like:

2014-10-01 17:55:02,001 GET /path?arg=foo
WARNI [myapp.views] This is some log message!

Refer to Pyramid’s chapter on logging and the documentation for the Python logging module’s configuration file format for more details on how this works.

Imperative Configuration

You can of course configure logging imperatively. For example, with:

import logging
from pyramid_log import Formatter

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

root_logger = logging.getLogger()
for handler in root_logger.handlers:

Then, a view can log a message like so:

log = logging.getLogger(__name__)

def persimmon_view(request):
    log.warning("%s was called!", request.view_name)

Which will yield a log message like:

2014-10-01 17:55:02,001 GET /persimmon: persimmon was called

Further Details

Accessing Dict-like Values

The dot notation can be used to access not only instance attributes, but also to access items in dict-like values. Attribute access is tried first; if there is no attribute of the given name, then the instances __getitem__ method is tried. For example, "%(" will get at request.matchdict['id'].

Numeric Fallback

Explicit fallback values are always interpreted as strings, however, if the fallback is used in a numeric context, an attempt will be made at conversion to the requested type. For example, if there is no request, "%+(request.status_code|555)d" will format to "+555".

If the fallback string can not be converted to a numeric value, then 0 (zero) is used in integer contexts and NaN is used in float contexts.

Default Fallback Values

If no fallback value is explicitly specified, then a default fallback value will be used if the requested attribute does not exist. The missing attribute name is included in the default fallback value. For example "%(request.method)s" will produce "<?request.method?>" if there is no current request.

See Also

The pyramid_logging distribution provides similar functionality.


Jeff Dairiki <>


Release 0.2.1 (2017-12-17)

This release officially drops support for python 2.6, 3.2, 3.3 (and theremore pypy3) and adds support for python 3.5 and 3.6.

Other than changes in test configuration, there are no substantive changes from 0.2.

Release 0.2 (2014-10-09)


Better fallback values.

  • Now, by default, if an attribute is missing (which can happen, e.g., for %(request.method)s is there is no current request) it is rendered as <?attribute-name?> (e.g. "<?request.method?>".)
  • There is now a syntax for explicitly specifying fallback values. E.g. "%(request.method|(no-request))" which will format to (no request), if there is no current request (or if the current request does not have a method attribute.)

Dict-like access to values

  • When looking up a dotted name, if an attribute can not be found, dict-style (__getitem__) lookup will be attempted. E.g. "%(request.matchdict.arg)" will get at request.matchdict['arg'].

Release 0.1.1 (2014-10-02)

Bugs Fixed

  • If an exception is thrown by a request property, render it as None.
  • Disable logging during log formatting to prevent recursion if a request property generates a log message.

Release 0.1 (2014-10-02)

  • Initial release

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for pyramid-log, version 0.2.1
Filename, size File type Python version Upload date Hashes
Filename, size pyramid_log-0.2.1-py2.py3-none-any.whl (12.7 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size pyramid_log-0.2.1.tar.gz (12.5 kB) File type Source Python version None Upload date Hashes View

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