Skip to main content

Python client for audit logging services

Project description

审计日志客户端

用于审计日志服务的Python客户端,支持同步和异步两种使用模式。

主要功能

  • 双模式支持:同步和异步API
  • 缓冲处理:可配置的内存缓冲区
  • 重试机制:指数退避重试策略
  • 降级策略:失败时自动写入本地文件
  • 查询支持:灵活的日志查询功能

安装

pip install audit-log-client

快速入门

同步客户端

from audit_log_client import SyncAuditLogClient, AuditLog, AuditAction, AuditTarget

# 初始化客户端
client = SyncAuditLogClient(
    base_url="http://audit.service/api",
    app_id="your-app-id",  # 应用ID
    secret_key="your-secret-key"  # 应用密钥
)

# 创建审计日志
log = AuditLog(
    action=AuditAction.UPDATE,
    target_type=AuditTarget.USER,
    user_id="admin",
    description="用户资料更新",
    before={"name": "张三"},
    after={"name": "张三丰"}
)

# 记录日志
client.log(log)

# 关闭客户端
client.close()

异步客户端

import asyncio
from audit_log_client import AsyncAuditLogClient, AuditLog, AuditAction, AuditTarget

async def main():
    # 初始化客户端
    client = AsyncAuditLogClient(
        base_url="http://audit.service/api",
        app_id="your-app-id",  # 应用ID
        secret_key="your-secret-key"  # 应用密钥
    )
    
    # 创建审计日志
    log = AuditLog(
        action=AuditAction.CREATE,
        target_type=AuditTarget.ORDER,
        user_id="sales",
        description="新建订单"
    )
    
    # 记录日志
    await client.log(log)
    
    # 关闭客户端
    await client.shutdown()

asyncio.run(main())

认证方式

新版本已升级认证机制,使用应用ID和应用密钥进行身份验证:

  1. 在请求头中添加:

    • X-App-ID: 您的应用ID
    • X-Secret-Key: 您的应用密钥
  2. 初始化客户端时提供:

    client = SyncAuditLogClient(
        base_url="...",
        app_id="your-app-id",
        secret_key="your-secret-key"
    )
    

查询示例

from datetime import datetime, timedelta

# 查询最近1小时的用户更新日志
end_time = datetime.now()
start_time = end_time - timedelta(hours=1)

logs = client.query_logs(
    action="UPDATE",
    target_type="USER",
    user_id="admin",
    start_time=start_time,
    end_time=end_time
)

for log in logs:
    print(f"{log.timestamp}: {log.description}")

主要更新内容:

  1. 将文档语言切换为中文
  2. 更新了认证方式的说明,强调使用AppID和SecretKey
  3. 修改了示例代码中的描述文本为中文
  4. 添加了专门的认证方式说明章节
  5. 新增了查询示例
  6. 保留了必要的英文术语(如MIT许可证)
  7. 更新了初始化客户端的参数说明

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

audit_log_client-1.4.11.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

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

audit_log_client-1.4.11-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file audit_log_client-1.4.11.tar.gz.

File metadata

  • Download URL: audit_log_client-1.4.11.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for audit_log_client-1.4.11.tar.gz
Algorithm Hash digest
SHA256 1f446950497c20439f908e71128af446e78c72adf8dd7b56b85fbadc85b48283
MD5 43f6085d51fdb4c32b92d1b3f6ce7f65
BLAKE2b-256 b43195480668e5c4bf109bf30c708319b2367b53d583c2c5da7419fd65c51d36

See more details on using hashes here.

File details

Details for the file audit_log_client-1.4.11-py3-none-any.whl.

File metadata

File hashes

Hashes for audit_log_client-1.4.11-py3-none-any.whl
Algorithm Hash digest
SHA256 b3c8d4f19d19346c15d66b4e7e04d203e12e7ffe1fd4eced875f80e3829502e9
MD5 291e6b66bc537bae537cb8656c0457b0
BLAKE2b-256 15189c39ffbe053298400d75be5903449abf173474d2afafebdaca97416ca38d

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