fix logger handler classes, remove skip file

This commit is contained in:
foamyguy 2025-06-20 09:22:05 -05:00
parent 82e3a2adca
commit 463ac6586b
8 changed files with 92 additions and 98 deletions

View file

@ -1,4 +0,0 @@
CircuitPython_Logger/ble_handler.py 16: Bad option value 'missing-super-argument' (bad-option-value)
CircuitPython_Logger/uart_handler.py 16: Bad option value 'missing-super-argument' (bad-option-value)
CircuitPython_Logger/file_handler.py 15: Bad option value 'missing-super-argument' (bad-option-value)
CircuitPython_Logger/aio_handler.py 15: Bad option value 'missing-super-argument' (bad-option-value)

View file

@ -17,30 +17,21 @@ All text above must be included in any redistribution.
""" """
from adafruit_portalbase import PortalBase from adafruit_portalbase import PortalBase
from adafruit_logging import Handler, NOTSET
# Example:
#
# from aio_handler import AIOHandler
# import adafruit_logging as logging
# l = logging.getLogger('aio')
# # Pass in the device object based on portal_base
# # (Funhouse, PyPortal, MagTag, etc) as the 2nd parameter
# l.addHandler(AIOHandler('test', portal_device))
# l.level = logging.ERROR
# l.error("test")
from adafruit_logging import Handler
class AIOHandler(Handler): class AIOHandler(Handler):
def __init__(self, name, portal_device): def __init__(self, name, portal_device, level: int = NOTSET):
"""Create an instance.""" """Create an instance."""
self._log_feed_name=f"{name}-logging" super().__init__(name)
self._log_feed_name = f"{name}-logging"
if not issubclass(type(portal_device), PortalBase): if not issubclass(type(portal_device), PortalBase):
raise TypeError("portal_device must be a PortalBase or subclass of PortalBase") raise TypeError(
"portal_device must be a PortalBase or subclass of PortalBase"
)
self._portal_device = portal_device self._portal_device = portal_device
def emit(self, record): def emit(self, record):
"""Generate the message and write it to the AIO Feed. """Generate the message and write it to the AIO Feed.

View file

@ -8,27 +8,28 @@ from adafruit_pyportal import PyPortal
from aio_handler import AIOHandler from aio_handler import AIOHandler
import adafruit_logging as logging import adafruit_logging as logging
device=PyPortal() device = PyPortal()
l = logging.getLogger("aio")
l.addHandler(AIOHandler("test", device))
l = logging.getLogger('aio')
l.addHandler(AIOHandler('test', device))
def go(): def go():
while True: while True:
t = random.randint(1, 5) t = random.randint(1, 5)
if t == 1: if t == 1:
print('debug') print("debug")
l.debug("debug message: %d", random.randint(0, 1000)) l.debug("debug message: %d", random.randint(0, 1000))
elif t == 2: elif t == 2:
print('info') print("info")
l.info("info message: %d", random.randint(0, 1000)) l.info("info message: %d", random.randint(0, 1000))
elif t == 3: elif t == 3:
print('warning') print("warning")
l.warning("warning message: %d", random.randint(0, 1000)) l.warning("warning message: %d", random.randint(0, 1000))
elif t == 4: elif t == 4:
print('error') print("error")
l.error("error message: %d", random.randint(0, 1000)) l.error("error message: %d", random.randint(0, 1000))
elif t == 5: elif t == 5:
print('critical') print("critical")
l.critical("critical message: %d", random.randint(0, 1000)) l.critical("critical message: %d", random.randint(0, 1000))
time.sleep(5.0 + (random.random() * 5.0)) time.sleep(5.0 + (random.random() * 5.0))

View file

@ -17,17 +17,19 @@ All text above must be included in any redistribution.
""" """
from adafruit_logging import Handler from adafruit_logging import Handler, NOTSET
from adafruit_ble.uart import UARTServer from adafruit_ble.uart import UARTServer
class BLEHandler(Handler): class BLEHandler(Handler):
"""Send logging output to the BLE uart port.""" """Send logging output to the BLE uart port."""
def __init__(self): def __init__(self, level: int = NOTSET):
"""Create an instance. """Create an instance.
:param uart: the busio.UART instance to which to write messages :param uart: the busio.UART instance to which to write messages
""" """
super().__init__(level)
self._advertising_now = False self._advertising_now = False
self._uart = UARTServer() self._uart = UARTServer()
self._uart.start_advertising() self._uart.start_advertising()
@ -37,7 +39,7 @@ class BLEHandler(Handler):
:param record: The record (message object) to be logged :param record: The record (message object) to be logged
""" """
return super().format(record) + '\r\n' return super().format(record) + "\r\n"
def emit(self, record): def emit(self, record):
"""Generate the message and write it to the UART. """Generate the message and write it to the UART.
@ -46,5 +48,5 @@ class BLEHandler(Handler):
""" """
while not self._uart.connected: while not self._uart.connected:
pass pass
data = bytes(self.format(record), 'utf-8') data = bytes(self.format(record), "utf-8")
self._uart.write(data) self._uart.write(data)

View file

@ -7,26 +7,27 @@ import random
from ble_handler import BLEHandler from ble_handler import BLEHandler
import adafruit_logging as logging import adafruit_logging as logging
l = logging.getLogger('ble') l = logging.getLogger("ble")
l.addHandler(BLEHandler()) l.addHandler(BLEHandler())
def go(): def go():
while True: while True:
t = random.randint(1, 5) t = random.randint(1, 5)
if t == 1: if t == 1:
print('debug') print("debug")
l.debug("%d", random.randint(0, 1000)) l.debug("%d", random.randint(0, 1000))
elif t == 2: elif t == 2:
print('info') print("info")
l.info("%d", random.randint(0, 1000)) l.info("%d", random.randint(0, 1000))
elif t == 3: elif t == 3:
print('warning') print("warning")
l.warning("%d", random.randint(0, 1000)) l.warning("%d", random.randint(0, 1000))
elif t == 4: elif t == 4:
print('error') print("error")
l.error("%d", random.randint(0, 1000)) l.error("%d", random.randint(0, 1000))
elif t == 5: elif t == 5:
print('critical') print("critical")
l.critical(" %d", random.randint(0, 1000)) l.critical(" %d", random.randint(0, 1000))
time.sleep(5.0 + (random.random() * 5.0)) time.sleep(5.0 + (random.random() * 5.0))

View file

@ -21,25 +21,26 @@ sdcard = adafruit_sdcard.SDCard(spi, cs)
vfs = storage.VfsFat(sdcard) vfs = storage.VfsFat(sdcard)
storage.mount(vfs, "/sd") storage.mount(vfs, "/sd")
l = logging.getLogger('file') l = logging.getLogger("file")
l.addHandler(logging.FileHandler('/sd/test.txt')) l.addHandler(logging.FileHandler("/sd/test.txt"))
def go(): def go():
while True: while True:
t = random.randint(1, 5) t = random.randint(1, 5)
if t == 1: if t == 1:
print('debug') print("debug")
l.debug("debug message: %d", random.randint(0, 1000)) l.debug("debug message: %d", random.randint(0, 1000))
elif t == 2: elif t == 2:
print('info') print("info")
l.info("info message: %d", random.randint(0, 1000)) l.info("info message: %d", random.randint(0, 1000))
elif t == 3: elif t == 3:
print('warning') print("warning")
l.warning("warning message: %d", random.randint(0, 1000)) l.warning("warning message: %d", random.randint(0, 1000))
elif t == 4: elif t == 4:
print('error') print("error")
l.error("error message: %d", random.randint(0, 1000)) l.error("error message: %d", random.randint(0, 1000))
elif t == 5: elif t == 5:
print('critical') print("critical")
l.critical("critical message: %d", random.randint(0, 1000)) l.critical("critical message: %d", random.randint(0, 1000))
time.sleep(5.0 + (random.random() * 5.0)) time.sleep(5.0 + (random.random() * 5.0))

View file

@ -1,57 +1,13 @@
# SPDX-FileCopyrightText: 2018 Dave Astels for Adafruit Industries # SPDX-FileCopyrightText: 2018 Dave Astels for Adafruit Industries
# #
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
import board
import busio
from uart_handler import UartHandler
import adafruit_logging as logging
""" uart = busio.UART(board.TX, board.RX, baudrate=115200)
UART based message handler for CircuitPython logging. logger = logging.getLogger("test")
logger.addHandler(UartHandler(uart))
Adafruit invests time and resources providing this open source code. logger.setLevel(logging.INFO)
Please support Adafruit and open source hardware by purchasing logger.info("testing")
products from Adafruit!
Written by Dave Astels for Adafruit Industries
Copyright (c) 2018 Adafruit Industries
Licensed under the MIT license.
All text above must be included in any redistribution.
"""
# Example:
#
# import board
# import busio
# from uart_handler import UartHandler
# import adafruit_logging as logging
#
# uart = busio.UART(board.TX, board.RX, baudrate=115200)
# logger = logging.getLogger('uart')
# logger.addHandler(UartHandler(uart))
# logger.level = logging.INFO
# logger.info('testing')
from adafruit_logging import Handler
class UartHandler(Handler):
"""Send logging output to a serial port."""
def __init__(self, uart):
"""Create an instance.
:param uart: the busio.UART instance to which to write messages
"""
self._uart = uart
def format(self, record):
"""Generate a string to log.
:param record: The record (message object) to be logged
"""
return super().format(record) + '\r\n'
def emit(self, record):
"""Generate the message and write it to the UART.
:param record: The record (message object) to be logged
"""
self._uart.write(bytes(self.format(record), 'utf-8'))

View file

@ -0,0 +1,46 @@
# SPDX-FileCopyrightText: 2018 Dave Astels for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
UART based message handler for CircuitPython logging.
Adafruit invests time and resources providing this open source code.
Please support Adafruit and open source hardware by purchasing
products from Adafruit!
Written by Dave Astels for Adafruit Industries
Copyright (c) 2018 Adafruit Industries
Licensed under the MIT license.
All text above must be included in any redistribution.
"""
from adafruit_logging import Handler, NOTSET
class UartHandler(Handler):
"""Send logging output to a serial port."""
def __init__(self, uart, level: int = NOTSET):
"""Create an instance.
:param uart: the busio.UART instance to which to write messages
"""
super().__init__(level)
self._uart = uart
def format(self, record):
"""Generate a string to log.
:param record: The record (message object) to be logged
"""
return super().format(record) + "\r\n"
def emit(self, record):
"""Generate the message and write it to the UART.
:param record: The record (message object) to be logged
"""
self._uart.write(bytes(self.format(record), "utf-8"))