Skip to content

Commit 5afd688

Browse files
committed
chore(log): Add typings
1 parent 307d856 commit 5afd688

File tree

1 file changed

+53
-56
lines changed

1 file changed

+53
-56
lines changed

src/tmuxp/log.py

+53-56
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ def setup_logger(
5353
def set_style(
5454
message: str,
5555
stylized: bool,
56-
style_before: t.Optional[str] = None,
57-
style_after: t.Optional[str] = None,
56+
style_before: str = "",
57+
style_after: str = "",
5858
prefix: str = "",
5959
suffix: str = "",
6060
) -> str:
@@ -64,60 +64,56 @@ def set_style(
6464
return prefix + message + suffix
6565

6666

67-
def default_log_template(
68-
self: t.Type[logging.Formatter],
69-
record: logging.LogRecord,
70-
stylized: t.Optional[bool] = False,
71-
**kwargs: t.Any,
72-
) -> str:
73-
"""
74-
Return the prefix for the log message. Template for Formatter.
75-
76-
Parameters
77-
----------
78-
:py:class:`logging.LogRecord` :
79-
object. this is passed in from inside the
80-
:py:meth:`logging.Formatter.format` record.
81-
82-
Returns
83-
-------
84-
str
85-
template for logger message
86-
"""
87-
88-
reset = Style.RESET_ALL
89-
levelname = set_style(
90-
"(%(levelname)s)",
91-
stylized,
92-
style_before=(LEVEL_COLORS.get(record.levelname, "") + Style.BRIGHT),
93-
style_after=Style.RESET_ALL,
94-
suffix=" ",
95-
)
96-
asctime = set_style(
97-
"%(asctime)s",
98-
stylized,
99-
style_before=(Fore.BLACK + Style.DIM + Style.BRIGHT),
100-
style_after=(Fore.RESET + Style.RESET_ALL),
101-
prefix="[",
102-
suffix="]",
103-
)
104-
name = set_style(
105-
"%(name)s",
106-
stylized,
107-
style_before=(Fore.WHITE + Style.DIM + Style.BRIGHT),
108-
style_after=(Fore.RESET + Style.RESET_ALL),
109-
prefix=" ",
110-
suffix=" ",
111-
)
112-
113-
if stylized:
114-
return reset + levelname + asctime + name + reset
115-
116-
return levelname + asctime + name
117-
118-
11967
class LogFormatter(logging.Formatter):
120-
template = default_log_template
68+
def template(
69+
self: logging.Formatter,
70+
record: logging.LogRecord,
71+
stylized: bool = False,
72+
**kwargs: t.Any,
73+
) -> str:
74+
"""
75+
Return the prefix for the log message. Template for Formatter.
76+
77+
Parameters
78+
----------
79+
:py:class:`logging.LogRecord` :
80+
object. this is passed in from inside the
81+
:py:meth:`logging.Formatter.format` record.
82+
83+
Returns
84+
-------
85+
str
86+
template for logger message
87+
"""
88+
reset = Style.RESET_ALL
89+
levelname = set_style(
90+
"(%(levelname)s)",
91+
stylized,
92+
style_before=(LEVEL_COLORS.get(record.levelname, "") + Style.BRIGHT),
93+
style_after=Style.RESET_ALL,
94+
suffix=" ",
95+
)
96+
asctime = set_style(
97+
"%(asctime)s",
98+
stylized,
99+
style_before=(Fore.BLACK + Style.DIM + Style.BRIGHT),
100+
style_after=(Fore.RESET + Style.RESET_ALL),
101+
prefix="[",
102+
suffix="]",
103+
)
104+
name = set_style(
105+
"%(name)s",
106+
stylized,
107+
style_before=(Fore.WHITE + Style.DIM + Style.BRIGHT),
108+
style_after=(Fore.RESET + Style.RESET_ALL),
109+
prefix=" ",
110+
suffix=" ",
111+
)
112+
113+
if stylized:
114+
return reset + levelname + asctime + name + reset
115+
116+
return levelname + asctime + name
121117

122118
def __init__(self, color: bool = True, *args, **kwargs) -> None:
123119
logging.Formatter.__init__(self, *args, **kwargs)
@@ -129,7 +125,8 @@ def format(self, record: logging.LogRecord) -> str:
129125
record.message = f"Bad message ({e!r}): {record.__dict__!r}"
130126

131127
date_format = "%H:%m:%S"
132-
record.asctime = time.strftime(date_format, self.converter(record.created))
128+
formatting = self.converter(record.created) # type:ignore
129+
record.asctime = time.strftime(date_format, formatting)
133130

134131
prefix = self.template(record) % record.__dict__
135132

0 commit comments

Comments
 (0)