A lightweight Python utility for file-based logging and persistent state tracking.
Project description
SLinfra
A lightweight Python utility for file-based logging and persistent state tracking.
Overview
- SLinfra is a minimal Python package designed to help small to medium projects:
- Keep clean and readable logs
- Persist application state between runs
- Avoid heavy logging frameworks
- It is especially useful for scripts, automation tools, and long-running processes.
Features
- Simple file-based logger
- Persistent state storage (JSON-based)
- Zero external dependencies
- Easy to integrate into existing projects
Installation
pip install SLinfra
Quick Start
File Logging
from SLinfra.file_logger import FileLogger
logger = FileLogger(log_dir="logs", log_file="app.log")
logger.info("Application started")
logger.warning("Low memory warning")
logger.error("Unexpected error occurred")
logger.close() # or use it as a context manager, see below
This will create (or append to) a log file and store timestamped log messages.
By default the log file rotates at 5 MB, keeping up to 3 backups
(app.log.1, app.log.2, app.log.3); pass max_bytes=0 to disable
rotation, or tune max_bytes / backup_count to taste.
Prefer the context manager form so the file handle is always closed:
with FileLogger(log_dir="logs", log_file="app.log") as logger:
logger.info("Application started")
State Tracking
from SLinfra.state_tracker import StateTracker, Status
state = StateTracker("state.json")
state.set("job-1", Status.PENDING.value, retries=0)
state.edit("job-1", Status.DONE.value, result="ok")
print(state.get("job-1"))
print(state.filter_by_status(Status.DONE.value))
This allows your application to persist important values between executions.
Each set/edit/remove/clear call saves to disk immediately using an
atomic write (write to a temp file, then os.replace), so a crash mid-write
can't leave you with a half-written state.json.
By default, StateTracker also takes an OS-level advisory file lock
(fcntl on POSIX, msvcrt on Windows) around each write, so multiple
processes pointed at the same state file won't clobber each other's
updates. Pass use_file_lock=False if you only ever use one process and
want to skip the lock-file overhead.
Project Structure
SLinfra/
├── SLinfra/
│ ├── __init__.py
│ ├── file_logger.py
│ └── state_tracker.py
├── tests/
│ ├── test_file_logger.py
│ └── test_state_tracker.py
├── README.md
├── LICENSE
└── pyproject.toml
Design Goals
- Keep the API small and intuitive
- Prefer simplicity over feature overload
- Avoid external dependencies
- Be suitable for educational and practical use
Limitations
FileLoggerrotation is size-based only (no time-based rotation, e.g. daily files)StateTracker's cross-process lock is advisory: it only protects writers that go throughStateTrackeritself, not unrelated programs editing the JSON file directly- Designed for small to medium workloads, not high-throughput logging
(every write does a
flush(), so very hot loops will be I/O-bound)
Running Tests
pip install -e ".[dev]"
pytest
Roadmap / TODO
- Time-based log rotation (daily/hourly) as an alternative to size-based
- Improve type hints and documentation
- Optional structured (JSON) log output
License
This project is licensed under the MIT License.
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 slinfra-1.0.1.tar.gz.
File metadata
- Download URL: slinfra-1.0.1.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f28ba6115b9b85b3dffc305d8d899f561e28faa9065ed3bdcdc0bc86957d9140
|
|
| MD5 |
f947c7f564eb1e77e5720180789f421d
|
|
| BLAKE2b-256 |
e33896fe5356ff6bb05d1cd9236edfda50e530bc7f8983f80849f48fc9a07b3b
|
File details
Details for the file slinfra-1.0.1-py3-none-any.whl.
File metadata
- Download URL: slinfra-1.0.1-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
16b22763fc2142d1e3c38d16749da56732e19531236e33c87695a0c1189a9b86
|
|
| MD5 |
ab5759dc6010ba457dc743e9d47bdd55
|
|
| BLAKE2b-256 |
1965f42033ac923487ab23f90f3a506e97be168af33dfbdb826f6532bb5d35d9
|