Skip to content

Commit 463ac65

Browse files
committed
fix logger handler classes, remove skip file
1 parent 82e3a2a commit 463ac65

File tree

8 files changed

+93
-99
lines changed

8 files changed

+93
-99
lines changed

CircuitPython_Logger/.circuitpython.skip

Lines changed: 0 additions & 4 deletions
This file was deleted.

CircuitPython_Logger/aio_handler/code.py renamed to CircuitPython_Logger/aio_test/aio_handler.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,21 @@
1717
"""
1818

1919
from adafruit_portalbase import PortalBase
20+
from adafruit_logging import Handler, NOTSET
2021

21-
# Example:
22-
#
23-
# from aio_handler import AIOHandler
24-
# import adafruit_logging as logging
25-
# l = logging.getLogger('aio')
26-
# # Pass in the device object based on portal_base
27-
# # (Funhouse, PyPortal, MagTag, etc) as the 2nd parameter
28-
# l.addHandler(AIOHandler('test', portal_device))
29-
# l.level = logging.ERROR
30-
# l.error("test")
31-
32-
from adafruit_logging import Handler
3322

3423
class AIOHandler(Handler):
3524

36-
def __init__(self, name, portal_device):
25+
def __init__(self, name, portal_device, level: int = NOTSET):
3726
"""Create an instance."""
38-
self._log_feed_name=f"{name}-logging"
27+
super().__init__(name)
28+
self._log_feed_name = f"{name}-logging"
3929
if not issubclass(type(portal_device), PortalBase):
40-
raise TypeError("portal_device must be a PortalBase or subclass of PortalBase")
30+
raise TypeError(
31+
"portal_device must be a PortalBase or subclass of PortalBase"
32+
)
4133
self._portal_device = portal_device
4234

43-
4435
def emit(self, record):
4536
"""Generate the message and write it to the AIO Feed.
4637

CircuitPython_Logger/aio_test/code.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,28 @@
88
from aio_handler import AIOHandler
99
import adafruit_logging as logging
1010

11-
device=PyPortal()
11+
device = PyPortal()
12+
13+
l = logging.getLogger("aio")
14+
l.addHandler(AIOHandler("test", device))
1215

13-
l = logging.getLogger('aio')
14-
l.addHandler(AIOHandler('test', device))
1516

1617
def go():
1718
while True:
1819
t = random.randint(1, 5)
1920
if t == 1:
20-
print('debug')
21+
print("debug")
2122
l.debug("debug message: %d", random.randint(0, 1000))
2223
elif t == 2:
23-
print('info')
24+
print("info")
2425
l.info("info message: %d", random.randint(0, 1000))
2526
elif t == 3:
26-
print('warning')
27+
print("warning")
2728
l.warning("warning message: %d", random.randint(0, 1000))
2829
elif t == 4:
29-
print('error')
30+
print("error")
3031
l.error("error message: %d", random.randint(0, 1000))
3132
elif t == 5:
32-
print('critical')
33+
print("critical")
3334
l.critical("critical message: %d", random.randint(0, 1000))
3435
time.sleep(5.0 + (random.random() * 5.0))

CircuitPython_Logger/ble_handler/code.py renamed to CircuitPython_Logger/ble_test/ble_handler.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,19 @@
1717
"""
1818

1919

20-
from adafruit_logging import Handler
20+
from adafruit_logging import Handler, NOTSET
2121
from adafruit_ble.uart import UARTServer
2222

23+
2324
class BLEHandler(Handler):
2425
"""Send logging output to the BLE uart port."""
2526

26-
def __init__(self):
27+
def __init__(self, level: int = NOTSET):
2728
"""Create an instance.
2829
2930
:param uart: the busio.UART instance to which to write messages
3031
"""
32+
super().__init__(level)
3133
self._advertising_now = False
3234
self._uart = UARTServer()
3335
self._uart.start_advertising()
@@ -37,7 +39,7 @@ def format(self, record):
3739
3840
:param record: The record (message object) to be logged
3941
"""
40-
return super().format(record) + '\r\n'
42+
return super().format(record) + "\r\n"
4143

4244
def emit(self, record):
4345
"""Generate the message and write it to the UART.
@@ -46,5 +48,5 @@ def emit(self, record):
4648
"""
4749
while not self._uart.connected:
4850
pass
49-
data = bytes(self.format(record), 'utf-8')
51+
data = bytes(self.format(record), "utf-8")
5052
self._uart.write(data)

CircuitPython_Logger/ble_test/code.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,27 @@
77
from ble_handler import BLEHandler
88
import adafruit_logging as logging
99

10-
l = logging.getLogger('ble')
10+
l = logging.getLogger("ble")
1111

1212
l.addHandler(BLEHandler())
1313

14+
1415
def go():
1516
while True:
1617
t = random.randint(1, 5)
1718
if t == 1:
18-
print('debug')
19+
print("debug")
1920
l.debug("%d", random.randint(0, 1000))
2021
elif t == 2:
21-
print('info')
22+
print("info")
2223
l.info("%d", random.randint(0, 1000))
2324
elif t == 3:
24-
print('warning')
25+
print("warning")
2526
l.warning("%d", random.randint(0, 1000))
2627
elif t == 4:
27-
print('error')
28+
print("error")
2829
l.error("%d", random.randint(0, 1000))
2930
elif t == 5:
30-
print('critical')
31+
print("critical")
3132
l.critical(" %d", random.randint(0, 1000))
3233
time.sleep(5.0 + (random.random() * 5.0))

CircuitPython_Logger/file_test/code.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,26 @@
2121
vfs = storage.VfsFat(sdcard)
2222
storage.mount(vfs, "/sd")
2323

24-
l = logging.getLogger('file')
25-
l.addHandler(logging.FileHandler('/sd/test.txt'))
24+
l = logging.getLogger("file")
25+
l.addHandler(logging.FileHandler("/sd/test.txt"))
26+
2627

2728
def go():
2829
while True:
2930
t = random.randint(1, 5)
3031
if t == 1:
31-
print('debug')
32+
print("debug")
3233
l.debug("debug message: %d", random.randint(0, 1000))
3334
elif t == 2:
34-
print('info')
35+
print("info")
3536
l.info("info message: %d", random.randint(0, 1000))
3637
elif t == 3:
37-
print('warning')
38+
print("warning")
3839
l.warning("warning message: %d", random.randint(0, 1000))
3940
elif t == 4:
40-
print('error')
41+
print("error")
4142
l.error("error message: %d", random.randint(0, 1000))
4243
elif t == 5:
43-
print('critical')
44+
print("critical")
4445
l.critical("critical message: %d", random.randint(0, 1000))
4546
time.sleep(5.0 + (random.random() * 5.0))
Lines changed: 10 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,13 @@
11
# SPDX-FileCopyrightText: 2018 Dave Astels for Adafruit Industries
22
#
33
# SPDX-License-Identifier: MIT
4-
5-
"""
6-
UART based message handler for CircuitPython logging.
7-
8-
Adafruit invests time and resources providing this open source code.
9-
Please support Adafruit and open source hardware by purchasing
10-
products from Adafruit!
11-
12-
Written by Dave Astels for Adafruit Industries
13-
Copyright (c) 2018 Adafruit Industries
14-
Licensed under the MIT license.
15-
16-
All text above must be included in any redistribution.
17-
"""
18-
19-
20-
# Example:
21-
#
22-
# import board
23-
# import busio
24-
# from uart_handler import UartHandler
25-
# import adafruit_logging as logging
26-
#
27-
# uart = busio.UART(board.TX, board.RX, baudrate=115200)
28-
# logger = logging.getLogger('uart')
29-
# logger.addHandler(UartHandler(uart))
30-
# logger.level = logging.INFO
31-
# logger.info('testing')
32-
33-
from adafruit_logging import Handler
34-
35-
class UartHandler(Handler):
36-
"""Send logging output to a serial port."""
37-
38-
def __init__(self, uart):
39-
"""Create an instance.
40-
41-
:param uart: the busio.UART instance to which to write messages
42-
"""
43-
self._uart = uart
44-
45-
def format(self, record):
46-
"""Generate a string to log.
47-
48-
:param record: The record (message object) to be logged
49-
"""
50-
return super().format(record) + '\r\n'
51-
52-
def emit(self, record):
53-
"""Generate the message and write it to the UART.
54-
55-
:param record: The record (message object) to be logged
56-
"""
57-
self._uart.write(bytes(self.format(record), 'utf-8'))
4+
import board
5+
import busio
6+
from uart_handler import UartHandler
7+
import adafruit_logging as logging
8+
9+
uart = busio.UART(board.TX, board.RX, baudrate=115200)
10+
logger = logging.getLogger("test")
11+
logger.addHandler(UartHandler(uart))
12+
logger.setLevel(logging.INFO)
13+
logger.info("testing")
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# SPDX-FileCopyrightText: 2018 Dave Astels for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
UART based message handler for CircuitPython logging.
7+
8+
Adafruit invests time and resources providing this open source code.
9+
Please support Adafruit and open source hardware by purchasing
10+
products from Adafruit!
11+
12+
Written by Dave Astels for Adafruit Industries
13+
Copyright (c) 2018 Adafruit Industries
14+
Licensed under the MIT license.
15+
16+
All text above must be included in any redistribution.
17+
"""
18+
19+
20+
from adafruit_logging import Handler, NOTSET
21+
22+
23+
class UartHandler(Handler):
24+
"""Send logging output to a serial port."""
25+
26+
def __init__(self, uart, level: int = NOTSET):
27+
"""Create an instance.
28+
29+
:param uart: the busio.UART instance to which to write messages
30+
"""
31+
super().__init__(level)
32+
self._uart = uart
33+
34+
def format(self, record):
35+
"""Generate a string to log.
36+
37+
:param record: The record (message object) to be logged
38+
"""
39+
return super().format(record) + "\r\n"
40+
41+
def emit(self, record):
42+
"""Generate the message and write it to the UART.
43+
44+
:param record: The record (message object) to be logged
45+
"""
46+
self._uart.write(bytes(self.format(record), "utf-8"))

0 commit comments

Comments
 (0)