Skip to content

Commit 9f1d4ec

Browse files
authored
feat(Logging): Unify logging format (#7050)
1 parent e09f651 commit 9f1d4ec

File tree

8 files changed

+73
-343
lines changed

8 files changed

+73
-343
lines changed

api/.env-ci

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
DATABASE_URL=postgresql://postgres:password@localhost:5432/flagsmith
22
ANALYTICS_DATABASE_URL=postgresql://postgres:password@localhost:5433/analytics
33
PYTEST_ADDOPTS=--cov . --cov-report xml -n auto --ci
4-
GUNICORN_LOGGER_CLASS=util.logging.GunicornJsonCapableLogger
54
COVERAGE_CORE=sysmon

api/app/settings/common.py

Lines changed: 7 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111
"""
1212

1313
import importlib
14-
import json
14+
import logging
1515
import os
1616
import warnings
1717
from datetime import datetime, time, timedelta
1818
from typing import Any
1919

2020
import dj_database_url
2121
import django_stubs_ext
22-
import prometheus_client
2322
import pytz
2423
from common.core import ReplicaReadStrategy
2524
from corsheaders.defaults import default_headers # type: ignore[import-untyped]
@@ -610,72 +609,19 @@
610609
CHARGEBEE_API_KEY = env("CHARGEBEE_API_KEY", default=None)
611610
CHARGEBEE_SITE = env("CHARGEBEE_SITE", default=None)
612611

613-
# Logging configuration
614-
ACCESS_LOG_EXTRA_ITEMS = env.list("ACCESS_LOG_EXTRA_ITEMS", subcast=str, default=[])
615-
LOGGING_CONFIGURATION_FILE = env.str("LOGGING_CONFIGURATION_FILE", default=None)
616-
if LOGGING_CONFIGURATION_FILE:
617-
with open(LOGGING_CONFIGURATION_FILE, "r") as f:
618-
LOGGING = json.loads(f.read())
619-
else:
620-
LOG_FORMAT = env.str("LOG_FORMAT", default="generic")
621-
LOG_LEVEL = env.str("LOG_LEVEL", default="WARNING")
622-
LOGGING = {
623-
"version": 1,
624-
"disable_existing_loggers": False,
625-
"formatters": {
626-
"generic": {"format": "%(name)-12s %(levelname)-8s %(message)s"},
627-
"json": {
628-
"()": "util.logging.JsonFormatter",
629-
"datefmt": "%Y-%m-%d %H:%M:%S",
630-
},
631-
},
632-
"handlers": {
633-
"console": {
634-
"level": LOG_LEVEL,
635-
"class": "logging.StreamHandler",
636-
"formatter": LOG_FORMAT,
637-
}
638-
},
639-
"loggers": {
640-
"": {"level": LOG_LEVEL, "handlers": ["console"]},
641-
# Not sure why the following loggers are necessary, but it doesn't seem to
642-
# write log messages for e.g. features.workflows.core.models without adding
643-
# them explicitly.
644-
# TODO: move all apps to a parent 'apps' directory and configure the logger
645-
# for that dir
646-
"features": {
647-
"level": LOG_LEVEL,
648-
"handlers": ["console"],
649-
"propagate": False,
650-
},
651-
"task_processor": {
652-
"level": LOG_LEVEL,
653-
"handlers": ["console"],
654-
"propagate": False,
655-
},
656-
"app_analytics": {
657-
"level": LOG_LEVEL,
658-
"handlers": ["console"],
659-
"propagate": False,
660-
},
661-
"webhooks": {
662-
"level": LOG_LEVEL,
663-
"handlers": ["console"],
664-
"propagate": False,
665-
},
666-
},
667-
}
612+
# Logging is configured by flagsmith-common's setup_logging() in ensure_cli_env(),
613+
# before Django loads. Disable Django's logging config to preserve our setup.
614+
LOGGING_CONFIG = None
615+
LOG_FORMAT = env.str("LOG_FORMAT", default="generic")
616+
LOG_LEVEL = env.str("LOG_LEVEL", default="WARNING")
668617

669618
ENABLE_DB_LOGGING = env.bool("DJANGO_ENABLE_DB_LOGGING", default=False)
670619
if ENABLE_DB_LOGGING:
671620
if not DEBUG:
672621
warnings.warn("Setting DEBUG=True to ensure DB logging functions correctly.")
673622
DEBUG = True
674623

675-
LOGGING["loggers"]["django.db.backends"] = {
676-
"level": "DEBUG",
677-
"handlers": ["console"],
678-
}
624+
logging.getLogger("django.db.backends").setLevel(logging.DEBUG)
679625

680626
CACHE_FLAGS_SECONDS = env.int("CACHE_FLAGS_SECONDS", default=0)
681627
FLAGS_CACHE_LOCATION = "environment-flags"
@@ -1442,12 +1388,6 @@
14421388
INSTALLED_APPS.append("licensing")
14431389

14441390
PROMETHEUS_ENABLED = env.bool("PROMETHEUS_ENABLED", False)
1445-
PROMETHEUS_HISTOGRAM_BUCKETS = tuple(
1446-
env.list(
1447-
"PROMETHEUS_HISTOGRAM_BUCKETS",
1448-
default=prometheus_client.Histogram.DEFAULT_BUCKETS,
1449-
)
1450-
)
14511391

14521392
DOCGEN_MODE = env.bool("DOCGEN_MODE", default=False)
14531393

api/organisations/urls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@
161161
]
162162

163163
if settings.LICENSING_INSTALLED: # pragma: no cover
164-
from licensing.views import ( # type: ignore[import-not-found]
164+
from licensing.views import ( # type: ignore[import-not-found,import-untyped,unused-ignore]
165165
create_or_update_licence,
166166
)
167167

api/poetry.lock

Lines changed: 63 additions & 63 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ pygithub = "~2.8"
163163
hubspot-api-client = "^12.0.0"
164164
djangorestframework-dataclasses = "^1.3.1"
165165
pyotp = "^2.9.0"
166-
flagsmith-common = { version = "^3.3.0", extras = [
166+
flagsmith-common = { version = ">=3.5.0,<4", extras = [
167167
"common-core",
168168
"flagsmith-schemas",
169169
"task-processor",

api/scripts/run-docker.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ set -e
33

44
# common environment variables
55
ACCESS_LOG_FORMAT=${ACCESS_LOG_FORMAT:-'%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s" %({origin}i)s %({access-control-allow-origin}o)s'}
6+
APPLICATION_LOGGERS=${APPLICATION_LOGGERS:-"common,features,task_processor,app_analytics,webhooks"}
67

78
waitfordb() {
89
if [ -z "${SKIP_WAIT_FOR_DB}" ]; then

api/tests/unit/util/test_logging.py

Lines changed: 0 additions & 140 deletions
This file was deleted.

api/util/logging.py

Lines changed: 0 additions & 70 deletions
This file was deleted.

0 commit comments

Comments
 (0)