Cross-process logging via a dedicated log server process and SocketHandler.
Project description
kitty_logger
Cross-process Python logger. The main process starts a dedicated log-server
subprocess; every other process (forked or spawned) ships LogRecords to it
via logging.handlers.SocketHandler, so all log lines end up in one place
with no interleaving or file-locking issues.
Install
pip install .
Usage
# main.py
import multiprocessing as mp
import kitty_logger
def worker(i):
log = kitty_logger.getLogger(f"worker.{i}")
log.info("hello from worker %d", i)
if __name__ == "__main__":
kitty_logger.setup_logging(log_file="app.log")
log = kitty_logger.getLogger("main")
log.info("starting")
with mp.get_context("spawn").Pool(4) as pool:
pool.map(worker, range(4))
setup_logging sets the env vars KITTY_LOGGER_HOST / KITTY_LOGGER_PORT,
which child processes inherit and use to connect.
API
setup_logging(log_file=None, level=logging.INFO, host="127.0.0.1", port=0, stream=True, console_fmt=..., file_fmt=..., datefmt=None, attach_main_logger=True) -> (host, port)Starts the log-server subprocess (always uses thespawnstart method). Idempotent. Registered withatexit.port=0lets the OS pick a free port; the actual(host, port)is returned.getLogger(name=None) -> logging.LoggerReturns a logger with aSocketHandlerpointing at the server.shutdown_logging()— stop the server subprocess explicitly.
Why spawn-only
Child processes started with fork inherit the parent's already-connected
SocketHandler (and its TCP fd). Parent and child would then interleave
pickled-record bytes on the same connection, which corrupts every record on
the wire. fork is also unsafe in multi-threaded parents and unavailable on
Windows. KittyLogger therefore only supports spawn. Use
multiprocessing.get_context("spawn") (or just rely on Python's defaults
under if __name__ == "__main__":).
Security
The server uses pickle to deserialize incoming LogRecords. Only ever bind
to a loopback address (the default 127.0.0.1). Binding to 0.0.0.0 or any
externally reachable interface exposes a remote-code-execution surface to anyone
who can reach the port.
Caveats
- Do not call
logging.basicConfig()(or otherwise attach aStreamHandlerto the root logger) beforesetup_logging(attach_main_logger=True)— the main process would emit each record twice: once via the local root handler and once via the log-server. Either let kitty_logger be the only configurer, or passattach_main_logger=Falseand manage main-process handlers yourself. - After
shutdown_logging(), kitty_logger removes its ownSocketHandlers from this process's loggers and clearsKITTY_LOGGER_*env vars, so the process state is symmetric withsetup_logging. If you attach extra handlers yourself, you remain responsible for cleaning them up.
Project details
Release history Release notifications | RSS feed
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 kitty_logger-0.1.0.tar.gz.
File metadata
- Download URL: kitty_logger-0.1.0.tar.gz
- Upload date:
- Size: 16.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
be0d8171cf9cb9399fa6ede41433776026a95dfdc2315679a5488eb95068173e
|
|
| MD5 |
498a5a2891d376418d9224aa618650ba
|
|
| BLAKE2b-256 |
cd1661ae47c1833b6ca2e32d1d8fd38ec34588658d8e260039aaa62d29d87dbf
|
File details
Details for the file kitty_logger-0.1.0-py3-none-any.whl.
File metadata
- Download URL: kitty_logger-0.1.0-py3-none-any.whl
- Upload date:
- Size: 14.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
58d58b9d91445f215c7319972b4b78b9af6ef1dde9634d83368f9a5b790d313e
|
|
| MD5 |
2a5cbfcda4f834e8f59b465c7a80d4d3
|
|
| BLAKE2b-256 |
ab011aa1df9f28ef99ee89398133cd6c98bfd751aa9f3611c03b4f70044a70cd
|