Skip to content

Commit cafd4a6

Browse files
committed
Colorize shell output based on log level
1 parent 7ad4ea1 commit cafd4a6

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"plyvel==1.0.5",
4545
"web3==4.4.1",
4646
"lahja==0.9.0",
47+
"termcolor>=1.1.0,<2.0.0",
4748
"uvloop==0.11.2;platform_system=='Linux' or platform_system=='Darwin'",
4849
"websockets==5.0.1",
4950
],

trinity/utils/logging.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
TraceLogger,
2929
)
3030

31+
from trinity.utils.shellart import (
32+
bold_red,
33+
bold_yellow,
34+
)
35+
3136
if TYPE_CHECKING:
3237
from multiprocessing import Queue # noqa: F401
3338

@@ -42,7 +47,13 @@ def __init__(self, fmt: str, datefmt: str) -> None:
4247

4348
def format(self, record: logging.LogRecord) -> str:
4449
record.shortname = record.name.split('.')[-1] # type: ignore
45-
return super().format(record)
50+
51+
if record.levelno >= logging.ERROR:
52+
return bold_red(super().format(record))
53+
elif record.levelno >= logging.WARNING:
54+
return bold_yellow(super().format(record))
55+
else:
56+
return super().format(record)
4657

4758

4859
class HasTraceLogger:

trinity/utils/shellart.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from termcolor import (
2+
colored
3+
)
4+
5+
6+
def bold_green(txt: str) -> str:
7+
return colored(txt, 'green', attrs=['bold'])
8+
9+
10+
def bold_red(txt: str) -> str:
11+
return colored(txt, 'red', attrs=['bold'])
12+
13+
14+
def bold_yellow(txt: str) -> str:
15+
return colored(txt, 'yellow', attrs=['bold'])
16+
17+
18+
def bold_white(txt: str) -> str:
19+
return colored(txt, 'white', attrs=['bold'])

0 commit comments

Comments
 (0)