1.0.0 • Published 5 years ago

@nioh/logger v1.0.0

Weekly downloads
1
License
ISC
Repository
-
Last release
5 years ago

中间件: elk

安装

Install with npm

npm i @server/elk --registry=http://npm.zkh360.com

说明

日志中间件,elk

使用

import * as path from 'path'
import * as Koa from 'koa'
import * as env from '@server/env'
import elk from '@server/elk'

const app = new Koa()

// 初始化 ELK 配置,/zkh/log/ 为固定目录,不可修改
const project = 'your-project-name'

app.use(elk({
  project,
  filename: env.LOCAL ? path.resolve(__dirname, '../log/logstash.log') : `/zkh/log/${project}/logstash.log`
}))

// debug, warn, error
app.use((ctx, next) => {
  
  // eg: 请求响应大于 300 ms, 可记录一条 Warning 日志
  ctx.elk.warn({
    message: 'slow response',
    type: 'api',
  })

  // eg: 当 catch 了某个错误,可记录一条 Error 日志
  ctx.elk.error({
    message: err.message,
    type: 'api',
  })

  await next()
})

app.listen(3000)

日志:log example

app.on('error', (err, ctx) => {
  debug('APP ERROR: ', ctx)
  ctx && ctx.elk && ctx.elk.error({
    message: err.message,
    type: 'app'
  })
})

ChangeLog

Tests

npm test

License

2019@ZKH-FE

1.0.0

5 years ago