-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathlog.py
More file actions
298 lines (248 loc) · 8.07 KB
/
Copy pathlog.py
File metadata and controls
298 lines (248 loc) · 8.07 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/env python
"""Log utilities for tmuxp."""
from __future__ import annotations
import logging
import sys
import time
import typing as t
from tmuxp._internal.colors import _ansi_colors, _ansi_reset_all
logger = logging.getLogger(__name__)
_ANSI_RESET = _ansi_reset_all # "\033[0m"
_ANSI_BRIGHT = "\033[1m"
_ANSI_FG_RESET = "\033[39m"
LEVEL_COLORS = {
"DEBUG": f"\033[{_ansi_colors['blue']}m",
"INFO": f"\033[{_ansi_colors['green']}m",
"WARNING": f"\033[{_ansi_colors['yellow']}m",
"ERROR": f"\033[{_ansi_colors['red']}m",
"CRITICAL": f"\033[{_ansi_colors['red']}m",
}
class TmuxpLoggerAdapter(logging.LoggerAdapter): # type: ignore[type-arg]
"""LoggerAdapter that merges extra dictionary on Python < 3.13.
Follows the portable pattern to avoid repeating the same `extra` on every call
while preserving the ability to add per-call `extra` kwargs.
Examples
--------
>>> adapter = TmuxpLoggerAdapter(
... logging.getLogger("test"),
... {"tmux_session": "my-session"},
... )
>>> msg, kwargs = adapter.process("hello %s", {"extra": {"tmux_window": "editor"}})
>>> msg
'hello %s'
>>> kwargs["extra"]["tmux_session"]
'my-session'
>>> kwargs["extra"]["tmux_window"]
'editor'
"""
def process(
self, msg: t.Any, kwargs: t.MutableMapping[str, t.Any]
) -> tuple[t.Any, t.MutableMapping[str, t.Any]]:
"""Merge extra dictionary on Python < 3.13."""
extra = dict(self.extra) if self.extra else {}
if "extra" in kwargs:
extra.update(kwargs["extra"])
kwargs["extra"] = extra
return msg, kwargs
def setup_logger(
logger: logging.Logger | None = 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("tmuxp")
has_handlers = any(not isinstance(h, logging.NullHandler) for h in logger.handlers)
if not has_handlers: # setup logger handlers
channel = logging.StreamHandler()
formatter = DebugLogFormatter() if level == "DEBUG" else LogFormatter()
channel.setFormatter(formatter)
logger.addHandler(channel)
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
----------
record : :py:class:`logging.LogRecord`
Object passed from :py:meth:`logging.Formatter.format`.
Returns
-------
str
Template for logger message.
"""
reset = _ANSI_RESET
levelname = set_style(
"(%(levelname)s)",
stylized,
style_before=(LEVEL_COLORS.get(record.levelname, "") + _ANSI_BRIGHT),
style_after=_ANSI_RESET,
suffix=" ",
)
asctime = set_style(
"%(asctime)s",
stylized,
style_before=(f"\033[{_ansi_colors['black']}m" + _ANSI_BRIGHT),
style_after=(_ANSI_FG_RESET + _ANSI_RESET),
prefix="[",
suffix="]",
)
name = set_style(
"%(name)s",
stylized,
style_before=(f"\033[{_ansi_colors['white']}m" + _ANSI_BRIGHT),
style_after=(_ANSI_FG_RESET + _ANSI_RESET),
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: type[logging.Formatter],
record: logging.LogRecord,
stylized: bool | None = False,
**kwargs: t.Any,
) -> str:
"""
Return the prefix for the log message. Template for Formatter.
Parameters
----------
record : :py:class:`logging.LogRecord`
Object passed from :py:meth:`logging.Formatter.format`.
Returns
-------
str
Log template.
"""
reset = _ANSI_RESET
levelname = (
LEVEL_COLORS.get(record.levelname, "")
+ _ANSI_BRIGHT
+ "(%(levelname)1.1s)"
+ _ANSI_RESET
+ " "
)
asctime = (
"["
+ f"\033[{_ansi_colors['black']}m"
+ _ANSI_BRIGHT
+ "%(asctime)s"
+ _ANSI_FG_RESET
+ _ANSI_RESET
+ "]"
)
name = (
" "
+ f"\033[{_ansi_colors['white']}m"
+ _ANSI_BRIGHT
+ "%(name)s"
+ _ANSI_FG_RESET
+ _ANSI_RESET
+ " "
)
module_funcName = (
f"\033[{_ansi_colors['green']}m" + _ANSI_BRIGHT + "%(module)s.%(funcName)s()"
)
lineno = (
f"\033[{_ansi_colors['black']}m"
+ _ANSI_BRIGHT
+ ":"
+ _ANSI_RESET
+ f"\033[{_ansi_colors['cyan']}m"
+ "%(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
def setup_log_file(log_file: str, level: str = "INFO") -> None:
"""Attach a file handler to the tmuxp logger.
Parameters
----------
log_file : str
Path to the log file.
level : str
Log level name (e.g. "DEBUG", "INFO"). Selects formatter and sets
handler filtering level.
Examples
--------
>>> import tempfile, os, logging
>>> f = tempfile.NamedTemporaryFile(suffix=".log", delete=False)
>>> f.close()
>>> setup_log_file(f.name, level="INFO")
>>> tmuxp_logger = logging.getLogger("tmuxp")
>>> tmuxp_logger.handlers = [
... h for h in tmuxp_logger.handlers if not isinstance(h, logging.FileHandler)
... ]
>>> os.unlink(f.name)
"""
handler = logging.FileHandler(log_file)
formatter = DebugLogFormatter() if level.upper() == "DEBUG" else LogFormatter()
handler.setFormatter(formatter)
handler_level = getattr(logging, level.upper())
handler.setLevel(handler_level)
tmuxp_logger = logging.getLogger("tmuxp")
tmuxp_logger.addHandler(handler)
if tmuxp_logger.level == logging.NOTSET or tmuxp_logger.level > handler_level:
tmuxp_logger.setLevel(handler_level)
def tmuxp_echo(
message: str | None = None,
file: t.TextIO | None = None,
) -> None:
"""Print user-facing CLI output.
Parameters
----------
message : str | None
Message to print. If None, does nothing.
file : t.TextIO | None
Output stream. Defaults to sys.stdout.
Examples
--------
>>> tmuxp_echo("Session loaded")
Session loaded
>>> tmuxp_echo("Warning message")
Warning message
"""
if message is None:
return
print(message, file=file or sys.stdout)