-
Notifications
You must be signed in to change notification settings - Fork 14
/
application.py
executable file
·82 lines (65 loc) · 2.26 KB
/
application.py
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
#!/usr/bin/env python
import logging
import os
import sys
from structlog import configure, contextvars
from structlog.dev import ConsoleRenderer
from structlog.processors import JSONRenderer, TimeStamper, format_exc_info
from structlog.stdlib import LoggerFactory, add_log_level
from app.utilities.json import json_dumps
def configure_logging():
log_level = logging.INFO
debug = os.getenv("FLASK_DEBUG") == "1"
if debug:
log_level = logging.DEBUG
log_handler = logging.StreamHandler(sys.stdout)
log_handler.setLevel(log_level)
log_handler.addFilter(lambda record: record.levelno <= logging.WARNING)
error_log_handler = logging.StreamHandler(sys.stderr)
error_log_handler.setLevel(logging.ERROR)
logging.basicConfig(
level=log_level, format="%(message)s", handlers=[error_log_handler, log_handler]
)
# Set werkzeug logging level
werkzeug_logger = logging.getLogger("werkzeug")
werkzeug_logger.setLevel(level=log_level)
def parse_exception(_, __, event_dict):
if debug:
return event_dict
exception = event_dict.get("exception")
if exception:
event_dict["exception"] = exception.replace('"', "'").split("\n")
return event_dict
# setup file logging
renderer_processor = (
ConsoleRenderer() if debug else JSONRenderer(serializer=json_dumps)
)
processors = [
contextvars.merge_contextvars,
add_log_level,
TimeStamper(key="created", fmt="iso"),
add_service,
format_exc_info,
parse_exception,
renderer_processor,
]
configure(
logger_factory=LoggerFactory(),
processors=processors,
cache_logger_on_first_use=True,
)
def add_service(logger, method_name, event_dict): # pylint: disable=unused-argument
"""
Add the service name to the event dict.
"""
event_dict["service"] = "eq-questionnaire-runner"
return event_dict
# Initialise logging before the rest of the application
configure_logging()
from app.setup import ( # NOQA isort:skip # pylint: disable=wrong-import-position
create_app,
)
application = create_app()
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
application.run(port=port, threaded=True)