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
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.10.tar.gz.
File metadata
- Download URL: audit_log_client-1.4.10.tar.gz
- Upload date:
- Size: 8.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b870f468547ea509863579593613c8f46a77463609253dc25b77dba356ef34d2
|
|
| MD5 |
1308c59b2260cbe8e8592ca87de3755f
|
|
| BLAKE2b-256 |
ef8339008bc0690b0a6e80ff715945ba178066df9733e14bde6733ddce2484d8
|
File details
Details for the file audit_log_client-1.4.10-py3-none-any.whl.
File metadata
- Download URL: audit_log_client-1.4.10-py3-none-any.whl
- Upload date:
- Size: 8.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 |
23d8f37df5423eff6c76a6a46a307417631884697b9eb00410bd4f6caa9a8c0b
|
|
| MD5 |
73bd8e921dfd5d0604b4b1ad409abc81
|
|
| BLAKE2b-256 |
e5d079d4446aed32bd7f0d0e862cd6464d3e507de49454bdbaffde4daca9e40c
|