Skip to content

Commit

Permalink
Colors :)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivaylo Korakov committed Dec 17, 2019
1 parent 44a3312 commit 5cce33a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
16 changes: 16 additions & 0 deletions kubi_ecs_logger/models/severity.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,19 @@ class Severity(OrderedEnum):
WARNING = 3
INFO = 2
DEBUG = 1

@staticmethod
def from_str(severity_str: str) -> 'Severity':
severity_str = severity_str.lower()
if severity_str == "debug":
return Severity.DEBUG
elif severity_str == "info":
return Severity.INFO
elif severity_str == "warning":
return Severity.WARNING
elif severity_str == "error":
return Severity.ERROR
elif severity_str == "critical":
return Severity.CRITICAL
else:
raise AssertionError(f"Invalid severity - {severity_str}")
31 changes: 30 additions & 1 deletion kubi_ecs_logger/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
import json
import sys

from kubi_ecs_logger.models import Severity


def pprint(data: dict, output_destination=sys.stdout):
print(json.dumps(data, indent=2, sort_keys=True), file=output_destination)
"""
Prints the data in a formatted way with colors
"""
severity = Severity.from_str(data["logline"]["level"])
raw_data = ConsoleColors.get_color(severity) + json.dumps(data, indent=2, sort_keys=True) + ConsoleColors.ENDC
print(raw_data, file=output_destination)


class ConsoleColors:
GREY = '\033[37m'
YELLOW = '\033[93m'
CYAN = '\033[96m'
RED = '\033[91m'
DARK_RED = '\033[31m'
ENDC = '\033[0m'

@staticmethod
def get_color(severity: Severity):
if severity == Severity.DEBUG:
return ConsoleColors.GREY
if severity == Severity.INFO:
return ConsoleColors.CYAN
if severity == Severity.WARNING:
return ConsoleColors.YELLOW
if severity == Severity.ERROR:
return ConsoleColors.RED
if severity == Severity.CRITICAL:
return ConsoleColors.DARK_RED
3 changes: 3 additions & 0 deletions kubi_ecs_logger/wrapper/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,9 @@ def __get_defaults_for(self, obj: type) -> Optional[dict]:
return None

def __output(self):
"""
The internal function that handles/passes on the printing
"""
if self._dev:
pprint(BaseSchema().dump(self._base).data, output_destination=sys.stdout)
else:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
# For a discussion on single-sourcing the version across setup.py and the
# project code, see
# https://packaging.python.org/en/latest/single_source_version.html
version='0.0.5', # Required
version='0.0.6', # Required

# This is a one-line description or tagline of what your project does. This
# corresponds to the "Summary" metadata field:
Expand Down

0 comments on commit 5cce33a

Please sign in to comment.