1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
from textwrap import dedent
import logbook
from .utils import make_fake_mail_handler
def test_handler_filter_after_processor(activation_strategy, logger):
handler = make_fake_mail_handler(
format_string=dedent(
"""
Subject: Application Error for {record.extra[path]} [{record.extra[method]}]
Message type: {record.level_name}
Location: {record.filename}:{record.lineno}
Module: {record.module}
Function: {record.func_name}
Time: {record.time:%Y-%m-%d %H:%M:%S}
Remote IP: {record.extra[ip]}
Request: {record.extra[path]} [{record.extra[method]}]
Message:
{record.message}
"""
).lstrip(),
filter=lambda r, h: "ip" in r.extra,
bubble=False,
)
class Request:
remote_addr = "127.0.0.1"
method = "GET"
path = "/index.html"
def handle_request(request):
def inject_extra(record):
record.extra["ip"] = request.remote_addr
record.extra["method"] = request.method
record.extra["path"] = request.path
processor = logbook.Processor(inject_extra)
with activation_strategy(processor):
handler.push_thread()
try:
try:
1 / 0
except Exception:
logger.exception("Exception happened during request")
finally:
handler.pop_thread()
handle_request(Request())
assert len(handler.mails) == 1
mail = handler.mails[0][2]
assert "Subject: Application Error for /index.html [GET]" in mail
assert "1 / 0" in mail
def test_handler_processors(activation_strategy, logger):
handler = make_fake_mail_handler(
format_string=dedent(
"""
Subject: Application Error for {record.extra[path]} [{record.extra[method]}]
Message type: {record.level_name}
Location: {record.filename}:{record.lineno}
Module: {record.module}
Function: {record.func_name}
Time: {record.time:%Y-%m-%d %H:%M:%S}
Remote IP: {record.extra[ip]}
Request: {record.extra[path]} [{record.extra[method]}]
Message:
{record.message}
"""
).lstrip()
)
class Request:
remote_addr = "127.0.0.1"
method = "GET"
path = "/index.html"
def handle_request(request):
def inject_extra(record):
record.extra["ip"] = request.remote_addr
record.extra["method"] = request.method
record.extra["path"] = request.path
processor = logbook.Processor(inject_extra)
with activation_strategy(processor):
handler.push_thread()
try:
try:
1 / 0
except Exception:
logger.exception("Exception happened during request")
finally:
handler.pop_thread()
handle_request(Request())
assert len(handler.mails) == 1
mail = handler.mails[0][2]
assert "Subject: Application Error for /index.html [GET]" in mail
assert "1 / 0" in mail
|