Package: request-id
request-idAttach a unique identifier to every request in your WSGI application.request-idis implemented as a WSGI middleware.The package will do 3 things:Generate a uniquerequest_ididentifier which will be stored in the
WSGIenvironand set as theX-Request-IDresponse header.Rename the processing thread with therequest_idsuch that any log
messages output by the logger have therequest_idattached.Log the request to the Python stdlib logging library, which can be used
to generate a simple access log.InstallationYou may install therequest-idpackage using pip:$ pip install request-idConfigure with PasteDeployUpdate your application INI to run in a pipeline with therequest-idfilter:[app:myapp]
use = egg:myapp

[filter:request-id]
use = egg:request-id
format = {status} {REQUEST_METHOD:<6} {REQUEST_PATH:<60} {REQUEST_ID}

[pipeline:main]
pipeline =
  request-id
  myapp

[loggers]
keys = translogger

[handlers]
keys = translogger

[formatters]
keys = minimal

[logger_translogger]
level = INFO
handlers = translogger
qualname = request_id
propagate = 0

[handler_translogger]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = minimal

[formatter_minimal]
format = %(message)sConfigure in codeCreate aRequestIdMiddlewareobject and compose it with your WSGI
application:fromrequest_idimportRequestIdMiddlewarefromwsgiref.simple_serverimportmake_serverdefapp(environ,start_response):start_response('200 OK',[('Content-Type','text/plain')])yield'Hello World\n'app=RequestIdMiddleware(app,format='{status}{REQUEST_METHOD:<6}{REQUEST_PATH:<60}{REQUEST_ID}',)if__name__=='__main__':server=make_server('0.0.0.0',8080,app)server.serve_forever()Access the request_idTherequest_idis stored in the requestenvirondictionary and may
be accessed from anywhere this is available usingrequest_id.get_request_id(request)whererequestis an instance ofwebob.request.Request.Settingslogger_nameThe name of the Python stdlib logger to which the log output will be
delivered. Default:request_idlogging_levelThe name of the Python stdlib logging level on which request information
will be output. Default:INFOformatA formatting string usingPEP-3101string format syntax. Possible
options are:REQUEST_IDREMOTE_ADDRREMOTE_USERREQUEST_METHODREQUEST_URIREQUEST_PATHHTTP_HOSTHTTP_VERSIONHTTP_REFERERHTTP_USER_AGENTtimedurationbytesstatusDefault:'{REMOTE_ADDR} {HTTP_HOST} {REMOTE_USER} [{time}] "{REQUEST_METHOD} {REQUEST_URI} {HTTP_VERSION}" {status} {bytes} {duration} "{HTTP_REFERER}" "{HTTP_USER_AGENT}" - {REQUEST_ID}source_headerIf notNonethen therequest_idwill be pulled from this header
in the request. This is useful if another system upstream is setting a
request identifier which you want to use in the WSGI application.
Default:Noneexclude_prefixesA (space or line separated) list of URL paths to be ignored based onrequest.path_info. Paths should have a leading/in order to match
properly. Default:NoneAcknowledgementsThis code is heavily based on the translogger middleware fromPaste.1.0.1 (2020-10-06)Fix a bug in whichexclude_prefixeswas not matching in cases where
thePATH_INFOin the WSGIenvironwas mutated while processing the
request.1.0 (2018-11-26)Fix a bug inexclude_prefixeswhere it only matches the first request
on Python 3.0.3.1 (2017-11-26)Fix changelog.0.3 (2017-11-26)Do not crash when the source header is missing. Instead set the request id
to “-“.Properly format the UTC offset in the{time}timestamp.Add 100% test coverage.0.2.1 (2016-11-03)Fixlogging_leveloption on Python 3.
Seehttps://github.com/mmerickel/request-id/pull/20.2 (2016-08-09)Catch exceptions and returnwebob.exc.HTTPInternalServerErrorso
that arequest_idmay be attached to the response.0.1.2 (2016-07-26)Fix a couple bugs withexclude_prefixesand add some docs for it.0.1.1 (2016-07-26)Add a new settingexclude_prefixeswhich can be used to avoid
logging certain requests.0.1.0 (2016-07-26)Initial release.