Skip to content

Commit

Permalink
Fix environment-specific rough edges of logging setup
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanCoding committed Oct 28, 2024
1 parent e21dd0a commit d393b79
Showing 1 changed file with 33 additions and 33 deletions.
66 changes: 33 additions & 33 deletions awx/main/utils/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Python
import base64
import logging
import logging.handlers
import sys
import traceback
import os
Expand All @@ -27,6 +28,9 @@
from opentelemetry.sdk.resources import Resource


__all__ = ['RSysLogHandler', 'SpecialInventoryHandler', 'ColorHandler']


class RSysLogHandler(logging.handlers.SysLogHandler):
append_nul = False

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit d393b79

Please sign in to comment.