Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix environment-specific rough edges of logging setup #15193

Open
wants to merge 1 commit into
base: devel
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading