操作日志 Log
模块位于 src/core/Log。通过 Application.registry(Log()) 注册后,路由上的 @Log / option.log 才会真正写入 sys_log。
异步、不影响主业务
@Log 只声明配置;真正写库在响应发送之后由框架调度:
- 业务 Handler 执行完毕并
response.send finally里用setImmediate+void recordOperationLog(...)(不 await)- 落库失败只
console.error,不会改写已返回的业务结果
因此加 @Log 不会拖慢接口耗时(除极短的同步调度开销外)。
注册方式
ts
import Log from '@/core/Log'
Application.registry(Auth({ /* ... */ }))
.registry(Log())
// 或 Log({ enabled: true, silent: false, excludeParamNames: ['token'] })
.start()未注册时,即使写了 @Log 也不会落库。
装饰器写法
从 @/core/Application 导入:
ts
import { Log, BusinessType, PostController } from '@/core/Application'
@PostController('/create')
@Log({ title: '创建用户', business: BusinessType.CREATE })
async create(@RequestBody() body: CreateUserDTO) {
await this.userService.createUser(body)
return null
}| 字段 | 说明 | 默认 |
|---|---|---|
title | 操作标题 | '' |
business | 业务类型序数(见下表) | OTHER |
oper | 操作端:OperType.PC / MOBILE / OTHER | PC |
excludeParamNames | 额外排除的参数名 | 密码类字段已默认排除 |
ignore | 为 true 时本接口不记日志 | false |
BusinessType
| 常量 | 值 | 含义 |
|---|---|---|
CREATE | 0 | 新增 |
UPDATE | 1 | 修改 |
DELETE | 2 | 删除 |
READ | 3 | 详情 |
LIST | 4 | 列表/分页 |
UPLOAD | 5 | 上传 |
EXPORT | 6 | 导出 |
IMPORT | 7 | 导入 |
LOGIN | 8 | 登录 |
OTHER | 9 | 其它 |
函数式写法
ts
Application.POST('/api/log/page', handler, {
auth: { roles: ['ROLE_admin'] },
paramType: ParamType.BODY,
log: { title: '操作日志分页', business: BusinessType.LIST },
})传 log: {} 即启用默认配置。
落库字段
- 操作人取自 Auth 会话的
username/avatar(登录时建议传入 profile) - 常见字段:
title、params、response、status、timeLong、ip、url、errorMessage等
管理端查询 / 导出见 Log API。