Skip to content

Commit

Permalink
refactor!(logging): mypy updates
Browse files Browse the repository at this point in the history
  • Loading branch information
tony committed Aug 14, 2022
1 parent 98b029d commit 8ef49a7
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions tmuxp/log.py
Expand Up @@ -7,6 +7,7 @@
"""
import logging
import time
import typing as t

from colorama import Fore, Style

Expand Down Expand Up @@ -56,7 +57,22 @@ def set_style(
return prefix + message + suffix


def default_log_template(self, record, stylized=False):
class LogTemplateFn(t.Protocol):
def template(
self,
record: logging.LogRecord,
stylized: t.Optional[bool],
**kwargs: t.Any,
) -> str:
...


def default_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.
Expand All @@ -76,7 +92,7 @@ def default_log_template(self, record, stylized=False):
levelname = set_style(
"(%(levelname)s)",
stylized,
style_before=(LEVEL_COLORS.get(record.levelname) + Style.BRIGHT),
style_before=(LEVEL_COLORS.get(record.levelname, "") + Style.BRIGHT),
style_after=Style.RESET_ALL,
suffix=" ",
)
Expand All @@ -103,7 +119,7 @@ def default_log_template(self, record, stylized=False):
return levelname + asctime + name


class LogFormatter(logging.Formatter):
class LogFormatter(logging.Formatter, LogTemplateFn):
template = default_log_template

def __init__(self, color=True, *args, **kwargs):
Expand All @@ -125,7 +141,13 @@ def format(self, record):
return formatted.replace("\n", "\n" + parts[0] + " ")


def debug_log_template(self, record):
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.
Expand All @@ -143,7 +165,7 @@ def debug_log_template(self, record):

reset = Style.RESET_ALL
levelname = (
LEVEL_COLORS.get(record.levelname)
LEVEL_COLORS.get(record.levelname, "")
+ Style.BRIGHT
+ "(%(levelname)1.1s)"
+ Style.RESET_ALL
Expand Down

0 comments on commit 8ef49a7

Please sign in to comment.