Skip to main content

Store you operation log to different backend.

Project description

Project icon

Loggage: easy and happy

English | 中文

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

  1. Install uv (A fast Python package installer and resolver):
curl -LsSf https://astral.sh/uv/install.sh | sh
  1. Clone the repository:
git clone https://github.com/jience/loggage.git
cd loggage
  1. Create a new virtual environment and activate it:
uv venv
source .venv/bin/activate  # On Unix/macOS
# Or on Windows:
# .venv\Scripts\activate
  1. Install dependencies:
uv pip install -r requirements.txt

Configuration

Follow these steps to set up your configuration:

  1. Create a config.yaml file in the config directory (you can copy from the example):
cp config/config.example.yaml config/config.yaml
  1. Edit config/config.yaml to 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 src.core.logger import AsyncOperationLogger
from src.core.models import OperationLog, LogDetailItem
from src.utils.config import load_config
from src.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

loggage-0.3.0.tar.gz (17.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

loggage-0.3.0-py3-none-any.whl (18.8 kB view details)

Uploaded Python 3

File details

Details for the file loggage-0.3.0.tar.gz.

File metadata

  • Download URL: loggage-0.3.0.tar.gz
  • Upload date:
  • Size: 17.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.1

File hashes

Hashes for loggage-0.3.0.tar.gz
Algorithm Hash digest
SHA256 33f30d4a737310e35237318a45a0b06d1d1a62a4500215e9f2dfec71eaf5c3d1
MD5 d5ccc6a9600b403adb387eb7825685a9
BLAKE2b-256 fd130e7c451f6cb009c79a27471513a7e8cc71a5240aed0e03ad599e641f2216

See more details on using hashes here.

File details

Details for the file loggage-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: loggage-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 18.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.1

File hashes

Hashes for loggage-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f4e97bedd32e633fdc043fd37ba30881d039389035610503c218398c46f85c69
MD5 d2ddfc3f657750a2d4354b83898a4061
BLAKE2b-256 e8b713a3e0e3a57459ce7e46ef926e4932633047f297391b90932f409d67648b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page