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和应用密钥进行身份验证:
-
在请求头中添加:
X-App-ID: 您的应用IDX-Secret-Key: 您的应用密钥
-
初始化客户端时提供:
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}")
主要更新内容:
- 将文档语言切换为中文
- 更新了认证方式的说明,强调使用AppID和SecretKey
- 修改了示例代码中的描述文本为中文
- 添加了专门的认证方式说明章节
- 新增了查询示例
- 保留了必要的英文术语(如MIT许可证)
- 更新了初始化客户端的参数说明
Project details
Release history Release notifications | RSS feed
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.12.tar.gz
(11.0 kB
view details)
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 audit_log_client-1.4.12.tar.gz.
File metadata
- Download URL: audit_log_client-1.4.12.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
80f4e89973c17263bf432830c6bec9735ab7abc7f8573d31498936a3aefe02bf
|
|
| MD5 |
dda714fbfeffe7a6c036d5b0e16bd984
|
|
| BLAKE2b-256 |
aec92c832609ddf804fbb6403d9ea4f94d7b6627742b5671ac1e613802bcaf5c
|
File details
Details for the file audit_log_client-1.4.12-py3-none-any.whl.
File metadata
- Download URL: audit_log_client-1.4.12-py3-none-any.whl
- Upload date:
- Size: 10.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
15390029d6d8003ec5f7d6f1f14a52bb7cc1d15c8fdfa035cb6a9aeb81847af5
|
|
| MD5 |
0580eae21c246573896140d72886e11c
|
|
| BLAKE2b-256 |
e620908f68b51a3ac22fe93033974de429306a7d5ba37d0eb96bcb52aa5511ff
|