From d393b79a8596c2ce4bc3615686c9f3deeaddcf48 Mon Sep 17 00:00:00 2001 From: Alan Rominger Date: Wed, 15 May 2024 11:35:19 -0400 Subject: [PATCH] Fix environment-specific rough edges of logging setup --- awx/main/utils/handlers.py | 66 +++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/awx/main/utils/handlers.py b/awx/main/utils/handlers.py index 4def0b6ba094..22d02d1bdefe 100644 --- a/awx/main/utils/handlers.py +++ b/awx/main/utils/handlers.py @@ -4,6 +4,7 @@ # Python import base64 import logging +import logging.handlers import sys import traceback import os @@ -27,6 +28,9 @@ from opentelemetry.sdk.resources import Resource +__all__ = ['RSysLogHandler', 'SpecialInventoryHandler', 'ColorHandler'] + + class RSysLogHandler(logging.handlers.SysLogHandler): append_nul = False @@ -109,39 +113,35 @@ def emit(self, record): if settings.COLOR_LOGS is True: - try: - from logutils.colorize import ColorizingStreamHandler - import colorama - - colorama.deinit() - colorama.init(wrap=False, convert=False, strip=False) - - class ColorHandler(ColorizingStreamHandler): - def colorize(self, line, record): - # comment out this method if you don't like the job_lifecycle - # logs rendered with cyan text - previous_level_map = self.level_map.copy() - if record.name == "awx.analytics.job_lifecycle": - self.level_map[logging.INFO] = (None, 'cyan', True) - msg = super(ColorHandler, self).colorize(line, record) - self.level_map = previous_level_map - return msg - - def format(self, record): - message = logging.StreamHandler.format(self, record) - return '\n'.join([self.colorize(line, record) for line in message.splitlines()]) - - level_map = { - logging.DEBUG: (None, 'green', True), - logging.INFO: (None, None, True), - logging.WARNING: (None, 'yellow', True), - logging.ERROR: (None, 'red', True), - logging.CRITICAL: (None, 'red', True), - } - - except ImportError: - # logutils is only used for colored logs in the dev environment - pass + from logutils.colorize import ColorizingStreamHandler + import colorama + + colorama.deinit() + colorama.init(wrap=False, convert=False, strip=False) + + class ColorHandler(ColorizingStreamHandler): + def colorize(self, line, record): + # comment out this method if you don't like the job_lifecycle + # logs rendered with cyan text + previous_level_map = self.level_map.copy() + if record.name == "awx.analytics.job_lifecycle": + self.level_map[logging.INFO] = (None, 'cyan', True) + msg = super(ColorHandler, self).colorize(line, record) + self.level_map = previous_level_map + return msg + + def format(self, record): + message = logging.StreamHandler.format(self, record) + return '\n'.join([self.colorize(line, record) for line in message.splitlines()]) + + level_map = { + logging.DEBUG: (None, 'green', True), + logging.INFO: (None, None, True), + logging.WARNING: (None, 'yellow', True), + logging.ERROR: (None, 'red', True), + logging.CRITICAL: (None, 'red', True), + } + else: ColorHandler = logging.StreamHandler