A Logger for CircuitPython - Adafruit Industries

A Logger for CircuitPython

Created by Dave Astels



Last updated on 2022-12-01 03:33:31 PM EST

?Adafruit Industries

Page 1 of 21

Table of Contents

Overview

3

? Parts

Using a Logger

5

? Basic Use

CircuitPython

6

? Getting Familiar ? Download Library Files

Code Walkthrough

8

? Levels ? Getting a Logger ? Logger ? Handlers

Adding Handlers

12

Log to UART

13

Log to File

15

Log to Adafruit IO

16

Log to BLE

18

Testing and Expanding Handlers

20

? Testing handlers ? Getting More Elaborate

?Adafruit Industries

Page 2 of 21

Overview

Have you ever been working on code and needed a view into what was going on as it runs (or tries to)? In many environments you can go into a debugger and poke around. We don't have that ability in CircuitPython, though. If you're like this author, you sprinkle tactical print statements as needed.

Afterwards you probably go through and remove them or comment them out. Sometimes you miss some. Sometimes you'd like to leave them in place and be able to turn them on and off. There are times when you'd like to see some debugging information, and other times when you want to be notified of critical errors only.

A logging framework will let you do all that and more.

Specifically, the logging framework described in this guide will:

? let you output messages at one of several levels of priority, ? ignore messages below a specific priority, ? automatically add a timestamp to messages, ? provide the string format method support for building messages, ? give you convenience methods for the outputting at standard priority levels, ? control where messages go, and ? make it easy to add new places for messages to go.

This guide will go over the use of the framework, walk through the implementation, and work through an example of adding a new destination capability.

?Adafruit Industries

Page 3 of 21

Parts

As this service uses RAM and space for longer programs, this guide will note use on M4 and nRF52840-based boards.

Adafruit PyPortal - CircuitPython Powered Internet Display PyPortal, our easy-to-use IoT device that allows you to create all the things for the "Internet of Things" in minutes. Make custom touch screen interface...

Adafruit Metro M4 feat. Microchip ATSAMD51 Are you ready? Really ready? Cause here comes the fastest, most powerful Metro ever. The Adafruit Metro M4 featuring the Microchip ATSAMD51. This...

Adafruit Feather M4 Express - Featuring ATSAMD51 It's what you've been waiting for, the Feather M4 Express featuring ATSAMD51. This Feather is fast like a swift, smart like an owl, strong like a ox-bird (it's half ox,...

?Adafruit Industries

Page 4 of 21

Adafruit Feather nRF52840 Express The Adafruit Feather nRF52840 Express is the new Feather family member with Bluetooth Low Energy and native USB support featuring the nRF52840! It's...

USB cable - USB A to Micro-B This here is your standard A to micro-B USB cable, for USB 1.1 or 2.0. Perfect for connecting a PC to your Metro, Feather, Raspberry Pi or other dev-board or...

Using a Logger

First, let's cover how to use a logger in general. The code we're using is very similar to the Python Logging API () so if you've used that, you'll find this familiar

Basic Use

To use the framework, you create a logger and sprinkle logging calls throughout your code at appropriate levels.

import adafruit_logging as logging logger = logging.getLogger('test') logger.setLevel(logging.ERROR) ('Info message') logger.error('Error message')

The above example would ignore the info message and output the error one. Messages at any level less than the one set in the Logger will be ignored. By default (if you don't set the level) everything will be output. So the output would be:

?Adafruit Industries

Page 5 of 21

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download