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 May 15, 2024
1 parent 946bbe3 commit bb22453
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 @@ -3,6 +3,7 @@

# Python
import logging
import logging.handlers
import sys
import traceback
from datetime import datetime
Expand All @@ -16,6 +17,9 @@
from awx.main.exceptions import PostRunError


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


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

Expand Down Expand Up @@ -98,38 +102,34 @@ 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

0 comments on commit bb22453

Please sign in to comment.