Guard against missing request context in logging docs.
This commit is contained in:
parent
99660cc409
commit
45b2e7702d
1 changed files with 8 additions and 3 deletions
|
|
@ -117,13 +117,18 @@ your own fields that can be used in messages. You can change the formatter for
|
||||||
Flask's default handler, the mail handler defined above, or any other
|
Flask's default handler, the mail handler defined above, or any other
|
||||||
handler. ::
|
handler. ::
|
||||||
|
|
||||||
from flask import request
|
from flask import has_request_context, request
|
||||||
from flask.logging import default_handler
|
from flask.logging import default_handler
|
||||||
|
|
||||||
class RequestFormatter(logging.Formatter):
|
class RequestFormatter(logging.Formatter):
|
||||||
def format(self, record):
|
def format(self, record):
|
||||||
record.url = request.url
|
if has_request_context():
|
||||||
record.remote_addr = request.remote_addr
|
record.url = request.url
|
||||||
|
record.remote_addr = request.remote_addr
|
||||||
|
else:
|
||||||
|
record.url = None
|
||||||
|
record.remote_addr = None
|
||||||
|
|
||||||
return super().format(record)
|
return super().format(record)
|
||||||
|
|
||||||
formatter = RequestFormatter(
|
formatter = RequestFormatter(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue