Skip to content

Commit ef8c237

Browse files
committed
implement % sytle and change default to it
1 parent 555981a commit ef8c237

File tree

2 files changed

+41
-15
lines changed

2 files changed

+41
-15
lines changed

adafruit_logging.py

+24-11
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464

6565
try:
6666
# pylint: disable=deprecated-class
67-
from typing import Optional, Hashable
67+
from typing import Optional, Hashable, Dict
6868
from typing_extensions import Protocol
6969

7070
class WriteableStream(Protocol):
@@ -156,23 +156,28 @@ class Formatter:
156156
Only implements a sub-set of CPython logging.Formatter behavior,
157157
but retains all the same arguments in order to match the API.
158158
159-
The only init arguments currently supported are: fmt and defaults.
160-
All others are currently ignored
159+
The only init arguments currently supported are: fmt, defaults and
160+
style. All others are currently ignored
161161
162-
The only style value currently supported is '{'. CPython has support
163-
for some others, but this implementation does not. Additionally, the
164-
default value for style in this implementation is '{' whereas the default
165-
style value in CPython is '%'
162+
The only two styles currently supported are '%' and '{'. The default
163+
style is '{'
166164
"""
167165

168166
def __init__( # pylint: disable=too-many-arguments
169-
self, fmt=None, datefmt=None, style="{", validate=True, defaults=None
167+
self,
168+
fmt: Optional[str] = None,
169+
datefmt: Optional[str] = None,
170+
style: str = "%",
171+
validate: bool = True,
172+
defaults: Dict = None,
170173
):
171174
self.fmt = fmt
172175
self.datefmt = datefmt
173176
self.style = style
174-
if self.style != "{":
175-
raise ValueError("Only '{' formatting sytle is supported at this time.")
177+
if self.style not in ("{", "%"):
178+
raise ValueError(
179+
"Only '%' and '{' formatting style are supported at this time."
180+
)
176181

177182
self.validate = validate
178183
self.defaults = defaults
@@ -192,7 +197,7 @@ def format(self, record: LogRecord) -> str:
192197
"created": record.created,
193198
"args": record.args,
194199
}
195-
if "{asctime}" in self.fmt:
200+
if "{asctime}" in self.fmt or "%(asctime)s" in self.fmt:
196201
now = time.localtime()
197202
# pylint: disable=line-too-long
198203
vals[
@@ -203,6 +208,14 @@ def format(self, record: LogRecord) -> str:
203208
for key, val in self.defaults.items():
204209
vals[key] = val
205210

211+
if self.style not in ("{", "%"):
212+
raise ValueError(
213+
"Only '%' and '{' formatting style are supported at this time."
214+
)
215+
216+
if self.style == "%":
217+
return self.fmt % vals
218+
206219
return self.fmt.format(**vals)
207220

208221

examples/logging_formatter_example.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,32 @@
1717
logger.addHandler(print_handler)
1818

1919
default_formatter = logging.Formatter()
20+
2021
print_handler.setFormatter(default_formatter)
2122
logger.info("Default formatter example")
2223

2324

24-
timestamp_formatter = logging.Formatter(
25-
fmt="{asctime} {levelname}: {message}", style="{"
26-
)
25+
timestamp_formatter = logging.Formatter(fmt="%(asctime)s %(levelname)s: %(message)s")
2726
print_handler.setFormatter(timestamp_formatter)
2827
logger.info("Timestamp formatter example")
2928

3029

3130
custom_vals_formatter = logging.Formatter(
32-
fmt="{ip} {levelname}: {message}", style="{", defaults={"ip": "192.168.1.188"}
31+
fmt="%(ip)s %(levelname)s: %(message)s", defaults={"ip": "192.168.1.188"}
3332
)
3433
print_handler.setFormatter(custom_vals_formatter)
3534
logger.info("Custom formatter example")
35+
36+
37+
bracket_timestamp_formatter = logging.Formatter(
38+
fmt="{asctime} {levelname}: {message}", style="{"
39+
)
40+
print_handler.setFormatter(bracket_timestamp_formatter)
41+
logger.info("Timestamp formatter bracket style example")
42+
43+
44+
bracket_custom_vals_formatter = logging.Formatter(
45+
fmt="{ip} {levelname}: {message}", style="{", defaults={"ip": "192.168.1.188"}
46+
)
47+
print_handler.setFormatter(bracket_custom_vals_formatter)
48+
logger.info("Custom formatter bracket style example")

0 commit comments

Comments
 (0)