code.py
1 # SPDX-FileCopyrightText: 2018 Dave Astels for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 """ 6 Adafruit IO 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 from adafruit_portalbase import PortalBase 20 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 33 34 class AIOHandler(Handler): 35 36 def __init__(self, name, portal_device): 37 """Create an instance.""" 38 self._log_feed_name=f"{name}-logging" 39 if not issubclass(type(portal_device), PortalBase): 40 raise TypeError("portal_device must be a PortalBase or subclass of PortalBase") 41 self._portal_device = portal_device 42 43 44 def emit(self, record): 45 """Generate the message and write it to the AIO Feed. 46 47 :param record: The record (message object) to be logged 48 """ 49 self._portal_device.push_to_io(self._log_feed_name, self.format(record))