Loggage: easy and happy
Introduction
Loggage is a universal component designed to record operation logs. It enables you to log operational records of your business systems by leveraging decorators or simply invoking regular functions, while maintaining minimal impact on your business code. Additionally, it provides flexibility in storing operation logs, allowing you to choose different storage solutions based on your project needs. Loggage supports a variety of storage options, including relational databases like MySQL, search engines like Elasticsearch, and in-memory data stores like Redis.
Features
- Fully asynchronous: Supports the async/await asynchronous system architecture safely
- Exception safety: Logging operations will not disrupt the API's normal execution
- Type safety: Data validation based on Pydantic models
- High performance: Logging operations are fully asynchronous
- Easy to extend: Factory pattern + standard interface facilitates adding new storage processors (recommended phrasing: plug-in architecture)
- Configuration-driven: Uses YAML-based configuration files for flexible control of storage mechanisms
Installation
- Install uv (A fast Python package installer and resolver):
curl -LsSf https://astral.sh/uv/install.sh | sh
- Clone the repository:
git clone https://github.com/jience/loggage.git
cd loggage
- Create a new virtual environment and activate it:
uv venv
source .venv/bin/activate # On Unix/macOS
# Or on Windows:
# .venv\Scripts\activate
- Install dependencies:
uv pip install -r requirements.txt
Configuration
Follow these steps to set up your configuration:
- Create a
config.yamlfile in theconfigdirectory (you can copy from the example):
cp config/config.example.yaml config/config.yaml
- Edit
config/config.yamlto customize settings:
default_storage: mysql
storages:
mysql:
enabled: true
host: localhost
port: 3306
user: user
password: password
db: db_name
table: operation_log
pool_size: 20
max_overflow: 5
elasticsearch:
enabled: true
hosts: ["http://localhost:9200"]
index: operation-log
timeout: 30
redis:
enabled: false
host: localhost
port: 6379
stream_key: operation_log
Quick Start
One line for run:
python main.py
Usage
import asyncio
from datetime import datetime
from loggage.core.logger import AsyncOperationLogger
from loggage.core.models import OperationLog, LogDetailItem
from loggage.utils import load_config
from loggage.utils.tools import generate_uuid_str
async def main():
config = load_config("config/config.yaml")
async with AsyncOperationLogger(config) as op_logger:
log_detail = LogDetailItem(id=generate_uuid_str(), name="vdi", type="admin")
log_data = OperationLog(
request_id=generate_uuid_str(),
user_id=generate_uuid_str(),
user_name="vdi",
obj_id=generate_uuid_str(),
obj_name="Client-W",
ref_id=generate_uuid_str(),
ref_name="abc",
resource_type="user",
operation_type="business",
action="login",
status="success",
detail=[log_detail],
request_ip="127.0.0.1",
request_params="{}",
interval_time=0,
error_code="",
error_message="",
extra="{}",
response_body="{}",
created_at=datetime.now(),
updated_at=datetime.now(),
)
await op_logger.log(log_data)
# 或批量处理
# await op_logger.log_batch([log_data, log_data, log_data])
if __name__ == "__main__":
asyncio.run(main())
Metadata
Release files for loggage 0.8.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| loggage-0.8.0.tar.gz | 21.0 kB | Without provenance | Details for loggage-0.8.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| loggage-0.8.0-py3-none-any.whl | Python 3 | none | any | Details for loggage-0.8.0-py3-none-any.whl |
Total release size: 46.9 kB
Release files / loggage-0.8.0.tar.gz
| Download URL | loggage-0.8.0.tar.gz |
|---|---|
| Size | 21.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7f8ce317c2295b42e47c945cdcf3dd458ada5eaaf3b09963d1280d07856ad494
|
|
BLAKE2b-256 checksum How to use checksums |
24e3595dd292d45b12d86e12b9b91d84a439e584ebfe67008fcc54fb8cc45c48
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.1
|
Release files / loggage-0.8.0-py3-none-any.whl
| Download URL | loggage-0.8.0-py3-none-any.whl |
|---|---|
| Size | 25.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
45504be4739f7e1cdb4a4e716d2a6d3e90f8ad696d9ff39a17f26fc81a66b181
|
|
BLAKE2b-256 checksum How to use checksums |
12741f25b1a014f2c30f88105ab29aa1eb74403c261fd831752bb3edfee37a27
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.1
|
Security for loggage 0.8.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.