-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.py
94 lines (79 loc) · 3.17 KB
/
Logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from babel.numbers import format_decimal
from rich.console import Console
from rich.highlighter import JSONHighlighter
from rich.logging import RichHandler
from rich.theme import Theme
import locale
import json
import logging
class Logger:
SUBPROC = 25 # Between INFO (20) and WARNING (30)
HELP = 15 # Between DEBUG (10) and INFO (20)
def __init__(self):
self.user_locale = locale.getlocale()[0] # Get the user's locale
console = Console(
stderr=True,
theme=Theme(
{
"logging.level.subproc": "bold blue",
"logging.level.help": "bold green",
}
),
)
logging.basicConfig(
level=logging.NOTSET,
format="%(message)s",
datefmt="[%X]",
handlers=[RichHandler(console=console)],
)
self.logger = logging.getLogger(__name__)
logging.addLevelName(self.SUBPROC, "SUBPROC")
logging.addLevelName(self.HELP, "HELP")
def format_numbers(self, message):
if isinstance(message, str):
lines = message.splitlines()
for i, line in enumerate(lines):
words = line.split()
for j, word in enumerate(words):
try:
# Try to convert the word to a float
num = float(word)
# If successful, format the number and replace the word
words[j] = format_decimal(num, locale=self.user_locale)
except ValueError:
# If the word can't be converted to a float, ignore it
pass
# Join the words back into a single string
lines[i] = " ".join(words)
# Join the lines back into a single string
message = "\n".join(lines)
elif isinstance(message, int):
message = format_decimal(message, locale=self.user_locale)
return message
def info(self, message):
message = self.format_numbers(message)
self.logger.info(message)
def debug(self, message):
message = self.format_numbers(message)
self.logger.debug(message)
def warn(self, message):
message = self.format_numbers(message)
self.logger.warning(message)
def error(self, message):
message = self.format_numbers(message)
self.logger.error(message)
def subproc(self, message, *args, **kwargs):
message = self.format_numbers(message)
if not message: # Check if the message is empty
message = "No errors reported"
if self.logger.isEnabledFor(self.SUBPROC):
self.logger._log(self.SUBPROC, message, args, **kwargs)
def help(self, message, *args, **kwargs):
message = self.format_numbers(message)
if not message:
message = "No help available"
if self.logger.isEnabledFor(self.HELP):
self.logger._log(self.HELP, message, args, **kwargs)
def json(self, data):
json_str = json.dumps(data, indent=4)
self.logger.info(json_str, extra={"highlighter": JSONHighlighter()})