Skip to main content

ESPlog - A Lightweight Logger for MicroPython & ESP Microcontrollers

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

esplog-1.0.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

esplog-1.0.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file esplog-1.0.0.tar.gz.

File metadata

  • Download URL: esplog-1.0.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.0

File hashes

Hashes for esplog-1.0.0.tar.gz
Algorithm Hash digest
SHA256 5adec5790b7ff409ffd7ad8f88382653666aaca1d2c21f977cea2122f037921a
MD5 fbe93762ac0dddf11b92e9480f37100d
BLAKE2b-256 4a7de9d0b5217ffc6d8ffe20e4fd970530293622b175e89f2cd7c3b7d59b10f9

See more details on using hashes here.

File details

Details for the file esplog-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: esplog-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.0

File hashes

Hashes for esplog-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 54289d0f8cbf635fea4ce9efca687b684068f424392d11372288f1c0ed15ff3b
MD5 abee08c609dc25d21457016eaeea1d66
BLAKE2b-256 50583b8f100a86d25340fd7f5e3ff5234745b7a7b234cc7b92f9e3534dd70d21

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page