Skip to content
Myr

Guard against missing request context in logging docs.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Diego Alberto Torres Quintanilla · committed by Diego Alberto Torres Quintanilla · parent 99660cc · Browse files · 1 file+8 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+8 added−3 removed

  • other:+8 added−3 removed

1 file+8 added−3 removed

modified:docs/logging.rst+8 added-3 removed
115 unmodified lines
116your own fields that can be used in messages. You can change the formatter for117Flask's default handler, the mail handler defined above, or any other118handler. ::119120 from flask import request120 from flask import has_request_context, request121 from flask.logging import default_handler122123 class RequestFormatter(logging.Formatter):124 def format(self, record):125 if has_request_context():125 record.url = request.url126 record.remote_addr = request.remote_addr126 record.url = request.url127 record.remote_addr = request.remote_addr128 else:129 record.url = None130 record.remote_addr = None131132 return super().format(record)133134 formatter = RequestFormatter(135 '[%(asctime)s] %(remote_addr)s requested %(url)s\n'
45 unmodified lines