Package: pyramid-log
What It DoesThepyramid_logdistribution includes a Pythonlogging formatterwhich makesPyramidrequest attributes available for use in its
format string.  Specifically,pyramid_log.Formatteris special in
the following ways:It sets a.requestattribute 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"%(request.matched_route.name)s"will work in the format string.There is a syntax for explicitly specifying fallback values.  For
example, a format string of"%(request.method|<norequest>)s"will format to"<no request>"if there is no current request (or
if the current request has nomethodattribute.)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.methodrequest.url(orrequest.path,request.path_qs, etc…)request.unauthenticated_useridrequest.client_addrrequest.GET(orrequest.POSTorrequest.params)request.matched_route.name,request.view_nameSee thePyramid documentationfor a more
complete list of available request attributes.InstallationThe distribution may be downloaded frompypi, but it may be easier to
install usingpip:pip install pyramid-logIt has been tested on python 2.7, 3.4–3.6 and pypy.Development happens athttps://github.com/dairiki/pyramid_log/.ConfigurationConfiguring Logging in a FileIf you configure logging in your application configuration (or some
other) file you can do something like:[loggers]
key = root

[handlers]
keys = console

[formatters]
keys = pyramid

[logger_root]
level = INFO
handlers = console

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

[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)sThis 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’schapter on loggingand the documentation for the
Pythonloggingmodule’sconfiguration file formatfor more details
on how this works.Imperative ConfigurationYou 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')

logging.basicConfig()
root_logger = logging.getLogger()
for handler in root_logger.handlers:
    handler.setFormatter(fmt)Then, a view can log a message like so:log = logging.getLogger(__name__)

@view_config(name='persimmon')
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 192.168.1.1 GET /persimmon: persimmon was calledFurther DetailsAccessing Dict-like ValuesThe dot notation can be used to access not only instance attributes,
but also to access items indict-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,"%(request.matchdict.id)s"will get atrequest.matchdict['id'].Numeric FallbackExplicit 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, then0(zero) is used in integer contexts andNaNis used infloatcontexts.Default Fallback ValuesIf 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 AlsoThepyramid_loggingdistribution provides similar functionality.AuthorJeff Dairiki <dairiki@dairiki.org>HistoryRelease 1.0.1 (2021-12-07)A bit of code refactoring and cleanup in preparation for dropping
python 2 support.No API or functional changes.Release 1.0 (2021-12-05)This release adds support for python>=3.8 and pyramid>=2.The 1.x releases will be the last to support running under python 2.7.CompatibilityPython >= 3.8:logger.Formatterrequires thevalidate=Falseargument, otherwise it forbids'.'in %-style format strings.Pyramid >= 2.0: provide our own replacement forpyramid.compatwhich no longer existsTestingTest under python 3.7–3.10 and pypy3.Stop testing under python 3.4 and 3.5.Test with Pyramid 1.*Convert CI tests from Travis to github workflowPackagingThe packaging has been PEP517-ized.Release 0.2.1 (2017-12-17)This release officially drops support for python 2.6, 3.2, 3.3 (and
therefore pypy3) and adds support for python 3.5 and 3.6.Other than changes in test configuration, there are no substantive
changes from0.2.Release 0.2 (2014-10-09)FeaturesBetter fallback values.Now, by default, if an attribute is missing (which can happen, e.g.,
for%(request.method)sis 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
amethodattribute.)Dict-like access to valuesWhen 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 atrequest.matchdict['arg'].Release 0.1.1 (2014-10-02)Bugs FixedIf an exception is thrown by a request property, render it asNone.Disable logging during log formatting to prevent recursion if a request
property generates a log message.Release 0.1 (2014-10-02)Initial release