Skip to main content

Python logging library that works with JSON

Project description

krill

API Reference

This module provides a ctypes-based interface to a Zig-made shared library for JSON‑based key→subkey lookups, and a custom logging.Logger subclass that records identifiers retrieved from that library.


Classes

KrillCore

KrillCore(json_path: str, key: str, subkey: str)

Description Calls into the Zig-based shared library via ctypes to perform lookups from a JSON file. Maintains internal state for key and subkey.

Parameters

  • json_path (str): Path to the JSON file containing nested key→subkey mappings.
  • key (str): Primary lookup key.
  • subkey (str): Secondary lookup key.

Attributes

  • json_path (str)
  • key (str)
  • subkey (str)
  • buf_size (int): Size of the output buffer (default: 256).

Methods

  • setSubkey(subkey: str) -> None

    • Set the secondary lookup key for subsequent calls.
  • _bytes_from_string(s: str) -> bytes

    • Encode a Python string to UTF‑8 bytes.
  • find() -> Optional[str]

    • Perform the lookup in the shared library and return the matching string (or None if not found).

KrillLogger

class KrillLogger(logging.Logger)

Description A logging.Logger subclass that integrates with KrillCore to optionally record subkey identifiers on each log call.

Attributes

  • _krill (Optional[KrillCore]): Attached KrillCore instance (set by setup_logger).
  • alloc_id_list (list[Optional[str]]): History of identifiers retrieved from KrillCore.

Methods

  • getSubKeyList() -> list[Optional[str]]

    • Return the full list of recorded subkey identifiers.
  • getLastSubkey() -> Optional[str]

    • Return the most recently recorded subkey identifier (or None).
  • debug(msg, identifier: bool=False, *args, **kwargs)

  • info(msg, identifier: bool=False, *args, **kwargs)

  • warning(msg, identifier: bool=False, *args, **kwargs)

  • error(msg, identifier: bool=False, *args, **kwargs)

  • critical(msg, identifier: bool=False, *args, **kwargs)

    Each logging method accepts an extra identifier flag. If True and a KrillCore instance is attached, it will:

    1. Call KrillCore.setSubkey(msg) with the log message.
    2. Call KrillCore.find() and append the result to alloc_id_list.

Functions

setup_logger

def setup_logger(
    name: str,
    json_path: str,
    level: int = logging.INFO
) -> KrillLogger:

Description Configure the root logging system to use KrillLogger, attach a KrillCore instance, and return the configured logger.

Parameters

  • name (str): Logger name (also used as the primary key for lookups).
  • json_path (str): Path to the JSON file for KrillCore.
  • level (int, optional): Logging level (default: logging.INFO).

Returns

  • KrillLogger: Configured logger with a StreamHandler and attached KrillCore.

Sample Usage

import logging
from krill import KrillCore, KrillLogger, setup_logger

API_NAME  = 'API_ROOT_ID'
JSON_PATH = 'logger_output.json'

# Initialize the logger
logger = setup_logger(
    name=API_NAME,
    json_path=JSON_PATH,
    level=logging.DEBUG
)

# Log messages with subkey tracking enabled
logger.info("hello", identifier=True)
logger.debug("debug message", identifier=True)
logger.warning("warning message", identifier=True)
logger.error("error message", identifier=True)
logger.critical("critical message", identifier=True)

# Inspect recorded identifiers
print("SubKey List:", logger.getSubKeyList())
print("Last SubKey:", logger.getLastSubkey())
  • Step-by-step
    1. Call setup_logger() to create a KrillLogger with attached KrillCore.
    2. Pass identifier=True to any log call to record the JSON lookup result for the message.
    3. Use getSubKeyList() or getLastSubkey() to retrieve identifiers collected so far.

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

c_krill-0.0.6.tar.gz (1.5 MB view details)

Uploaded Source

Built Distribution

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

c_krill-0.0.6-py3-none-any.whl (1.5 MB view details)

Uploaded Python 3

File details

Details for the file c_krill-0.0.6.tar.gz.

File metadata

  • Download URL: c_krill-0.0.6.tar.gz
  • Upload date:
  • Size: 1.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for c_krill-0.0.6.tar.gz
Algorithm Hash digest
SHA256 57be16469f361b2a5e610c81b15738a50f7235c0c54045c40f54f82fafef8adf
MD5 21e07d794139d48874774a586b6f983f
BLAKE2b-256 8fffb45db5e06f4595f4be670d66e7828890af732a014be8927bde2fb47a484d

See more details on using hashes here.

File details

Details for the file c_krill-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: c_krill-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 1.5 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for c_krill-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 d2659e98ae4c9833c34d69adfdbe1434d0df86c3e8aba8e61647af3fb5e2f7ec
MD5 227134ec1589ba7ea7a5c82bed7aa381
BLAKE2b-256 fd99d0ded45fe6231f3e5dd8f072b2f5f92213d177a7e5fb785843f5a396d235

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