logger.py (loboris' MicroPython)

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
ttmetro
Posts: 104
Joined: Mon Jul 31, 2017 12:44 am

logger.py (loboris' MicroPython)

Post by ttmetro » Thu Oct 19, 2017 9:27 pm

logger.py included with loboris' MicroPython does not define the function basicConfig.

It is used by a library I am using. For reference I am including the code for a different logger.py downloaded from the web (with basicConfig and a few other differences). What's the best way to get these different versions to sync?

Code: Select all

import sys

CRITICAL = 50
ERROR    = 40
WARNING  = 30
INFO     = 20
DEBUG    = 10
NOTSET   = 0

_level_dict = {
    CRITICAL: "CRIT",
    ERROR: "ERROR",
    WARNING: "WARN",
    INFO: "INFO",
    DEBUG: "DEBUG",
}

_stream = sys.stderr

class Logger:

    def __init__(self, name):
        self.level = NOTSET
        self.name = name

    def _level_str(self, level):
        if level in _level_dict:
            return _level_dict[level]
        return "LVL" + str(level)

    def log(self, level, msg, *args):
        if level >= (self.level or _level):
            print(("%s:%s:" + msg) % ((self._level_str(level), self.name) + args), file=_stream)

    def debug(self, msg, *args):
        self.log(DEBUG, msg, *args)

    def info(self, msg, *args):
        self.log(INFO, msg, *args)

    def warning(self, msg, *args):
        self.log(WARNING, msg, *args)

    def error(self, msg, *args):
        self.log(ERROR, msg, *args)

    def critical(self, msg, *args):
        self.log(CRITICAL, msg, *args)


_level = INFO
_loggers = {}

def getLogger(name):
    if name in _loggers:
        return _loggers[name]
    l = Logger(name)
    _loggers[name] = l
    return l

def info(msg, *args):
    getLogger(None).info(msg, *args)

def debug(msg, *args):
    getLogger(None).debug(msg, *args)

def basicConfig(level=INFO, filename=None, stream=None, format=None):
    global _level, _stream
    _level = level
    if stream:
        _stream = stream
    if filename is not None:
        print("logging.basicConfig: filename arg is not supported")
    if format is not None:
        print("logging.basicConfig: format arg is not supported")
Bernhard Boser

loboris
Posts: 344
Joined: Fri Oct 02, 2015 6:19 pm

Re: logger.py (loboris' MicroPython)

Post by loboris » Thu Oct 19, 2017 10:46 pm

Thank you for noticing that.
The included logging.py module was left from some testing, I forgot to replace it.
I've replaced it now with logging.py from micropython-lib (the one you have quoted), the changes will be pushed tomorrow.

Post Reply