Skip to content

CircuitPython Logger fixes #3065

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions CircuitPython_Logger/.circuitpython.skip

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,21 @@
"""

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):

def __init__(self, name, portal_device):
def __init__(self, name, portal_device, level: int = NOTSET):
"""Create an instance."""
self._log_feed_name=f"{name}-logging"
super().__init__(level)
self._log_feed_name = f"{name}-logging"
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


def emit(self, record):
"""Generate the message and write it to the AIO Feed.

Expand Down
43 changes: 21 additions & 22 deletions CircuitPython_Logger/aio_test/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,26 @@
from aio_handler import AIOHandler
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():
while True:
t = random.randint(1, 5)
if t == 1:
print('debug')
l.debug("debug message: %d", random.randint(0, 1000))
elif t == 2:
print('info')
l.info("info message: %d", random.randint(0, 1000))
elif t == 3:
print('warning')
l.warning("warning message: %d", random.randint(0, 1000))
elif t == 4:
print('error')
l.error("error message: %d", random.randint(0, 1000))
elif t == 5:
print('critical')
l.critical("critical message: %d", random.randint(0, 1000))
time.sleep(5.0 + (random.random() * 5.0))
while True:
t = random.randint(1, 5)
if t == 1:
print("debug")
l.debug("debug message: %d", random.randint(0, 1000))
elif t == 2:
print("info")
l.info("info message: %d", random.randint(0, 1000))
elif t == 3:
print("warning")
l.warning("warning message: %d", random.randint(0, 1000))
elif t == 4:
print("error")
l.error("error message: %d", random.randint(0, 1000))
elif t == 5:
print("critical")
l.critical("critical message: %d", random.randint(0, 1000))
time.sleep(5.0 + (random.random() * 5.0))
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,39 @@
"""


from adafruit_logging import Handler
from adafruit_ble.uart import UARTServer
from adafruit_logging import Handler, NOTSET

from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.nordic import UARTService


class BLEHandler(Handler):
"""Send logging output to the BLE uart port."""

def __init__(self):
def __init__(self, level: int = NOTSET):
"""Create an instance.

:param uart: the busio.UART instance to which to write messages
"""
super().__init__(level)
self._advertising_now = False
self._uart = UARTServer()
self._uart.start_advertising()
ble = BLERadio()
self._uart = UARTService()
self._advertisement = ProvideServicesAdvertisement(self._uart)
ble.start_advertising(self._advertisement)

def format(self, record):
"""Generate a string to log.

: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):
"""Generate the message and write it to the UART.

:param record: The record (message object) to be logged
"""
while not self._uart.connected:
pass
data = bytes(self.format(record), 'utf-8')
data = bytes(self.format(record), "utf-8")
self._uart.write(data)
39 changes: 19 additions & 20 deletions CircuitPython_Logger/ble_test/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,25 @@
from ble_handler import BLEHandler
import adafruit_logging as logging

l = logging.getLogger('ble')
l = logging.getLogger("ble")

l.addHandler(BLEHandler())

def go():
while True:
t = random.randint(1, 5)
if t == 1:
print('debug')
l.debug("%d", random.randint(0, 1000))
elif t == 2:
print('info')
l.info("%d", random.randint(0, 1000))
elif t == 3:
print('warning')
l.warning("%d", random.randint(0, 1000))
elif t == 4:
print('error')
l.error("%d", random.randint(0, 1000))
elif t == 5:
print('critical')
l.critical(" %d", random.randint(0, 1000))
time.sleep(5.0 + (random.random() * 5.0))
while True:
t = random.randint(1, 5)
if t == 1:
print("debug")
l.debug("%d", random.randint(0, 1000))
elif t == 2:
print("info")
l.info("%d", random.randint(0, 1000))
elif t == 3:
print("warning")
l.warning("%d", random.randint(0, 1000))
elif t == 4:
print("error")
l.error("%d", random.randint(0, 1000))
elif t == 5:
print("critical")
l.critical(" %d", random.randint(0, 1000))
time.sleep(5.0 + (random.random() * 5.0))
41 changes: 20 additions & 21 deletions CircuitPython_Logger/file_test/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,24 @@
vfs = storage.VfsFat(sdcard)
storage.mount(vfs, "/sd")

l = logging.getLogger('file')
l.addHandler(logging.FileHandler('/sd/test.txt'))
l = logging.getLogger("file")
l.addHandler(logging.FileHandler("/sd/test.txt"))

def go():
while True:
t = random.randint(1, 5)
if t == 1:
print('debug')
l.debug("debug message: %d", random.randint(0, 1000))
elif t == 2:
print('info')
l.info("info message: %d", random.randint(0, 1000))
elif t == 3:
print('warning')
l.warning("warning message: %d", random.randint(0, 1000))
elif t == 4:
print('error')
l.error("error message: %d", random.randint(0, 1000))
elif t == 5:
print('critical')
l.critical("critical message: %d", random.randint(0, 1000))
time.sleep(5.0 + (random.random() * 5.0))
while True:
t = random.randint(1, 5)
if t == 1:
print("debug")
l.debug("debug message: %d", random.randint(0, 1000))
elif t == 2:
print("info")
l.info("info message: %d", random.randint(0, 1000))
elif t == 3:
print("warning")
l.warning("warning message: %d", random.randint(0, 1000))
elif t == 4:
print("error")
l.error("error message: %d", random.randint(0, 1000))
elif t == 5:
print("critical")
l.critical("critical message: %d", random.randint(0, 1000))
time.sleep(5.0 + (random.random() * 5.0))
64 changes: 10 additions & 54 deletions CircuitPython_Logger/uart_handler/code.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,13 @@
# 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.
"""


# 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'))
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("test")
logger.addHandler(UartHandler(uart))
logger.setLevel(logging.INFO)
logger.info("testing")
46 changes: 46 additions & 0 deletions CircuitPython_Logger/uart_handler/uart_handler.py
Original file line number Diff line number Diff line change
@@ -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"))