@ elastic/ ecs- 帮助器
帮别人的人 ecs-logb-nodejs 软件包你应该 不直接使用此软件包.
安装
npm install @elastic/ecs-helpers
API 密码
version
目前支持的版本 弹性通用外观。 。 。 .
formatError(obj, err) -> bool
添加函数 ECS 错误字段
给定的 Error 对象。 如果给定的话, 它返回真实 err 错误
对象可以处理.
const { formatError } = require('@elastic/ecs-helpers')
const logRecord = { msg: 'oops', /* ... */ }
formatError(logRecord, new Error('boom'))
console.log(logRecord)
将显示:
{
msg: 'oops',
error: {
type: 'Error',
message: 'boom',
stack_trace: 'Error: boom\n' +
' at REPL30:1:26\n' +
' at Script.runInThisContext (vm.js:133:18)\n' +
// ...
}
}
ECS 日志库通常使用此程序自动处理 err
元数据字段传递到一个日志语句。 举个例子.
log.warn({err: myErr}, '...') 对于皮诺, log.warn('...', {err: myErr})
为温顿.
formatHttpRequest(obj, req) -> bool
用 http 请求数据增强 ECS 对象的函数 .
给定的请求对象, req,必须是以下之一:
- 节点.js的核心
http.IncomingMessage, (中文(简体) ), - Express 请求对象 延伸来信,或
- (单位:千美元) hopi 请求对象。 。 。 .
如果给定的函数返回真 req 是一个请求对象 它可以
进程。 请注意,目前这显然不处理
http.ClientRequest
从 http.request()。 。 。 .
const http = require('http')
const { formatHttpRequest } = require('@elastic/ecs-helpers')
http.createServer(function (req, res) {
res.end('hi')
const obj = {}
formatHttpRequest(obj, req)
console.log('obj:', JSON.stringify(obj, null, 4))
}).listen(3000)
运行此并请求通过 curl http://localhost:3000/ 将
打印接近 :
obj: {
"http": {
"version": "1.1",
"request": {
"method": "get",
"headers": {
"host": "localhost:3000",
"accept": "*/*"
}
}
},
"url": {
"full": "http://localhost:3000/",
"path": "/"
},
"client": {
"address": "::1",
"ip": "::1",
"port": 61969
},
"user_agent": {
"original": "curl/7.64.1"
}
}
formatHttpResponse(obj, res)
用 http 响应数据增强 ECS 对象的函数 .
给定的请求对象, req,必须是以下之一:
- 节点.js的核心
http.ServerResponse, (中文(简体) ), - Express 响应对象 扩展服务器响应,或
- (单位:千美元) 霍比语name 请求 对象
如果给定的函数返回真 res 是一个反应对象,它可以
进程。 请注意,目前这显然不处理
http.IncomingMessage
这就是我们的观点
“答复”事件 页:1
客户端 http.request()
const http = require('http')
const { formatHttpRequest } = require('@elastic/ecs-helpers')
http.createServer(function (req, res) {
res.setHeader('Foo', 'Bar')
res.end('hi')
const obj = {}
formatHttpResponse(obj, res)
console.log('obj:', JSON.stringify(obj, null, 4))
}).listen(3000)
运行此并请求通过 curl http://localhost:3000/ 将
打印接近 :
rec: {
"http": {
"response": {
"status_code": 200,
"headers": {
"foo": "Bar"
}
}
}
}
许可证
该软件根据 Apache 2 许可证。 。 。 .