Skip to content

Commit a31956f

Browse files
authored
Merge pull request #71 from jd/fix-ignore
chore: add custom LogRecord classes to avoid ignore
2 parents c3729a9 + ce03370 commit a31956f

File tree

4 files changed

+48
-21
lines changed

4 files changed

+48
-21
lines changed

daiquiri/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import logging.handlers
1616
import sys
1717
import traceback
18-
import types
18+
import types as _ptypes
1919
import typing
2020

2121
from daiquiri import output
@@ -112,7 +112,7 @@ def setup(
112112
def logging_excepthook(
113113
exc_type: typing.Optional[typing.Type[BaseException]],
114114
value: typing.Optional[BaseException],
115-
tb: typing.Optional[types.TracebackType],
115+
tb: typing.Optional[_ptypes.TracebackType],
116116
) -> None:
117117
program_logger.critical(
118118
"".join(traceback.format_exception(exc_type, value, tb))

daiquiri/formatter.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
from pythonjsonlogger import jsonlogger
1818

19+
from daiquiri import types
20+
1921

2022
DEFAULT_FORMAT = (
2123
"%(asctime)s [%(process)d] %(color)s%(levelname)-8.8s "
@@ -42,22 +44,23 @@ class ColorFormatter(logging.Formatter):
4244

4345
COLOR_STOP = "\033[0m"
4446

45-
def add_color(self, record: logging.LogRecord) -> None:
47+
def add_color(self, record: types.ColoredLogRecord) -> None:
4648
"""Add color to a record."""
4749
if getattr(record, "_stream_is_a_tty", False):
48-
record.color = self.LEVEL_COLORS[record.levelno] # type: ignore[attr-defined]
49-
record.color_stop = self.COLOR_STOP # type: ignore[attr-defined]
50+
record.color = self.LEVEL_COLORS[record.levelno]
51+
record.color_stop = self.COLOR_STOP
5052
else:
51-
record.color = "" # type: ignore[attr-defined]
52-
record.color_stop = "" # type: ignore[attr-defined]
53+
record.color = ""
54+
record.color_stop = ""
5355

54-
def remove_color(self, record: logging.LogRecord) -> None:
56+
def remove_color(self, record: types.ColoredLogRecord) -> None:
5557
"""Remove color from a record."""
56-
del record.color # type: ignore[attr-defined]
57-
del record.color_stop # type: ignore[attr-defined]
58+
del record.color
59+
del record.color_stop
5860

5961
def format(self, record: logging.LogRecord) -> str:
6062
"""Format a record."""
63+
record = typing.cast(types.ColoredLogRecord, record)
6164
self.add_color(record)
6265
s = super(ColorFormatter, self).format(record)
6366
self.remove_color(record)
@@ -118,24 +121,25 @@ def __init__(
118121
self.extras_suffix = extras_suffix
119122
super(ExtrasFormatter, self).__init__(*args, **kwargs)
120123

121-
def add_extras(self, record: logging.LogRecord) -> None:
124+
def add_extras(self, record: types.ExtrasLogRecord) -> None:
122125
if not hasattr(record, "_daiquiri_extra_keys"):
123-
record.extras = "" # type: ignore[attr-defined]
126+
record.extras = ""
124127
return
125128

126129
extras = self.extras_separator.join(
127130
self.extras_template.format(k, getattr(record, k))
128-
for k in record._daiquiri_extra_keys # type: ignore[attr-defined]
131+
for k in record._daiquiri_extra_keys
129132
if k != "_daiquiri_extra_keys" and k not in self.keywords
130133
)
131134
if extras != "":
132135
extras = self.extras_prefix + extras + self.extras_suffix
133-
record.extras = extras # type: ignore[attr-defined]
136+
record.extras = extras
134137

135-
def remove_extras(self, record: logging.LogRecord) -> None:
136-
del record.extras # type: ignore[attr-defined]
138+
def remove_extras(self, record: types.ExtrasLogRecord) -> None:
139+
del record.extras
137140

138141
def format(self, record: logging.LogRecord) -> str:
142+
record = typing.cast(types.ExtrasLogRecord, record)
139143
self.add_extras(record)
140144
s = super(ExtrasFormatter, self).format(record)
141145
self.remove_extras(record)
@@ -146,6 +150,7 @@ class ColorExtrasFormatter(ColorFormatter, ExtrasFormatter):
146150
"""Combines functionality of ColorFormatter and ExtrasFormatter."""
147151

148152
def format(self, record: logging.LogRecord) -> str:
153+
record = typing.cast(types.ColoredLogRecord, record)
149154
self.add_color(record)
150155
s = ExtrasFormatter.format(self, record)
151156
self.remove_color(record)

daiquiri/handlers.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
syslog = None # type: ignore[assignment]
2929

3030

31+
from daiquiri import types
32+
3133
# This is a copy of the numerical constants from syslog.h. The
3234
# definition of these goes back at least 20 years, and is specifically
3335
# 3 bits in a packed field, so these aren't likely to ever need
@@ -96,7 +98,8 @@ def emit(self, record: logging.LogRecord) -> None:
9698
extras["EXCEPTION_INFO"] = record.exc_info
9799

98100
if hasattr(record, "_daiquiri_extra_keys"):
99-
for k in record._daiquiri_extra_keys: # type: ignore[attr-defined]
101+
record = typing.cast(types.ExtrasLogRecord, record)
102+
for k in record._daiquiri_extra_keys:
100103
if k != "_daiquiri_extra_keys":
101104
extras[k.upper()] = getattr(record, k)
102105

@@ -113,16 +116,17 @@ class TTYDetectorStreamHandler(_TTYDetectorStreamHandlerBase):
113116
"""Stream handler that adds a hint in the record if the stream is a TTY."""
114117

115118
def format(self, record: logging.LogRecord) -> str:
119+
record = typing.cast(types.TTYDetectionLogRecord, record)
116120
if hasattr(self.stream, "isatty"):
117121
try:
118-
record._stream_is_a_tty = self.stream.isatty() # type: ignore[attr-defined]
122+
record._stream_is_a_tty = self.stream.isatty()
119123
except ValueError:
120124
# Stream has been closed, usually during interpretor shutdown
121-
record._stream_is_a_tty = False # type: ignore[attr-defined]
125+
record._stream_is_a_tty = False
122126
else:
123-
record._stream_is_a_tty = False # type: ignore[attr-defined]
127+
record._stream_is_a_tty = False
124128
s = super(TTYDetectorStreamHandler, self).format(record)
125-
del record._stream_is_a_tty # type: ignore[attr-defined]
129+
del record._stream_is_a_tty
126130
return s
127131

128132

daiquiri/types.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import logging
2+
import typing
3+
4+
5+
class ColoredLogRecord(logging.LogRecord):
6+
color: str
7+
color_stop: str
8+
9+
10+
class ExtrasLogRecord(logging.LogRecord):
11+
extras_prefix: str
12+
extras_suffix: str
13+
extras: str
14+
_daiquiri_extra_keys: typing.Set[str]
15+
16+
17+
class TTYDetectionLogRecord(logging.LogRecord):
18+
_stream_is_a_tty: bool

0 commit comments

Comments
 (0)