micropython-lib icon indicating copy to clipboard operation
micropython-lib copied to clipboard

logging: add call to superclass initialiser; set default formatter

Open colin-nolan opened this issue 1 year ago • 0 comments

Addresses minor issues in logging module, reported in https://github.com/micropython/micropython-lib/issues/691.

Change 1: Makes subclasses of logging.Handler call the superclasses' initialiser

CPython:

>>> import logging
>>> logger = logging.StreamHandler()
>>> logger.level 
0

MicroPython (master):

>>> logger = logging.StreamHandler()
>>> logger.level
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'StreamHandler' object has no attribute 'level'

MicroPython (this branch):

>>> logger = logging.StreamHandler()
>>> logger.level
0

Change 2: Sets a default formatter if not set

CPython:

>>> record = logging.LogRecord("Test", 20, "/example", 1, "Hello", (), None)
>>> logger.emit(record)
Hello

MicroPython (master + fix 1):

>>> record = logging.LogRecord()
>>> record.set("Test", 20, "Hello")
>>> logger.emit(record)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "logging.py", line 72, in emit
  File "logging.py", line 56, in format
AttributeError: 'NoneType' object has no attribute 'format'

MicroPython (this branch):

>>> record = logging.LogRecord()
>>> record.set("Test", 20, "Hello")
>>> logger.emit(record)
INFO:Test:Hello

The CPython implementation of logging can be found here: https://github.com/python/cpython/blob/main/Lib/logging/init.py

colin-nolan avatar Aug 05 '23 18:08 colin-nolan