Add support for JSON log format. #7138

This commit is contained in:
Florian 2024-02-22 12:09:25 +01:00 committed by GitHub
parent dee1a2ae6b
commit 16406f88a7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 31 additions and 4 deletions

View File

@ -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

View File

@ -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'

View File

@ -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")

View File

@ -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)