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
Noneif not found).
- Perform the lookup in the shared library and return the matching string (or
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]): AttachedKrillCoreinstance (set bysetup_logger).alloc_id_list(list[Optional[str]]): History of identifiers retrieved fromKrillCore.
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).
- Return the most recently recorded subkey identifier (or
-
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
identifierflag. IfTrueand aKrillCoreinstance is attached, it will:- Call
KrillCore.setSubkey(msg)with the log message. - Call
KrillCore.find()and append the result toalloc_id_list.
- Call
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 forKrillCore.level(int, optional): Logging level (default:logging.INFO).
Returns
KrillLogger: Configured logger with aStreamHandlerand attachedKrillCore.
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
- Call
setup_logger()to create aKrillLoggerwith attachedKrillCore. - Pass
identifier=Trueto any log call to record the JSON lookup result for the message. - Use
getSubKeyList()orgetLastSubkey()to retrieve identifiers collected so far.
- Call
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file c_krill-0.0.3.tar.gz.
File metadata
- Download URL: c_krill-0.0.3.tar.gz
- Upload date:
- Size: 2.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
130dc1d7dc3e611bd31c3c554a2e83cdbc215b4a3f6be0826417d468e2651600
|
|
| MD5 |
74abc1e185eee5bb47dc048d12a26fd7
|
|
| BLAKE2b-256 |
0af89ad18d57e61c6cb0e3b241f52199911490947bd3c3b9969626e397b12f2c
|
File details
Details for the file c_krill-0.0.3-py3-none-any.whl.
File metadata
- Download URL: c_krill-0.0.3-py3-none-any.whl
- Upload date:
- Size: 3.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dfd62ff9335d999ae5884220ee92e8b876d56ebd392144bb0463c3344b451de3
|
|
| MD5 |
93759d9dc3f62f367b9eacf4e04372d8
|
|
| BLAKE2b-256 |
057351eceaff8822180303f8940b96de75ee216671058a39f4f5983fee008028
|