Skip to main content

钩子函数

SDK提供了丰富的钩子供用户使用。以下将举例一个钩子函数的使用,钩子函数调用时机详见文末。

使用示例

onBeforeSend

该钩子将在发送给日志服务之前执行,用于对上报数据的拦截和修改.

const aegis = new Aegis({
id : 'xxx',
appKey: 'xxxx',
onBeforeSend: (logs, aegis) => {
return logs.filter(log => log.type === 'api');
},
plugin: {
pagePerformance: true, // 性能
error: true, // 错误上报
api: { // API上报
apiDetail: true,
},
},
})

所有的钩子的参数都是一样的

onBeforeProcess(logs, Aegis)

logs的示例内容如下,可以在各个钩子的调用时机对其进行处理。

{
aegisv2_goto: '3851c4e4a9399dc1',
timestamp: 1754535777595,
sysMemory: 30967548,
sysMemoryPercent: 92.29,
sysCPUPercent: 5.87,
mainMemory: 110524,
mainMemoryPercent: 0.33,
mainCPUPercent: 0,
type: 'processPerformance',
level: 'info',
plugin: 'processPerformance',
snapshootInfo: '{}'
}

钩子函数调用时机

钩子函数名称描述
onNewAegis该钩子将会在Aegis 初始化时执行
onConfigChange该钩子将会在使用实例方法setConfig时执行
onBeforeCollect该钩子将会在插件开始收集log 之前执行
onBeforeProcess该钩子将会在插件格式化处理log 之前执行
onProcessed该钩子将会在插件格式化处理log后,开始发送给日志服务之前执行
onBeforeSend该钩子将在发送给日志服务之前执行
onSended该钩子将在发送给日志服务之后执行,基本等同于原来的afterRequest
onBeforeDestroy该钩子将在调用实例方法destroy,执行销毁实例逻辑之前执行
onDestroyed该钩子将在调用实例方法destroy,执行销毁实例逻辑之后执行