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) logger.info('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:
1556.96: ERROR - Error message
When you use the log method you can pass in a numeric value, similarly you can set the level of the logger to any numeric value. This gives you the most control over the logger. As an alternative, you can use the 5 defined level values:
-
DEBUG
- 10 -
INFO
- 20 -
WARNING
- 30 -
ERROR
- 40 -
CRITICAL
- 50
When a log message is output, the level gets rounded down. For example, a level of 36 would output as WARNING
.
To make things easy to use, Logger
provides a method for each of the levels. As shown above, you can use calls likelogger.error('Error message').
As mentioned, you can use existing Python formatting strings to build the message:
logger.info('Bad value: %d', value)
That's pretty much it. You create a logger, add logging statements to your code, and when your code starts up, set the lowest level of messages you want to see.
Text editor powered by tinymce.