0

我正在尝试使用 cls-proxify 为每个请求创建 bunyan 实例,但在此代码上:

const Koa = require('koa');
const bunyan = require('bunyan');
const {clsProxify} = require('cls-proxify');
const {clsProxifyKoaMiddleware} = require('cls-proxify/integration/koa');

const app = new Koa();

const logger = bunyan.createLogger({
  name: 'cls-test',
  streams: [
    {
      level: 'error',
      stream: process.stderr,
    },
  ]
});

const loggerCls = clsProxify('clsKeyLogger', logger);

app.use(clsProxifyKoaMiddleware('clsKeyLogger', (ctx) => {
  return logger.child({
    requestId: Math.random(),
    level: 'error',
  });
}));

app.use(async (ctx, next) => {
  try {
    await next();
  } catch (err) {
    loggerCls.error(err); // TypeError [ERR_INVALID_ARG_TYPE]...
  }
});

app.use(async ctx => {
  ctx.throw(new Error('boom'));
});

app.listen(3000, () => { console.log('Server listening on port', 3000); });

我有一个错误:

TypeError [ERR_INVALID_ARG_TYPE]:“块”参数必须是字符串类型或 Buffer 或 Uint8Array 的实例。在新的 NodeError 处收到 undefined (node:internal/errors:278:15)

4

0 回答 0