From 16406f88a775bef09b18e3e5097e339c86e0a5f9 Mon Sep 17 00:00:00 2001 From: Florian Date: Thu, 22 Feb 2024 12:09:25 +0100 Subject: [PATCH] Add support for JSON log format. #7138 --- requirements.txt | 1 + web/config.py | 15 +++++++++++++++ web/pgAdmin4.py | 1 - web/pgadmin/__init__.py | 18 +++++++++++++++--- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/requirements.txt b/requirements.txt index 1f228385e..044286cb0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -62,3 +62,4 @@ Werkzeug==2.3.*; python_version > '3.7' Werkzeug==2.2.3; python_version <= '3.7' typer[all]==0.9.* setuptools==69.*; python_version >= '3.12' +jsonformatter~=0.3.2 diff --git a/web/config.py b/web/config.py index 1e08f712b..84041cdd8 100644 --- a/web/config.py +++ b/web/config.py @@ -15,6 +15,7 @@ import builtins import logging import os import sys +from collections import OrderedDict # We need to include the root directory in sys.path to ensure that we can # find everything we need when running in the standalone runtime. @@ -256,6 +257,20 @@ CONSOLE_LOG_LEVEL = logging.WARNING FILE_LOG_LEVEL = logging.WARNING # Log format. +JSON_LOGGER = False +CONSOLE_LOG_FORMAT_JSON = OrderedDict([ + ("time", "asctime"), + ("message", "message"), + ("level", "levelname") +]) + +FILE_LOG_FORMAT_JSON = OrderedDict([ + ("time", "asctime"), + ("message", "message"), + ("level", "levelname") +]) + + CONSOLE_LOG_FORMAT = '%(asctime)s: %(levelname)s\t%(name)s:\t%(message)s' FILE_LOG_FORMAT = '%(asctime)s: %(levelname)s\t%(name)s:\t%(message)s' diff --git a/web/pgAdmin4.py b/web/pgAdmin4.py index 35a04b37d..b9062c80e 100644 --- a/web/pgAdmin4.py +++ b/web/pgAdmin4.py @@ -39,7 +39,6 @@ if (3, 10) > sys.version_info > (3, 8, 99) and os.name == 'posix': # Fix eventlet issue with Python 3.9. # Ref: https://github.com/eventlet/eventlet/issues/670 # This was causing issue in psycopg3 - import select from eventlet import hubs hubs.use_hub("poll") diff --git a/web/pgadmin/__init__.py b/web/pgadmin/__init__.py index 001c79a19..c20016bbc 100644 --- a/web/pgadmin/__init__.py +++ b/web/pgadmin/__init__.py @@ -50,7 +50,7 @@ from pgadmin.utils.csrf import pgCSRFProtect from pgadmin import authenticate from pgadmin.utils.security_headers import SecurityHeaders from pgadmin.utils.constants import KERBEROS, OAUTH2, INTERNAL, LDAP, WEBSERVER - +from jsonformatter import JsonFormatter # Explicitly set the mime-types so that a corrupted windows registry will not # affect pgAdmin 4 to be load properly. This will avoid the issues that may @@ -260,14 +260,26 @@ def create_app(app_name=None): config.LOG_ROTATION_MAX_LOG_FILES) fh.setLevel(config.FILE_LOG_LEVEL) - fh.setFormatter(logging.Formatter(config.FILE_LOG_FORMAT)) + + if config.JSON_LOGGER: + json_formatter = JsonFormatter(config.FILE_LOG_FORMAT_JSON) + fh.setFormatter(json_formatter) + else: + fh.setFormatter(logging.Formatter(config.FILE_LOG_FORMAT)) + app.logger.addHandler(fh) logger.addHandler(fh) # Console logging ch = logging.StreamHandler() ch.setLevel(config.CONSOLE_LOG_LEVEL) - ch.setFormatter(logging.Formatter(config.CONSOLE_LOG_FORMAT)) + + if config.JSON_LOGGER: + json_formatter = JsonFormatter(config.CONSOLE_LOG_FORMAT_JSON) + ch.setFormatter(json_formatter) + else: + ch.setFormatter(logging.Formatter(config.CONSOLE_LOG_FORMAT)) + app.logger.addHandler(ch) logger.addHandler(ch)