Skip to content

Commit c7e5a3f

Browse files
committed
refactor(logging): Type annotations
1 parent a748ea8 commit c7e5a3f

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

tmuxp/log.py

+27-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
"""
88
import logging
99
import time
10+
import typing as t
11+
from typing import overload
1012

1113
from colorama import Fore, Style
1214

@@ -56,7 +58,21 @@ def set_style(
5658
return prefix + message + suffix
5759

5860

59-
def default_log_template(self, record, stylized=False):
61+
class LogTemplateFn(t.Protocol):
62+
def __call__(
63+
self,
64+
record: logging.LogRecord,
65+
stylized: t.Optional[bool],
66+
**kwargs: t.Any,
67+
) -> str:
68+
...
69+
70+
71+
def default_log_template(
72+
record: logging.LogRecord,
73+
stylized: t.Optional[bool] = False,
74+
**kwargs: t.Any,
75+
) -> str:
6076
"""
6177
Return the prefix for the log message. Template for Formatter.
6278
@@ -76,7 +92,7 @@ def default_log_template(self, record, stylized=False):
7692
levelname = set_style(
7793
"(%(levelname)s)",
7894
stylized,
79-
style_before=(LEVEL_COLORS.get(record.levelname) + Style.BRIGHT),
95+
style_before=(LEVEL_COLORS.get(record.levelname, "") + Style.BRIGHT),
8096
style_after=Style.RESET_ALL,
8197
suffix=" ",
8298
)
@@ -104,7 +120,7 @@ def default_log_template(self, record, stylized=False):
104120

105121

106122
class LogFormatter(logging.Formatter):
107-
template = default_log_template
123+
template: LogTemplateFn = default_log_template
108124

109125
def __init__(self, color=True, *args, **kwargs):
110126
logging.Formatter.__init__(self, *args, **kwargs)
@@ -125,7 +141,12 @@ def format(self, record):
125141
return formatted.replace("\n", "\n" + parts[0] + " ")
126142

127143

128-
def debug_log_template(self, record):
144+
def debug_log_template(
145+
record: logging.LogRecord,
146+
stylized: t.Optional[bool] = False,
147+
**kwargs: t.Any,
148+
) -> str:
149+
129150
"""
130151
Return the prefix for the log message. Template for Formatter.
131152
@@ -143,7 +164,7 @@ def debug_log_template(self, record):
143164

144165
reset = Style.RESET_ALL
145166
levelname = (
146-
LEVEL_COLORS.get(record.levelname)
167+
LEVEL_COLORS.get(record.levelname, "")
147168
+ Style.BRIGHT
148169
+ "(%(levelname)1.1s)"
149170
+ Style.RESET_ALL
@@ -188,4 +209,4 @@ def debug_log_template(self, record):
188209
class DebugLogFormatter(LogFormatter):
189210
"""Provides greater technical details than standard log Formatter."""
190211

191-
template = debug_log_template
212+
template: LogTemplateFn = debug_log_template

0 commit comments

Comments
 (0)