configurable json logging for atst app
This commit is contained in:
parent
5d05c146d6
commit
d73b082471
25
atst/app.py
25
atst/app.py
@ -29,11 +29,16 @@ from atst.utils.form_cache import FormCache
|
|||||||
from atst.utils.json import CustomJSONEncoder
|
from atst.utils.json import CustomJSONEncoder
|
||||||
from atst.queue import queue
|
from atst.queue import queue
|
||||||
|
|
||||||
|
from logging.config import dictConfig
|
||||||
|
from atst.utils.logging import JsonFormatter, RequestContextFilter
|
||||||
|
|
||||||
|
|
||||||
ENV = os.getenv("FLASK_ENV", "dev")
|
ENV = os.getenv("FLASK_ENV", "dev")
|
||||||
|
|
||||||
|
|
||||||
def make_app(config):
|
def make_app(config):
|
||||||
|
if ENV == "prod" or config.get("LOG_JSON"):
|
||||||
|
apply_json_logger()
|
||||||
|
|
||||||
parent_dir = Path().parent
|
parent_dir = Path().parent
|
||||||
|
|
||||||
@ -143,6 +148,7 @@ def map_config(config):
|
|||||||
"RQ_QUEUES": [config["default"]["RQ_QUEUES"]],
|
"RQ_QUEUES": [config["default"]["RQ_QUEUES"]],
|
||||||
"DISABLE_CRL_CHECK": config.getboolean("default", "DISABLE_CRL_CHECK"),
|
"DISABLE_CRL_CHECK": config.getboolean("default", "DISABLE_CRL_CHECK"),
|
||||||
"CRL_FAIL_OPEN": config.getboolean("default", "CRL_FAIL_OPEN"),
|
"CRL_FAIL_OPEN": config.getboolean("default", "CRL_FAIL_OPEN"),
|
||||||
|
"LOG_JSON": config.getboolean("default", "LOG_JSON"),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -228,3 +234,22 @@ def make_mailer(app):
|
|||||||
)
|
)
|
||||||
sender = app.config.get("MAIL_SENDER")
|
sender = app.config.get("MAIL_SENDER")
|
||||||
app.mailer = mailer.Mailer(mailer_connection, sender)
|
app.mailer = mailer.Mailer(mailer_connection, sender)
|
||||||
|
|
||||||
|
|
||||||
|
def apply_json_logger():
|
||||||
|
dictConfig(
|
||||||
|
{
|
||||||
|
"version": 1,
|
||||||
|
"formatters": {"default": {"()": lambda *a, **k: JsonFormatter()}},
|
||||||
|
"filters": {"requests": {"()": lambda *a, **k: RequestContextFilter()}},
|
||||||
|
"handlers": {
|
||||||
|
"wsgi": {
|
||||||
|
"class": "logging.StreamHandler",
|
||||||
|
"stream": "ext://flask.logging.wsgi_errors_stream",
|
||||||
|
"formatter": "default",
|
||||||
|
"filters": ["requests"],
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": {"level": "INFO", "handlers": ["wsgi"]},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
@ -2,11 +2,12 @@ import datetime
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from flask import g, request
|
from flask import g, request, has_request_context
|
||||||
|
|
||||||
|
|
||||||
class RequestContextFilter(logging.Filter):
|
class RequestContextFilter(logging.Filter):
|
||||||
def filter(self, record):
|
def filter(self, record):
|
||||||
|
if has_request_context():
|
||||||
if getattr(g, "current_user", None):
|
if getattr(g, "current_user", None):
|
||||||
record.user_id = str(g.current_user.id)
|
record.user_id = str(g.current_user.id)
|
||||||
|
|
||||||
|
@ -10,6 +10,7 @@ DISABLE_CRL_CHECK = false
|
|||||||
CRL_FAIL_OPEN = false
|
CRL_FAIL_OPEN = false
|
||||||
DEBUG = true
|
DEBUG = true
|
||||||
ENVIRONMENT = dev
|
ENVIRONMENT = dev
|
||||||
|
LOG_JSON = false
|
||||||
PERMANENT_SESSION_LIFETIME = 600
|
PERMANENT_SESSION_LIFETIME = 600
|
||||||
PE_NUMBER_CSV_URL = http://c95e1ebb198426ee57b8-174bb05a294821bedbf46b6384fe9b1f.r31.cf5.rackcdn.com/penumbers.csv
|
PE_NUMBER_CSV_URL = http://c95e1ebb198426ee57b8-174bb05a294821bedbf46b6384fe9b1f.r31.cf5.rackcdn.com/penumbers.csv
|
||||||
PGAPPNAME = atst
|
PGAPPNAME = atst
|
||||||
|
Loading…
x
Reference in New Issue
Block a user