Metadata-Version: 2.4
Name: kybra_simple_logging
Version: 0.1.3
Summary: A lightweight logging library for Kybra on the Internet Computer (IC)
Home-page: https://github.com/smart-social-contracts/kybra_simple_logging
Author: Smart Social Contracts
Author-email: Smart Social Contracts <smartsocialcontracts@gmail.com>
License: MIT License
        
        Copyright (c) 2025 Smart Social Contracts
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/smart-social-contracts/kybra_simple_logging
Project-URL: Repository, https://github.com/smart-social-contracts/kybra-simple-logging.git
Project-URL: Issues, https://github.com/smart-social-contracts/kybra-simple-logging/issues
Keywords: database,key-value,entity,relationship,audit
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Topic :: Database
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.7
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: author
Dynamic: home-page
Dynamic: license-file
Dynamic: requires-python

# Kybra Simple Logging

[![Test](https://github.com/smart-social-contracts/kybra-simple-logging/actions/workflows/test.yml/badge.svg)](https://github.com/smart-social-contracts/kybra-simple-logging/actions)
[![PyPI version](https://badge.fury.io/py/kybra-simple-logging.svg)](https://badge.fury.io/py/kybra-simple-logging)
[![Python 3.10](https://img.shields.io/badge/python-3.10-blue.svg)](https://www.python.org/downloads/release/python-3107/)
[![License](https://img.shields.io/github/license/smart-social-contracts/kybra-simple-logging.svg)](https://github.com/smart-social-contracts/kybra-simple-logging/blob/main/LICENSE)

A robust logging system for Internet Computer canisters built with Kybra, designed to overcome the limitations of Python's standard logging module in the IC environment. The library includes in-memory log storage capabilities, making it ideal for debugging asynchronous functions where standard logging might be unreliable.

## Features

### Basic Logging
- 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
- Best practices for library logging implementation

### In-Memory Logging
- Circular buffer to store logs in memory without exhausting memory
- Capture logs even when display/printing is disabled
- Store detailed log entries with timestamp, level, logger name, and message
- Retrieve and filter logs by level, logger name, or custom criteria
- Independent controls to enable/disable in-memory logging separately from console output
- Ideal for debugging asynchronous functions in the Internet Computer environment

## Installation

### Using pip

```bash
pip install kybra-simple-logging
```

## Quick Start

```python
from kybra_simple_logging import get_logger

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

# Log messages at different levels
logger.debug("This is a debug message")
logger.info("This is an info message")
logger.warning("This is a warning message")
logger.error("This is an error message")
logger.critical("This is a critical message")

# Configure logging
from kybra_simple_logging import set_log_level, LogLevel

# Set global log level
set_log_level(LogLevel.INFO)

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

# Disable logging completely
from kybra_simple_logging import disable_logging, enable_logging

disable_logging()
# ... operations without logging ...
enable_logging()

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

# Clear any existing logs
clear_logs()

# Control memory logging (independent from console output)
disable_memory_logging()  # Stop storing logs in memory (still prints to console if enabled)
logger.info("This will NOT be stored in memory")  

enable_memory_logging()  # Start storing logs in memory again
logger.info("This message is stored in memory")

# Retrieve all logs
logs = get_logs()
for log in logs:
    print(f"{log['timestamp']} [{log['level']}] [{log['logger_name']}]: {log['message']}")

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

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

## Advanced Usage

### Creating Multiple Loggers

```python
# Create loggers for different components
db_logger = get_logger("database")
api_logger = get_logger("api")
auth_logger = get_logger("auth")

# Set different log levels per component
db_logger.set_level(LogLevel.DEBUG)  # More verbose logging for database
api_logger.set_level(LogLevel.INFO)  # Standard logging for API
auth_logger.set_level(LogLevel.WARNING)  # Only warnings and above for auth
```

### In Kybra Canister Functions

```python
from kybra import query
from kybra_simple_logging import get_logger

logger = get_logger("my_canister")

@query
def get_data():
    logger.info("Processing get_data request")
    # ... your code ...
    return result
```

### In-Memory Logging for Debugging

```python
from kybra import update, query
from kybra_simple_logging import (
    get_logger, get_logs, clear_logs, set_max_log_entries,
    enable_memory_logging, disable_memory_logging, is_memory_logging_enabled
)

logger = get_logger("debug_logger")

# Configure the maximum number of logs to keep in memory
set_max_log_entries(1000)

@update
def process_async_task():
    # Clear logs for this specific task
    clear_logs()
    
    # Make sure memory logging is enabled for this task
    if not is_memory_logging_enabled():
        enable_memory_logging()
    
    logger.debug("Starting async task")
    # ... complex asynchronous operations ...
    logger.debug("Step 1 completed")
    # ... more operations ...
    logger.debug("Async task completed")
    
    return "Task completed"

@query
def get_debug_logs(min_level=None, component=None):
    """Retrieve logs for debugging purposes"""
    return get_logs(min_level=min_level, logger_name=component)

@query
def toggle_memory_logging(enabled=True):
    """Enable or disable memory logging"""
    if enabled:
        enable_memory_logging()
    else:
        disable_memory_logging()
    return is_memory_logging_enabled()
```

### Using In-Memory Logs with kybra_simple_shell

You can use this logging system with `kybra_simple_shell` to debug asynchronous functions:

```python
# In your canister code
from kybra_simple_logging import logger, get_logs

# Later, in kybra_simple_shell
logs = get_logs(min_level="DEBUG")
for log in logs:
    print(f"{log['timestamp']} [{log['level']}] [{log['logger_name']}]: {log['message']}")
```

## Development

```bash
# 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

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

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

# Run linters
./run_linters.sh
```

## License

MIT
