From 243488128ccbdcf37466defeb173857d79484f11 Mon Sep 17 00:00:00 2001 From: nadeemshahzad Date: Tue, 8 Oct 2024 15:08:11 +0500 Subject: [PATCH 1/3] feat: Update Gunicorn Logging Configuration for Datadog Compatibility --- notesserver/docker_gunicorn_configuration.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/notesserver/docker_gunicorn_configuration.py b/notesserver/docker_gunicorn_configuration.py index fb612f73..ff2c418d 100644 --- a/notesserver/docker_gunicorn_configuration.py +++ b/notesserver/docker_gunicorn_configuration.py @@ -9,6 +9,17 @@ workers = 2 +# Set the log level for Gunicorn +loglevel = 'info' + +# Set access and error logs to std +accesslog = '-' # Access logs to stdout +errorlog = '-' # Error logs to stdout + +# Define the custom access log format to match the Datadog pipeline format +access_log_format = '%(t)s %(l)s [%(r)s] [%(p)s] [dd.trace_id=%(o)s dd.span_id=%(O)s] "%(m)s %(U)s %(H)s" %(s)s %(b)s "%(f)s" "%(a)s"' + + def pre_request(worker, req): worker.log.info("%s %s" % (req.method, req.path)) From 51bd007a57e77e185578cc034e5eeb938a2919c2 Mon Sep 17 00:00:00 2001 From: nadeemshahzad Date: Tue, 8 Oct 2024 23:41:29 +0500 Subject: [PATCH 2/3] chore: update --- notesserver/docker_gunicorn_configuration.py | 11 ----- notesserver/settings/logger.py | 42 ++------------------ 2 files changed, 4 insertions(+), 49 deletions(-) diff --git a/notesserver/docker_gunicorn_configuration.py b/notesserver/docker_gunicorn_configuration.py index ff2c418d..fb612f73 100644 --- a/notesserver/docker_gunicorn_configuration.py +++ b/notesserver/docker_gunicorn_configuration.py @@ -9,17 +9,6 @@ workers = 2 -# Set the log level for Gunicorn -loglevel = 'info' - -# Set access and error logs to std -accesslog = '-' # Access logs to stdout -errorlog = '-' # Error logs to stdout - -# Define the custom access log format to match the Datadog pipeline format -access_log_format = '%(t)s %(l)s [%(r)s] [%(p)s] [dd.trace_id=%(o)s dd.span_id=%(O)s] "%(m)s %(U)s %(H)s" %(s)s %(b)s "%(f)s" "%(a)s"' - - def pre_request(worker, req): worker.log.info("%s %s" % (req.method, req.path)) diff --git a/notesserver/settings/logger.py b/notesserver/settings/logger.py index be084b9e..0eec01fa 100644 --- a/notesserver/settings/logger.py +++ b/notesserver/settings/logger.py @@ -15,10 +15,6 @@ def build_logging_config(): """ Return the appropriate logging config dictionary. You should assign the result of this to the LOGGING var in your settings. - If dev_env is set to true logging will not be done via local rsyslogd, - instead, application logs will be dropped in log_dir. - "edx_filename" is ignored unless dev_env is set to true since otherwise - logging is handled by rsyslogd. """ # Revert to INFO if an invalid string is passed in @@ -37,21 +33,19 @@ def build_logging_config(): syslog_format = ( "[service_variant={service_variant}]" "[%(name)s][env:{logging_env}] %(levelname)s " - "[{hostname} %(process)d] [%(filename)s:%(lineno)d] " + "[{hostname} %(process)d] [%(filename)s:%(lineno)d] " "- %(message)s" ).format(service_variant=service_variant, logging_env=logging_env, hostname=hostname) - if debug: - handlers = ['console'] - else: - handlers = ['local'] + handlers = ['console'] logger_config = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'standard': { - 'format': '%(asctime)s %(levelname)s %(process)d ' '[%(name)s] %(filename)s:%(lineno)d - %(message)s', + 'format': '%(asctime)s,%(msecs)03d %(levelname)s [%(name)s] [%(process)d] - %(message)s', + 'datefmt': '%Y-%m-%dT%H:%M:%S', }, 'syslog_format': {'format': syslog_format}, 'raw': {'format': '%(message)s'}, @@ -71,32 +65,4 @@ def build_logging_config(): }, } - if dev_env: - edx_file_loc = os.path.join(log_dir, edx_filename) - logger_config['handlers'].update( - { - 'local': { - 'class': 'logging.handlers.RotatingFileHandler', - 'level': local_loglevel, - 'formatter': 'standard', - 'filename': edx_file_loc, - 'maxBytes': 1024 * 1024 * 2, - 'backupCount': 5, - } - } - ) - else: - logger_config['handlers'].update( - { - 'local': { - 'level': local_loglevel, - 'class': 'logging.handlers.SysLogHandler', - # Use a different address for Mac OS X - 'address': '/var/run/syslog' if sys.platform == "darwin" else '/dev/log', - 'formatter': 'syslog_format', - 'facility': SysLogHandler.LOG_LOCAL0, - } - } - ) - return logger_config From f8a0db77427b087efdfa35018babb160991153b5 Mon Sep 17 00:00:00 2001 From: nadeemshahzad Date: Wed, 9 Oct 2024 00:04:04 +0500 Subject: [PATCH 3/3] fix: formatter --- notesserver/settings/logger.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/notesserver/settings/logger.py b/notesserver/settings/logger.py index 0eec01fa..a0dbcdb3 100644 --- a/notesserver/settings/logger.py +++ b/notesserver/settings/logger.py @@ -59,9 +59,9 @@ def build_logging_config(): }, }, 'loggers': { - 'django': {'handlers': handlers, 'propagate': True, 'level': 'INFO'}, - "elasticsearch.trace": {'handlers': handlers, 'level': 'WARNING', 'propagate': False}, - '': {'handlers': handlers, 'level': 'DEBUG', 'propagate': False}, + 'django': {'handlers': handlers, 'propagate': True, 'level': 'INFO', 'formatter': 'standard'}, + "elasticsearch.trace": {'handlers': handlers, 'level': 'WARNING', 'propagate': False, 'formatter': 'standard'}, + '': {'handlers': handlers, 'level': 'DEBUG', 'propagate': False, 'formatter': 'standard'}, }, }