--- kind: logging_system name: MicroPython logging Module and C-Layer Output Mechanism category: logging_system scope: - '**' source_files: - firmware/port/modules/logging.py - esp32s3/firmware/modules/logging.py - esp32s3/firmware/modules/uwebsockets/client.py - esp32s3/firmware/modules/uwebsockets/protocol.py - firmware/port/file_system/uwebsockets/client.py --- # 1. System/Solution Used This repository is the MicroPython-based mPython Board firmware; its logging system consists of two parts: - **Python layer**: Implements a slimmed-down subset of the Python standard library `logging` (compatible with the MicroPython environment) in `firmware/port/modules/logging.py` and `esp32s3/firmware/modules/logging.py`, providing the `Logger` class, `getLogger`, `basicConfig`, the `DEBUG/INFO/WARNING/ERROR/CRITICAL` constants, and the `debug/info/warning/error/critical/exc/exception` methods. - **C layer**: Uses `sys.stderr` as the default output stream; underneath it actually relies on the MicroPython runtime to redirect `print()` / `_stream.write()` to the REPL or serial debug output; the C-extension builtins do not implement a separate logging framework and only output exception stack traces via `sys.print_exception`. This implementation is a slimmed-down port of the official MicroPython `logging` module, not a self-developed framework. # 2. Key Files | File | Layer | Purpose | |---|---|---| | `firmware/port/modules/logging.py` | Python extension module (ESP32 port) | Provides the `logging` module implementation | | `esp32s3/firmware/modules/logging.py` | Python extension module (ESP32-S3 port) | Same as above, fully identical to the ESP32 port | | `esp32s3/firmware/modules/uwebsockets/client.py` | Python extension module | Uses `logging.getLogger(__name__)` to log WebSocket connection debug information | | `esp32s3/firmware/modules/uwebsockets/protocol.py` | Python extension module | Same as above | | `firmware/port/file_system/uwebsockets/*.py` | File-system Python module | Same as above, for user code on SD/Flash | # 3. Architecture and Conventions ## Log Levels Uses numeric definitions consistent with the Python standard library: `NOTSET=0, DEBUG=10, INFO=20, WARNING=30, ERROR=40, CRITICAL=50`. Each `Logger` instance maintains its own `level`; if unset, it falls back to the module-level global `_level` (default `INFO`). The condition is `level >= (self.level or _level)`. ## Output Format Each log entry is output in the form `LEVEL:name:msg`, where the level label maps to `CRIT/ERROR/WARN/INFO/DEBUG` and unknown levels display as `LVL`. The name comes from the module name passed to `getLogger(name)` or `None`. ## Output Target The default output stream is `_stream = sys.stderr`, replaceable via `basicConfig(stream=...)`. The `filename` and `format` arguments are explicitly ignored and print notices: `logging.basicConfig: filename arg is not supported` / `format arg is not supported`. ## Logger Cache The module maintains a `_loggers = {}` dictionary; `getLogger(name)` reuses Logger instances by name to avoid repeated creation. ## Exception Output `exc(e, msg)` first outputs the message at ERROR level, then calls `sys.print_exception(e, _stream)` to output the full stack trace; `exception(msg)` obtains the exception object from the current exception context and delegates to `exc`. ## Top-Level Convenience Functions `logging.info()` and `logging.debug()` operate directly on a default `getLogger(None)` instance for quick output. # 4. Usage Patterns and Constraints - **Module-level logger pattern**: Modules such as `uwebsockets` obtain a named logger via `LOGGER = logging.getLogger(__name__)` and control debug-level output with the `__debug__` conditional switch; this is the only usage observed in the repository. - **No structured fields**: Log output is only string concatenation and does not support structured fields (JSON, key-value pairs, etc.). - **No multiple sinks**: Only supports a single `sys.stderr` output; cannot simultaneously write to a file, network, or SPIFFS/LittleFS. - **No async/thread safety**: Implemented as a synchronous single-process model without concurrency protection. - **No separate C-layer logging**: No custom logging macros or `ESP_LOG*` calls were found in the C-extension builtins (`firmware/port/builtins/`, `esp32s3/firmware/builtins/`); C-layer debugging relies on the MicroPython runtime's `printf`/REPL output. - **Aligned with MicroPython behavior**: This implementation follows MicroPython's standard trimming strategy for `logging` -- retaining the core API and removing heavyweight components such as handler/formatter. # 5. Conclusion The repository's logging system is a lightweight Python-layer `logging` adaptation aimed at resource-constrained embedded MicroPython environments, with all output routed through `sys.stderr` (i.e., serial/REPL). It lacks the features of a production-grade logging system (multiple sinks, structuring, async, filter chains), but it is sufficient for level-filtered text logging during the development and debugging phase.