-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: rollback json access logging (#3694)
- Loading branch information
1 parent
d6ab996
commit 6f66e0f
Showing
8 changed files
with
77 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,27 @@ | ||
import json | ||
import logging | ||
import os | ||
import sys | ||
from datetime import datetime | ||
from typing import Any | ||
|
||
from gunicorn.config import Config | ||
from gunicorn.glogging import Logger as GunicornLogger | ||
|
||
|
||
class JsonFormatter(logging.Formatter): | ||
"""Custom formatter for json logs.""" | ||
|
||
def get_json_record(self, record: logging.LogRecord) -> dict[str, Any]: | ||
formatted_message = record.getMessage() | ||
return { | ||
"levelname": record.levelname, | ||
"message": formatted_message, | ||
"timestamp": self.formatTime(record, self.datefmt), | ||
"logger_name": record.name, | ||
"process_id": record.process, | ||
"thread_name": record.threadName, | ||
} | ||
|
||
def format(self, record: logging.LogRecord) -> str: | ||
def format(self, record): | ||
""" | ||
%s is replaced with {} because legacy string formatting | ||
conventions in django-axes module prevent correct | ||
interpolation of arguments when using this formatter. | ||
""" | ||
try: | ||
return json.dumps(self.get_json_record(record)) | ||
log_message = record.msg.replace("%s", "{}") | ||
formatted_message = log_message.format(*record.args) | ||
log_record = { | ||
"levelname": record.levelname, | ||
"message": formatted_message, | ||
"timestamp": self.formatTime(record, self.datefmt), | ||
"logger_name": record.name, | ||
"process_id": record.process, | ||
"thread_name": record.threadName, | ||
} | ||
return json.dumps(log_record) | ||
except (ValueError, TypeError) as e: | ||
return json.dumps({"message": f"{e} when dumping log"}) | ||
|
||
|
||
class GunicornAccessLogJsonFormatter(JsonFormatter): | ||
def get_json_record(self, record: logging.LogRecord) -> dict[str, Any]: | ||
response_time = datetime.strptime(record.args["t"], "[%d/%b/%Y:%H:%M:%S %z]") | ||
url = record.args["U"] | ||
if record.args["q"]: | ||
url += f"?{record.args['q']}" | ||
|
||
return { | ||
**super().get_json_record(record), | ||
"time": response_time.isoformat(), | ||
"path": url, | ||
"remote_ip": record.args["h"], | ||
"method": record.args["m"], | ||
"status": str(record.args["s"]), | ||
"user_agent": record.args["a"], | ||
"referer": record.args["f"], | ||
"duration_in_ms": record.args["M"], | ||
"pid": record.args["p"], | ||
} | ||
|
||
|
||
class GunicornJsonCapableLogger(GunicornLogger): | ||
def setup(self, cfg: Config) -> None: | ||
super().setup(cfg) | ||
if os.getenv("LOG_FORMAT") == "json": | ||
self._set_handler( | ||
self.error_log, | ||
cfg.errorlog, | ||
JsonFormatter(), | ||
) | ||
self._set_handler( | ||
self.access_log, | ||
cfg.accesslog, | ||
GunicornAccessLogJsonFormatter(), | ||
stream=sys.stdout, | ||
) | ||
return f"Error formatting log record: {str(e)}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters