Skip to main content

esplog - A Lightweight Logger for MicroPython & ESP Microcontrollers

Overview

esplog is a lightweight logging system designed for MicroPython environments. It allows you to log messages at different severity levels, output logs to both the console and a file, and supports log rotation based on file size. The system is flexible and can be configured to use color-coded logs for the console and store logs in either text or JSON format. It is optimized for use on embedded systems and microcontrollers where resources are limited.

Features

  • Multiple log levels: Supports various log levels including DEBUG, INFO, WARNING, ERROR, CRITICAL, and TRACE.
  • Color-coded console output: Log messages in the console are color-coded for better visibility and differentiation based on severity.
  • File logging: Logs can be written to a file in either text or JSON format.
  • Log rotation: If the log file exceeds a certain size, it will be automatically rotated (older logs are renamed).
  • Timestamping: Each log entry includes a timestamp in the format YYYY-MM-DD HH:MM:SS.
  • Flexible log level management: You can set the minimum log level dynamically and filter out lower-level messages.
  • Disable logging: You can completely disable logging when necessary.

To install esplog, you can use upip:

for MicroPython, use the appropriate package manager like upip to install directly on your microcontroller.

upip install esplog

Usage

Example:

from esplog.core import Logger

# Create a Logger instance with the desired settings
logger = Logger(
    level="DEBUG",          # Set default log level to DEBUG
    log_to_console=True,    # Enable logging to console
    log_to_file=True,       # Enable logging to file
    file_name="app_log.txt", # Name of the log file
    max_file_size=1024,     # Maximum log file size in bytes before rotation
    use_colors=True,        # Use color coding in console logs
    log_format="text"       # Log format, can be "text" or "json"
)

# Logging messages at different levels
logger.debug("This is a debug message.")
logger.info("System initialized successfully.")
logger.warning("Warning: High memory usage detected.")
logger.error("Error: Disk space is running low.")
logger.critical("Critical error: Immediate action required.")
logger.trace("Trace message for detailed debugging.")

# Changing the log level to ERROR, which will ignore lower levels (INFO, DEBUG)
logger.set_level("ERROR")
logger.info("This message will not be logged.")
logger.error("A critical error occurred.")

# Disabling all logging
logger.disable()
logger.error("This message will not be logged.")

Logger Class Overview

__init__(self, level="INFO", log_to_console=True, log_to_file=False, file_name="log.txt", max_file_size=0, use_colors=True, log_format="text")

  • level: Set the default log level (one of DEBUG, INFO, WARNING, ERROR, CRITICAL).
  • log_to_console: Boolean flag to log messages to the console.
  • log_to_file: Boolean flag to log messages to a file.
  • file_name: Name of the log file when logging to file.
  • max_file_size: Maximum file size in bytes before rotating the log file (0 means no rotation).
  • use_colors: Boolean flag to enable color-coded output in the console.
  • log_format: The format of the log file, either "text" or "json".

Log Levels

The logger supports the following log levels:

  • DEBUG: Detailed debug messages.
  • INFO: General information messages.
  • WARNING: Warnings about potential issues.
  • ERROR: Errors that need attention.
  • CRITICAL: Critical errors requiring immediate action.
  • TRACE: Trace messages for detailed debugging (lowest level).

Methods

  • set_level(level): Set the logging level to control the severity of messages logged.
  • debug(message): Log a message at the DEBUG level.
  • info(message): Log a message at the INFO level.
  • warning(message): Log a message at the WARNING level.
  • error(message): Log a message at the ERROR level.
  • critical(message): Log a message at the CRITICAL level.
  • trace(message): Log a message at the TRACE level.
  • disable(): Disable all logging (no messages will be logged).

Log File Format

Text Format

Each log entry consists of the following format:

[YYYY-MM-DD HH:MM:SS][LEVEL] message

Example:

[2024-12-16 14:23:45][INFO] System initialized successfully.

JSON Format

Each log entry is a JSON object with the following fields:

{
  "timestamp": "YYYY-MM-DD HH:MM:SS",
  "level": "LEVEL",
  "message": "message",
  "color": "color_code"  // Only included for console logs with colors enabled
}

Example:

{
  "timestamp": "2024-12-16 14:23:45",
  "level": "INFO",
  "message": "System initialized successfully.",
  "color": "\u001b[92m"
}

Notes

  • File Rotation: When the log file exceeds the defined size (max_file_size), the file is rotated and renamed with a .old suffix.
  • Compatibility: This logger is designed for MicroPython environments and works on microcontrollers like ESP32, ESP8266, and other low-resource systems.
  • Error Handling: The logger gracefully handles errors when writing to files or rotating logs, ensuring that logging continues without disruption.

License

esplog is licensed under the MIT License. See the LICENSE file for more details.

Metadata

Release files for esplog 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for esplog 1.0.0
File Size Uploaded Provenance Actions
esplog-1.0.0.tar.gz 4.4 kB Without provenance Details for esplog-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for esplog 1.0.0
File Interpreter ABI Platform Actions
esplog-1.0.0-py3-none-any.whl Python 3 none any Details for esplog-1.0.0-py3-none-any.whl

Total release size: 8.5 kB

Release files / esplog-1.0.0.tar.gz

Download URL esplog-1.0.0.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
5adec5790b7ff409ffd7ad8f88382653666aaca1d2c21f977cea2122f037921a
BLAKE2b-256 checksum
How to use checksums
4a7de9d0b5217ffc6d8ffe20e4fd970530293622b175e89f2cd7c3b7d59b10f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.0

Release files / esplog-1.0.0-py3-none-any.whl

Download URL esplog-1.0.0-py3-none-any.whl
Size 4.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
54289d0f8cbf635fea4ce9efca687b684068f424392d11372288f1c0ed15ff3b
BLAKE2b-256 checksum
How to use checksums
50583b8f100a86d25340fd7f5e3ff5234745b7a7b234cc7b92f9e3534dd70d21
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.0

Security for esplog 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page