Skip to content

Commit

Permalink
adding Formatter and example including timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
FoamyGuy committed Aug 5, 2024
1 parent cbfea81 commit f7dc544
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
68 changes: 67 additions & 1 deletion adafruit_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,71 @@ def _logRecordFactory(name, level, msg, args):
return LogRecord(name, level, _level_for(level), msg, time.monotonic(), args)


class Formatter:
"""
Responsible for converting a LogRecord to an output string to be
interpreted by a human or external system.
Only implements a sub-set of CPython logging.Formatter behavior,
but retains all the same arguments in order to match the API.
The only init arguments currently supported are: fmt and defaults.
All others are currently ignored
The only style value currently supported is '{'. CPython has support
for some others, but this implementation does not. Additionally, the
default value for style in this implementation is '{' whereas the default
style value in CPython is '%'
"""

def __init__( # pylint: disable=too-many-arguments
self, fmt=None, datefmt=None, style="{", validate=True, defaults=None
):
self.fmt = fmt
self.datefmt = datefmt
self.style = style
if self.style != "{":
raise ValueError("Only '{' formatting sytle is supported at this time.")

self.validate = validate
self.defaults = defaults

def format(self, record: LogRecord) -> str:
"""
Format the given LogRecord into an output string
"""
if self.fmt is None:
return record.msg

vals = {
"name": record.name,
"levelno": record.levelno,
"levelname": record.levelname,
"message": record.msg,
"created": record.created,
"args": record.args,
}
if "{asctime}" in self.fmt:
now = time.localtime()
vals["asctime"] = (
f"{now.tm_year}-{now.tm_mon:02d}-{now.tm_mday:02d} "
f"{now.tm_hour:02d}:{now.tm_min:02d}:{now.tm_sec:02d}"
)

if self.defaults:
for key, val in self.defaults.items():
vals[key] = val

return self.fmt.format(**vals)


class Handler:
"""Base logging message handler."""

def __init__(self, level: int = NOTSET) -> None:
"""Create Handler instance"""
self.level = level
self.formatter = None

def setLevel(self, level: int) -> None:
"""
Expand All @@ -167,7 +226,8 @@ def format(self, record: LogRecord) -> str:
:param record: The record (message object) to be logged
"""

if self.formatter:
return self.formatter.format(record)
return f"{record.created:<0.3f}: {record.levelname} - {record.msg}"

def emit(self, record: LogRecord) -> None:
Expand All @@ -182,6 +242,12 @@ def emit(self, record: LogRecord) -> None:
def flush(self) -> None:
"""Placeholder for flush function in subclasses."""

def setFormatter(self, formatter: Formatter) -> None:
"""
Set the Formatter to be used by this Handler.
"""
self.formatter = formatter


# pylint: disable=too-few-public-methods
class StreamHandler(Handler):
Expand Down
33 changes: 33 additions & 0 deletions examples/logging_formatter_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# SPDX-FileCopyrightText: 2024 Tim Cocks

Check failure on line 1 in examples/logging_formatter_example.py

View workflow job for this annotation

GitHub Actions / test

reformatted
# SPDX-License-Identifier: MIT


"""Briefly exercise the logger and null logger."""

import adafruit_logging as logging
# To test on CPython, un-comment below and comment out above
# import logging


logger = logging.getLogger("example")
logger.setLevel(logging.INFO)
print_handler = logging.StreamHandler()
logger.addHandler(print_handler)

default_formatter = logging.Formatter()
print_handler.setFormatter(default_formatter)
logger.info("Default formatter example")


timestamp_formatter = logging.Formatter(
fmt="{asctime} {levelname}: {message}", style="{"
)
print_handler.setFormatter(timestamp_formatter)
logger.info("Timestamp formatter example")


custom_vals_formatter = logging.Formatter(
fmt="{ip} {levelname}: {message}", style="{", defaults={"ip": "192.168.1.188"}
)
print_handler.setFormatter(custom_vals_formatter)
logger.info("Custom formatter example")

0 comments on commit f7dc544

Please sign in to comment.