CoAkka Logger · 1.2.2 · PyPI
Use CoAkka Logger with Python
Install the package, read a record and check the result before connecting Logger to an application.
Beginner
Install the package
Use Python 3.11 or newer. Create a virtual environment so the package and the interpreter used to run the code match.
macOS / Linux
Terminal
python3 -m venv .venv
.venv/bin/python -m pip install coakka-logger==1.2.2
Windows · PowerShell
Terminal
py -3 -m venv .venv
.venv\Scripts\python.exe -m pip install coakka-logger==1.2.2
Beginner
Read the first record
Save as main.py.
main.py
from __future__ import annotations
from coakka_logger import CoakkaLoggerLevel, Logger, LoggerSpec
def main() -> None:
info = Logger.read_info()
print(
f"coakka_logger_info abi={info.abi_version} "
f"version={info.runtime_version} git={info.git_commit}"
)
category = "samples.logger.python.basic"
message = '{"event":"hello","language":"python"}'
with Logger.start(spec=LoggerSpec(system_name="python-sample-logger", min_level=CoakkaLoggerLevel.INFO)) as logger:
sequence = logger.info(category, message)
if sequence is None:
raise RuntimeError("expected INFO log to be accepted")
record = logger.await_next(timeout_ms=1000)
if record is None:
raise RuntimeError("expected one drained logger record")
if record.sequence != sequence:
raise RuntimeError(f"record sequence={record.sequence} did not match emitted sequence={sequence}")
if record.category != category or record.message != message:
raise RuntimeError(f"unexpected record: {record!r}")
print(
f"coakka_logger_record sequence={record.sequence} "
f"level={record.level_name} category={record.category} message={record.message}"
)
stats = logger.stats()
print(
f"coakka_logger_stats emitted={stats.emitted_count} "
f"delivered={stats.delivered_count} dropped={stats.dropped_count}"
)
if __name__ == "__main__":
main()
macOS / Linux
Terminal
.venv/bin/python main.py
Windows · PowerShell
Terminal
.venv\Scripts\python.exe main.py
-
system_name - Names the Logger instance.
-
min_level - Sets which levels can be admitted.
-
await_next(timeout_ms=1000) - Bounds how long the reader waits. Match the returned sequence to the accepted write.
coakka_logger_record sequence=1 level=info ...
coakka_logger_stats emitted=1 delivered=1 dropped=0
Advanced
Handle queue pressure
Create a separate program for this episode. Set the queue to 2, submit 8 enabled records and start reading only after the submissions. Read the queue-full result before counting a refusal.
Save as pressure.py.
pressure.py
from __future__ import annotations
from coakka_logger import (
CoakkaLoggerLevel,
CoakkaLoggerStatus,
Logger,
LoggerSpec,
LoggerStatusError,
)
def main() -> None:
attempts = 8
category = "samples.logger.python.pressure"
with Logger.start(
spec=LoggerSpec(
system_name="python-pressure-logger",
queue_capacity=2,
min_level=CoakkaLoggerLevel.INFO,
)
) as logger:
accepted = 0
rejected = 0
for index in range(attempts):
try:
sequence = logger.info(category, f'{{"event":"pressure","index":{index}}}')
if sequence is not None:
accepted += 1
except LoggerStatusError as error:
if error.status != CoakkaLoggerStatus.QUEUE_FULL:
raise
rejected += 1
before_drain = logger.stats()
drained = 0
while logger.poll() is not None:
drained += 1
after_drain = logger.stats()
if accepted != 2:
raise RuntimeError(f"expected accepted=2, got {accepted}")
if rejected != attempts - accepted:
raise RuntimeError(f"expected rejected={attempts - accepted}, got {rejected}")
if drained != accepted:
raise RuntimeError(f"expected drained={accepted}, got {drained}")
if before_drain.queue_high_watermark != 2:
raise RuntimeError(f"expected queue_high_watermark=2, got {before_drain.queue_high_watermark}")
if after_drain.dropped_count != rejected:
raise RuntimeError(f"expected dropped={rejected}, got {after_drain.dropped_count}")
print(
f"coakka_logger_pressure attempts={attempts} accepted={accepted} rejected={rejected} "
f"capacity={after_drain.queue_capacity} highWatermark={after_drain.queue_high_watermark}"
)
print(
f"coakka_logger_stats emitted={after_drain.emitted_count} "
f"delivered={after_drain.delivered_count} dropped={after_drain.dropped_count}"
)
if __name__ == "__main__":
main()
macOS / Linux
Terminal
.venv/bin/python pressure.py
Windows · PowerShell
Terminal
.venv\Scripts\python.exe pressure.py
queue_capacity = 2. Count only the queue-full result
from LoggerStatusError.
coakka_logger_pressure attempts=8 accepted=2 rejected=6 capacity=2 highWatermark=2
coakka_logger_stats emitted=2 delivered=2 dropped=6