evlog 的审计层不是一个并行系统。审计事件是带有保留 audit 字段的宽事件。所有现有原语——drains、enrichers、redact、tail-sampling——都按原样适用。通过添加1 个 enricher + 1 个 drain 包装器 + 1 个 helper来启用审计日志。
为我的应用添加审计日志
Agent Skills
安装 evlog 技能目录,这样你的助手就可以端到端遵循 build-audit-logs:书面策略、框架接线、withAudit / log.audit、拒绝、脱敏、多租户隔离、防篡改 sink,以及基于 grep 的审查流程。如果你将文件系统 drain 用于审计或通用日志,analyze-logs 会教助手读取 .evlog/logs/ 下的 NDJSON。
npx skills add https://www.evlog.dev
查看 Agent Skills 以获取完整列表。仓库中的技能路径:skills/build-audit-logs、skills/analyze-logs。
为什么需要审计日志?
合规框架(SOC2、HIPAA、GDPR、PCI)要求知道谁在什么时间、从哪里、对哪个资源、做了什么,以及结果如何。evlog 无需第二个日志库就能覆盖这一需求。
快速开始
你已经在使用 evlog 了。只需做三处改动即可添加审计日志:
import { auditEnricher, auditOnly, signed } from 'evlog'
import { createAxiomDrain } from 'evlog/axiom'
import { createFsDrain } from 'evlog/fs'
export default defineNitroPlugin((nitro) => {
nitro.hooks.hook('evlog:enrich', auditEnricher())
nitro.hooks.hook('evlog:drain', createAxiomDrain())
nitro.hooks.hook('evlog:drain', auditOnly(
signed(createFsDrain({ dir: '.audit' }), { strategy: 'hash-chain' }),
{ await: true },
))
})
export default defineEventHandler(async (event) => {
const log = useLogger(event)
const user = await requireUser(event)
const invoice = await refundInvoice(getRouterParam(event, 'id'))
log.audit({
action: 'invoice.refund',
actor: { type: 'user', id: user.id, email: user.email },
target: { type: 'invoice', id: invoice.id },
outcome: 'success',
reason: 'Customer requested refund',
})
return { ok: true }
})
import { withEvlog, useLogger } from '@/lib/evlog'
export const POST = withEvlog(async (req, { params }) => {
const log = useLogger()
const user = await requireUser(req)
const invoice = await refundInvoice(params.id)
log.audit({
action: 'invoice.refund',
actor: { type: 'user', id: user.id, email: user.email },
target: { type: 'invoice', id: invoice.id },
outcome: 'success',
reason: 'Customer requested refund',
})
return Response.json({ ok: true })
})
import type { EvlogVariables } from 'evlog/hono'
import { Hono } from 'hono'
const app = new Hono<EvlogVariables>()
app.post('/invoices/:id/refund', async (c) => {
const log = c.get('log')
const user = await requireUser(c)
const invoice = await refundInvoice(c.req.param('id'))
log.audit({
action: 'invoice.refund',
actor: { type: 'user', id: user.id, email: user.email },
target: { type: 'invoice', id: invoice.id },
outcome: 'success',
reason: 'Customer requested refund',
})
return c.json({ ok: true })
})
import type { Request, Response } from 'express'
app.post('/invoices/:id/refund', async (req: Request, res: Response) => {
const log = req.log
const user = await requireUser(req)
const invoice = await refundInvoice(req.params.id)
log.audit({
action: 'invoice.refund',
actor: { type: 'user', id: user.id, email: user.email },
target: { type: 'invoice', id: invoice.id },
outcome: 'success',
reason: 'Customer requested refund',
})
res.json({ ok: true })
})
import { audit } from 'evlog'
audit({
action: 'invoice.refund',
actor: { type: 'system', id: 'billing-worker' },
target: { type: 'invoice', id: 'inv_889' },
outcome: 'success',
reason: 'Auto-refund triggered by chargeback webhook',
})
{
"level": "info",
"service": "billing-api",
"method": "POST",
"path": "/api/invoices/inv_889/refund",
"status": 200,
"duration": "84ms",
"requestId": "a566ef91-7765-4f59-b6f0-b9f40ce71599",
"audit": {
"action": "invoice.refund",
"actor": { "type": "user", "id": "usr_42", "email": "demo@example.com" },
"target": { "type": "invoice", "id": "inv_889" },
"outcome": "success",
"reason": "Customer requested refund",
"version": 1,
"idempotencyKey": "ak_8f3c4b2a1e5d6f7c",
"context": {
"requestId": "a566ef91-7765-4f59-b6f0-b9f40ce71599",
"ip": "203.0.113.7",
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36"
}
}
}
就是这样。审计事件:
- 与其余日志一起沿着相同的宽事件管道流动。
- 会在 tail sampling 之后始终保留。
- 会同时进入你的主 drain(Axiom)以及一个专用的、已签名的、仅追加的 sink(FS journal)。
- 通过
auditEnricher自动携带requestId、traceId、ip和userAgent。
组合方式
每一层都是可选接入并可替换的。除了 log.audit、auditEnricher 以及 auditOnly / signed 之外,其他所有节点都与常规宽事件共享。
- log.audit / audit / withAudit audit
callsite
- set event.audit audit
reserved field
- force-keep tail-sample audit
never dropped
- auditEnricher() audit
+ requestId · ip · ua
- redact + auditRedactPreset shared
PII scrubbed
Axiom · Datadog · Sentry · …
hash-chain · WORM · 7y retention