UNPKG

co-log

Version:

Port of log4js work for node.RequestId is supported.

41 lines (38 loc) 921 B
# co-log 基于log4js的nodejs日志库,支持requestId输出,支持koa. ## Install ```git npm i co-log ``` ## Usage ```js import {Log} from 'co-log'; let log = new Log({path: '/opt/yourpath'}); ``` ## Options 初始化参数 - `'path'` - `string`类型,日志存放路径 ```js const options= { path: '/opt/yourpath' } ``` ## RequestId设置 ### `log.setRequestId(string)`function中设置requestId,所有`console.log`打印都带requestId ```js async function test() { log.setRequestId('ae21f206375744f0b6188f45b7f856d3') console.log({a:1}) //[2022-11-24 23:18:47.160] [INFO] - ae21f206375744f0b6188f45b7f856d3 {a:1} } ``` ### `log.setRequestIdForKoa()` 返回koa框架的中间件,所有api请求都自带requestId ```js const Koa = require('koa') const app = new Koa() //第一个引入log的中间件 app.use(log.setRequestIdForKoa()); ... ```