pinojs / pino-http

🌲 high-speed HTTP logger for Node.js
MIT License
539 stars 117 forks source link

node:util module not found #306

Open vijeetvinod1204 opened 11 months ago

vijeetvinod1204 commented 11 months ago

I have no idea how to resolve this:

image
jsumners commented 11 months ago

What is that from?

vijeetvinod1204 commented 11 months ago

This is my middleware:

const pino = require("pino");
const globalLogger = require("../helpers/globalLogger");
global.req = undefined;
const genRequestId = (req, res) => {
  let id = req.get("X-Request-Id");
  if (!id) {
    id = (Date.now() + Math.random()).toString(36).replace(".", "-");
  }
  req.id = id;
  return id;
};
const httpLoggerMiddleware = pinoHttp({
  genReqId: () => undefined,
  logger: globalLogger,
  serializers: {
    req(req) {
      req.body = req.raw.body;
      return req;
    },
  },
  wrapSerializers: true,
  customLogLevel: function (req, res, err) {
    if (res.statusCode >= 400 && res.statusCode < 500) {
      return "warn";
    } else if (res.statusCode >= 500 || err) {
      return "error";
    } else if (res.statusCode >= 300 && res.statusCode < 400) {
      return "silent";
    }
    return "info";
  },
  customSuccessMessage: function (req, res) {
    if (res.statusCode === 404) {
      return "resource not found";
    }
    return `${req.method} completed`;
  },
  customReceivedMessage: function (req, res) {
    return "request received: " + req.method;
  },
  customErrorMessage: function (req, res, err) {
    return "request errored with status code: " + res.statusCode;
  },
  customProps: function (req, res) {
    return {
      userid: req.headers.userid,
      reqId: genRequestId(req, res),
    };
  },
});
module.exports = httpLoggerMiddleware;
vijeetvinod1204 commented 11 months ago

My application won't deploy because of the above error. Not sure where it is coming from.

vijeetvinod1204 commented 11 months ago
const requestLoggerWrapper = (level, req, ...data) => {
  if (!req) {
    return globalLogger[level]({
      message: data.shift(),
      extraParams: JSON.stringify(data),
    });
  }
  if (data[0].length === 1) {
    return globalLogger[level]({
      reqId: req.id,
      userid: req.headers.userid,
      message: data[0],
    });
  }
  return globalLogger[level]({
    reqId: req.id,
    userid: req.headers.userid,
    message: data.shift(),
    extraParams: JSON.stringify(data),
  });
};
const requestLogger = {
  trace: (req, ...data) => requestLoggerWrapper("trace", req, ...data),
  debug: (req, ...data) => requestLoggerWrapper("debug", req, ...data),
  info: (req, ...data) => requestLoggerWrapper("info", req, ...data),
  warn: (req, ...data) => requestLoggerWrapper("warn", req, ...data),
  error: (req, ...data) => requestLoggerWrapper("error", req, ...data),
  fatal: (req, ...data) => requestLoggerWrapper("fatal", req, ...data),
  log: requestLoggerWrapper,
};
module.exports = requestLogger;
vijeetvinod1204 commented 11 months ago

I think any changes pushed post Oct 26 have caused this.

jsumners commented 11 months ago

Please provide a minimal reproducible example. Doing so will help us diagnose your issue. It should be the bare minimum code needed to trigger the issue, and easily runnable without any changes or extra code.

You may use a GitHub repository to host the code if it is too much to fit in a code block (or two).