如何用附加变量包装多个函数

How to wrap multiple function with additional variables

你好,我有这样的文件

const logger = require("./logger");
const { asyncLocalStorage } = require("../localStorage/storage");

const getContextData = () => {
  const roomName = asyncLocalStorage.getStore()?.get("roomName") || "";
  const userId = asyncLocalStorage.getStore()?.get("userId") || "";
  const userName = asyncLocalStorage.getStore()?.get("userName") || "";

  return { roomName, userId, userName };
};

const logInfo = (message, additionalData) => {
  const { roomName, userId, userName } = getContextData();
  logger.info(message, { roomName, userId, userName, additionalData });
};

const logWarn = (message, additionalData) => {
  const { roomName, userId, userName } = getContextData();
  logger.warn(message, { roomName, userId, userName, additionalData });
};

const logError = (message, additionalData) => {
  const { roomName, userId, userName } = getContextData();
  logger.error(message, { roomName, userId, userName, additionalData });
};

module.exports = {
  logInfo,
  logWarn,
  logError,
};

这基本上是用我的附加数据包装 winston 日志函数,我不喜欢那样,这是多余的。我的意思是这三个功能看起来基本相同。当我想要改变一些东西时,所有添加另一个级别的日志记录它会变得混乱。

有没有什么办法可以做到这样

func wrapper() {
  const { roomName, userId, userName } = getContextData();
}
const logWarn = (message, additionalData) => {
  logger.warn(message, { roomName, userId, userName, additionalData });
};
logWarn = wrapper(logWarn);

不必完全像这样。但只是我正在寻找一种比现在更干净的方法

因为它们都是一样的,你可以生成它们:

const exports = {};

["info", "warn", "error"].forEach(level => {
  // create the function name from level: logInfo, logWarn, logError
  const funcName = "log" + level.charAt(0).toUpperCase() + level.slice(1);
  // create the function itself and store it in the export object
  exports[funcName] = (message, additionalData) => {
    const { roomName, userId, userName } = getContextData();
    // since I don't know whether or not `logger` uses `this`, it is best to
    // be careful and preserve it, because it would be lost if we wrote simply
    // logger[level](message, { roomName, userId, userName, additionalData });
    logger[level].call(logger, message, { roomName, userId, userName, additionalData });
  };
};

module.exports = exports;

我认为这可能比下面更清晰,但我只想让您了解我将如何处理这个问题。

const logAll = (message, additionalData, messageType) => {
  const { roomName, userId, userName } = getContextData();
  if (messageType == 'info') {
      logger.info(message, { roomName, userId, userName, additionalData });
  }
  if (messageType == 'warn') {
      logger.warn(message, { roomName, userId, userName, additionalData });
  }
  if (messageType == 'error') {
      logger.error(message, { roomName, userId, userName, additionalData });
  }
};

您可以拥有一个可以调用 logger.warn、logger.info 等的通用函数,并在该函数中检索一次上下文数据。

const getContextData = () => {
  const roomName = asyncLocalStorage.getStore()?.get("roomName") || "";
  const userId = asyncLocalStorage.getStore()?.get("userId") || "";
  const userName = asyncLocalStorage.getStore()?.get("userName") || "";

  return { roomName, userId, userName };
};

const logSomething = (message, additionalData, type) => {
  const { roomName, userId, userName } = getContextData();
  switch (type) {
    case "info":
    case "warn":
    case "error":
      logger[type](message, {roomName, userId, userName, additionalData});
      break;
    default:
      // error invalid "type"
  }
}

const logInfo = (message, additionalData) => logSomething(message, additionalData, "info");
const logWarn = (message, additionalData) => logSomething(message, additionalData, "warn");
const logError = (message, additionalData) => logSomething(message, additionalData, "error");

module.exports = {
  logInfo,
  logWarn,
  logError,
};