/
log.py
202 lines (170 loc) · 4.9 KB
/
log.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env python
"""Log utilities for tmuxp."""
import logging
import time
import typing as t
from colorama import Fore, Style
LEVEL_COLORS = {
"DEBUG": Fore.BLUE, # Blue
"INFO": Fore.GREEN, # Green
"WARNING": Fore.YELLOW,
"ERROR": Fore.RED,
"CRITICAL": Fore.RED,
}
LOG_LEVELS = {
"CRITICAL": 50,
"ERROR": 40,
"WARNING": 30,
"INFO": 20,
"DEBUG": 10,
"NOTSET": 0,
}
def setup_logger(
logger: t.Optional[logging.Logger] = None,
level: str = "INFO",
) -> None:
"""Configure tmuxp's logging for CLI use.
Can checks for any existing loggers to prevent loading handlers twice.
Parameters
----------
logger : :py:class:`Logger`
logger instance for tmuxp
"""
if not logger: # if no logger exists, make one
logger = logging.getLogger()
if not logger.handlers: # setup logger handlers
logger.setLevel(level)
def set_style(
message: str,
stylized: bool,
style_before: str = "",
style_after: str = "",
prefix: str = "",
suffix: str = "",
) -> str:
"""Stylize terminal logging output."""
if stylized:
return prefix + style_before + message + style_after + suffix
return prefix + message + suffix
class LogFormatter(logging.Formatter):
"""Format logs for tmuxp."""
def template(
self: logging.Formatter,
record: logging.LogRecord,
stylized: bool = False,
**kwargs: t.Any,
) -> str:
"""
Return the prefix for the log message. Template for Formatter.
Parameters
----------
:py:class:`logging.LogRecord` :
object. this is passed in from inside the
:py:meth:`logging.Formatter.format` record.
Returns
-------
str
template for logger message
"""
reset = Style.RESET_ALL
levelname = set_style(
"(%(levelname)s)",
stylized,
style_before=(LEVEL_COLORS.get(record.levelname, "") + Style.BRIGHT),
style_after=Style.RESET_ALL,
suffix=" ",
)
asctime = set_style(
"%(asctime)s",
stylized,
style_before=(Fore.BLACK + Style.DIM + Style.BRIGHT),
style_after=(Fore.RESET + Style.RESET_ALL),
prefix="[",
suffix="]",
)
name = set_style(
"%(name)s",
stylized,
style_before=(Fore.WHITE + Style.DIM + Style.BRIGHT),
style_after=(Fore.RESET + Style.RESET_ALL),
prefix=" ",
suffix=" ",
)
if stylized:
return reset + levelname + asctime + name + reset
return levelname + asctime + name
def __init__(self, color: bool = True, **kwargs: t.Any) -> None:
logging.Formatter.__init__(self, **kwargs)
def format(self, record: logging.LogRecord) -> str:
"""Format a log record."""
try:
record.message = record.getMessage()
except Exception as e:
record.message = f"Bad message ({e!r}): {record.__dict__!r}"
date_format = "%H:%m:%S"
formatting = self.converter(record.created)
record.asctime = time.strftime(date_format, formatting)
prefix = self.template(record) % record.__dict__
parts = prefix.split(record.message)
formatted = prefix + " " + record.message
return formatted.replace("\n", "\n" + parts[0] + " ")
def debug_log_template(
self: t.Type[logging.Formatter],
record: logging.LogRecord,
stylized: t.Optional[bool] = False,
**kwargs: t.Any,
) -> str:
"""
Return the prefix for the log message. Template for Formatter.
Parameters
----------
record : :py:class:`logging.LogRecord`
This is passed in from inside the :py:meth:`logging.Formatter.format`
record.
Returns
-------
str
Log template.
"""
reset = Style.RESET_ALL
levelname = (
LEVEL_COLORS.get(record.levelname, "")
+ Style.BRIGHT
+ "(%(levelname)1.1s)"
+ Style.RESET_ALL
+ " "
)
asctime = (
"["
+ Fore.BLACK
+ Style.DIM
+ Style.BRIGHT
+ "%(asctime)s"
+ Fore.RESET
+ Style.RESET_ALL
+ "]"
)
name = (
" "
+ Fore.WHITE
+ Style.DIM
+ Style.BRIGHT
+ "%(name)s"
+ Fore.RESET
+ Style.RESET_ALL
+ " "
)
module_funcName = Fore.GREEN + Style.BRIGHT + "%(module)s.%(funcName)s()"
lineno = (
Fore.BLACK
+ Style.DIM
+ Style.BRIGHT
+ ":"
+ Style.RESET_ALL
+ Fore.CYAN
+ "%(lineno)d"
)
return reset + levelname + asctime + name + module_funcName + lineno + reset
class DebugLogFormatter(LogFormatter):
"""Provides greater technical details than standard log Formatter."""
template = debug_log_template