Skip to main content

A lightweight logging library for the Internet Computer (IC)

Project description

IC Python Logging

Test IC Test PyPI version Python 3.10 License

A simple logging system for the Internet Computer. Forked from kybra-simple-logging. The library includes in-memory log storage capabilities, providing robust logging for all canister functions including asynchronous operations.

Features

  • CLI tool for querying logs directly from canisters to your local machine (including in semi-real time with --follow)
  • Works seamlessly in both Internet Computer and non-IC environments
  • Avoids using Python's standard logging module (which has compatibility issues in the IC environment)
  • Named loggers with get_logger() function similar to Python's standard library
  • Support for level-based filtering (DEBUG, INFO, WARNING, ERROR, CRITICAL)
  • Global and per-logger log level configuration
  • Ability to enable/disable logging completely
  • Circular buffer to store logs in memory without exhausting memory

Installation

pip install ic-python-logging

Quick Start

from ic_python_logging import get_logger

# Create a logger
logger = get_logger("my_canister")

# Log messages at a specific level
logger.info("This is an info message")

# Set log level for a specific logger
logger.set_level(LogLevel.DEBUG)

# Use in-memory logging to retrieve logs
from ic_python_logging import get_logs, clear_logs, enable_memory_logging, disable_memory_logging

# Retrieve only ERROR logs
error_logs = get_logs(min_level="ERROR")

# Filter logs by logger name
component_logs = get_logs(logger_name="my_component")

CLI Tool

The package includes a command-line tool for querying logs from canisters. Example:

# View the first 10 ERROR log entries of logger with name MY_LOGGER_NAME, and then follow and poll every 5 seconds, from the canister with ID <CANISTER_ID> on the IC network
kslog <CANISTER_ID> --tail 10 --level ERROR --name MY_LOGGER_NAME --follow --ic --interval 5

To use this kslog with your canister, expose the query function:

# ##### Import Basilisk and the internal function #####

from basilisk import Opt, Record, Vec, nat, query
from ic_python_logging import get_canister_logs as _get_canister_logs


# Define the PublicLogEntry class directly in the test canister
class PublicLogEntry(Record):
    timestamp: nat
    level: str
    logger_name: str
    message: str
    id: nat


@query
def get_canister_logs(
        from_entry: Opt[nat] = None,
        max_entries: Opt[nat] = None,
        min_level: Opt[str] = None,
        logger_name: Opt[str] = None,
) -> Vec[PublicLogEntry]:
    """
    Re-export the get_canister_logs query function from the library
    This makes it accessible as a query method on the test canister
    """
    logs = _get_canister_logs(
        from_entry=from_entry,
        max_entries=max_entries,
        min_level=min_level,
        logger_name=logger_name
    )

    # Convert the logs to our local PublicLogEntry type
    return [
        PublicLogEntry(
            timestamp=log["timestamp"],
            level=log["level"],
            logger_name=log["logger_name"],
            message=log["message"],
            id=log["id"],
        )
        for log in logs
    ]

Development

# Clone the repository
git clone https://github.com/smart-social-contracts/ic-python-logging.git
cd ic-python-logging

# Recommended setup
pyenv install 3.10.7
pyenv local 3.10.7
python -m venv venv
source venv/bin/activate
pip install ic-basilisk
python -m basilisk install-dfx-extension

# Install development dependencies
pip install -r requirements-dev.txt

# Run linters
./run_linters.sh

# Run tests
cd tests && ./run_test.sh

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT

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

ic_python_logging-0.3.3.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

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

ic_python_logging-0.3.3-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file ic_python_logging-0.3.3.tar.gz.

File metadata

  • Download URL: ic_python_logging-0.3.3.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for ic_python_logging-0.3.3.tar.gz
Algorithm Hash digest
SHA256 b7a25195ad096567fbf2fa31538c030b0f6f17d9ec00c0ac77cbe40510da2d6b
MD5 ad7e718bf3dc4619f383e63710f78c97
BLAKE2b-256 a303ead9633c28f1c7320337661bca8d32a40ee89cbe216d10e6c008496d9334

See more details on using hashes here.

File details

Details for the file ic_python_logging-0.3.3-py3-none-any.whl.

File metadata

File hashes

Hashes for ic_python_logging-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 28fb080ca67c0e06ca54ee1dcc7a54fb8928afa8db02a9e2b9ddb4c03a80a421
MD5 f52edb71ec5dcaeb020c5bbc7fab95ad
BLAKE2b-256 a8121cd6eca9a1184a30ffe7907c80ae153a10608bc47d2059a725ab23d8f9d1

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