Skip to main content

Kybra Simple Logging

Test IC Test PyPI version Python 3.10 License

A simple logging system for the Internet Computer built with Kybra. 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 kybra-simple-logging

Quick Start

from kybra_simple_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 kybra_simple_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 Kybra and the internal function #####

from kybra import Opt, Record, Vec, nat, query
from kybra_simple_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/kybra-simple-logging.git
cd kybra-simple-logging

# Recommended setup
pyenv install 3.10.7
pyenv local 3.10.7
python -m venv venv
source venv/bin/activate
pip install kybra
python -m kybra 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

Release files for kybra-simple-logging 0.2.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 kybra-simple-logging 0.2.0
File Size Uploaded
kybra_simple_logging-0.2.0.tar.gz 12.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for kybra-simple-logging 0.2.0
File Interpreter ABI Platform
kybra_simple_logging-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 24.6 kB

Release files / kybra_simple_logging-0.2.0.tar.gz

Download URL kybra_simple_logging-0.2.0.tar.gz
Size 12.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c2af0724794b70a4afc725eafff567d02562cb304a66d2adbb70cfc9f880cc13
BLAKE2b-256 checksum
How to use checksums
5ad0d10141b924d9c022465e7af9cc67301c831400c851a3791cde4a84073665
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.17

Release files / kybra_simple_logging-0.2.0-py3-none-any.whl

Download URL kybra_simple_logging-0.2.0-py3-none-any.whl
Size 11.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
174a96f6ce8928de36f5954f66a6c0d3d04504dd972d4e191d2fb9d95dfd49a2
BLAKE2b-256 checksum
How to use checksums
c31b4e81d621fd072c182bd55d94035374a9f2c56aa33c8d1b307f528e5ff33b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.17

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

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