Store you operation log to different backend.
Project description
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())
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 loggage-0.5.0.tar.gz.
File metadata
- Download URL: loggage-0.5.0.tar.gz
- Upload date:
- Size: 16.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d7a40bb175aa330192378b07dd68040c62f775fe74ed648559b117e6e15253fd
|
|
| MD5 |
2b59c2c1cc972fc85a39e6bb194b22c4
|
|
| BLAKE2b-256 |
6963ab15cff17b006732108048c5cd244ac6f8ecb600d00b83186781676f1b47
|
File details
Details for the file loggage-0.5.0-py3-none-any.whl.
File metadata
- Download URL: loggage-0.5.0-py3-none-any.whl
- Upload date:
- Size: 19.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a0034f94898b35393e7666a4c609bd5a7d8fc7f7b767487004194460e72da761
|
|
| MD5 |
38903cf67e16310ac750f57f76bf63b6
|
|
| BLAKE2b-256 |
4d030a5484df40d8c0bd284c9744bcaa0a07977c71a92b6534eeccd196290343
|