Skip to content

Commit 5cce33a

Browse files
author
Ivaylo Korakov
committed
Colors :)
1 parent 44a3312 commit 5cce33a

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

kubi_ecs_logger/models/severity.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,19 @@ class Severity(OrderedEnum):
2929
WARNING = 3
3030
INFO = 2
3131
DEBUG = 1
32+
33+
@staticmethod
34+
def from_str(severity_str: str) -> 'Severity':
35+
severity_str = severity_str.lower()
36+
if severity_str == "debug":
37+
return Severity.DEBUG
38+
elif severity_str == "info":
39+
return Severity.INFO
40+
elif severity_str == "warning":
41+
return Severity.WARNING
42+
elif severity_str == "error":
43+
return Severity.ERROR
44+
elif severity_str == "critical":
45+
return Severity.CRITICAL
46+
else:
47+
raise AssertionError(f"Invalid severity - {severity_str}")

kubi_ecs_logger/utils.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,35 @@
11
import json
22
import sys
33

4+
from kubi_ecs_logger.models import Severity
5+
46

57
def pprint(data: dict, output_destination=sys.stdout):
6-
print(json.dumps(data, indent=2, sort_keys=True), file=output_destination)
8+
"""
9+
Prints the data in a formatted way with colors
10+
"""
11+
severity = Severity.from_str(data["logline"]["level"])
12+
raw_data = ConsoleColors.get_color(severity) + json.dumps(data, indent=2, sort_keys=True) + ConsoleColors.ENDC
13+
print(raw_data, file=output_destination)
14+
15+
16+
class ConsoleColors:
17+
GREY = '\033[37m'
18+
YELLOW = '\033[93m'
19+
CYAN = '\033[96m'
20+
RED = '\033[91m'
21+
DARK_RED = '\033[31m'
22+
ENDC = '\033[0m'
23+
24+
@staticmethod
25+
def get_color(severity: Severity):
26+
if severity == Severity.DEBUG:
27+
return ConsoleColors.GREY
28+
if severity == Severity.INFO:
29+
return ConsoleColors.CYAN
30+
if severity == Severity.WARNING:
31+
return ConsoleColors.YELLOW
32+
if severity == Severity.ERROR:
33+
return ConsoleColors.RED
34+
if severity == Severity.CRITICAL:
35+
return ConsoleColors.DARK_RED

kubi_ecs_logger/wrapper/logger.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,9 @@ def __get_defaults_for(self, obj: type) -> Optional[dict]:
356356
return None
357357

358358
def __output(self):
359+
"""
360+
The internal function that handles/passes on the printing
361+
"""
359362
if self._dev:
360363
pprint(BaseSchema().dump(self._base).data, output_destination=sys.stdout)
361364
else:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
# For a discussion on single-sourcing the version across setup.py and the
4747
# project code, see
4848
# https://packaging.python.org/en/latest/single_source_version.html
49-
version='0.0.5', # Required
49+
version='0.0.6', # Required
5050

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

0 commit comments

Comments
 (0)