Skip to content

Commit c53e5dc

Browse files
prettify logs with collors
1 parent e335eaf commit c53e5dc

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

movai_core_shared/consts.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Dor Marcous ([email protected]) - 2022
88
"""
99
from os import getpid
10+
import logging
1011

1112
# system
1213
PID = getpid()
@@ -148,3 +149,20 @@
148149
METRICS_QUERY_HANDLER_MSG_TYPE = "metrics_query"
149150
NOTIFICATIONS_HANDLER_MSG_TYPE = "notifications"
150151
ALERTS_HANDLER_METRIC_TYPE = "alerts"
152+
153+
CALLBACK_STDOUT_COLORS = {
154+
logging.DEBUG: "\033[36m",
155+
logging.INFO: "\u001b[0m",
156+
logging.WARNING: "\x1b[33m",
157+
logging.ERROR: "\x1b[31;20m",
158+
logging.CRITICAL: "\x1b[41;1m",
159+
logging.WARN: "\x1b[33m",
160+
}
161+
162+
SPAWNER_STDOUT_COLORS = {
163+
logging.DEBUG: "\x1b[1;34m",
164+
logging.INFO: "\x1b[1;37m",
165+
logging.WARNING: "\x1b[1;33m",
166+
logging.ERROR: "\x1b[31;1m",
167+
logging.CRITICAL: "\x1b[41;1m",
168+
}

movai_core_shared/envvars.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
""" Compilation of necessary environment variables to push to the database """
22
import os
3-
from logging import NOTSET, INFO, DEBUG, WARNING, FATAL, CRITICAL, ERROR, getLevelName
3+
from logging import DEBUG, getLevelName
44
import socket
55
from .consts import (
66
MESSAGE_SERVER_HOST,

movai_core_shared/logger.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
PID,
3232
USER_LOG_TAG,
3333
CALLBACK_LOGGER,
34+
CALLBACK_STDOUT_COLORS,
35+
SPAWNER_STDOUT_COLORS
3436
)
3537
from movai_core_shared.envvars import (
3638
DEVICE_NAME,
@@ -70,17 +72,12 @@
7072
logging.getLogger('rosout').setLevel(MOVAI_CALLBACK_VERBOSITY_LEVEL)
7173

7274
class StdOutHandler(logging.StreamHandler):
73-
_COLORS = {
74-
logging.DEBUG: "\x1b[30;1m", # light black (gray)
75-
logging.INFO: "", # default (white)
76-
logging.WARNING: "\x1b[33;1m", # yellow
77-
logging.ERROR: "\x1b[31;1m", # red
78-
logging.CRITICAL: "\x1b[41;1m", # bright red
79-
}
75+
_COLORS = SPAWNER_STDOUT_COLORS
8076
_COLOR_RESET = "\u001b[0m"
8177

82-
def __init__(self, stream=None):
78+
def __init__(self, color=SPAWNER_STDOUT_COLORS, stream=None):
8379
super().__init__(stream)
80+
self._COLORS = color
8481

8582
def emit(self, record):
8683
try:
@@ -119,7 +116,8 @@ def emit(self, record):
119116
self.flush()
120117
except Exception:
121118
self.handleError(record)
122-
119+
logging.getLogger('rosout').addHandler(StdOutHandler(color=CALLBACK_STDOUT_COLORS, stream=sys.stdout))
120+
logging.getLogger('rosout').propagate = False
123121

124122
class RemoteHandler(logging.StreamHandler):
125123
"""

0 commit comments

Comments
 (0)