审计日志客户端
用于审计日志服务的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许可证)
- 更新了初始化客户端的参数说明
Metadata
Release files for audit-log-client 1.4.14
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| audit_log_client-1.4.14.tar.gz | 11.8 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| audit_log_client-1.4.14-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 22.6 kB
Release files / audit_log_client-1.4.14.tar.gz
| Download URL | audit_log_client-1.4.14.tar.gz |
|---|---|
| Size | 11.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
46dc8e46560529f1af35ddbd990c58113b9363992aff9a1ac3c5919529968908
|
|
BLAKE2b-256 checksum How to use checksums |
d5f733775ba83489ad6b52c003f0fc2d6cb3d62f1af2fbe39c907e1050760689
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.12
|
Release files / audit_log_client-1.4.14-py3-none-any.whl
| Download URL | audit_log_client-1.4.14-py3-none-any.whl |
|---|---|
| Size | 10.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9cdfdf8fb9a93803af4a383c0067ed63ccdf40ca0d5c85f279daca8ca2400650
|
|
BLAKE2b-256 checksum How to use checksums |
e4d402b41d577f6819301268448b429c6b5a66512500bbcc05c73f4999699325
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.12
|
Security for audit-log-client 1.4.14
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.