【问题标题】:How to create log file in node js getting 502 error?如何在节点 js 中创建日志文件得到 502 错误?
【发布时间】:2019-09-18 23:06:03
【问题描述】:

您能告诉我如何在根目录中使用 Winston 创建日志文件吗? 我正在使用这个包 https://www.npmjs.com/package/winston

我们可以在文件大小增加到 5kb 或 1 小时后创建多个文件吗?

这是我的代码 https://codesandbox.io/s/beautiful-kapitsa-j1sku

const winston = require("winston");
const moment = require("moment");
const path = require("path");
const PROJECT_ROOT = path.join(__dirname, "..");

const consoleLogger = new winston.transports.File({
  timestamp: function() {
    const today = moment();
    return today.format("DD-MM-YYYY h:mm:ssa");
  },
  colorize: true,
  level: "debug"
});

const logger = winston.createLogger();
//   {
//   transports: [consoleLogger]
// }

logger.add(
  new winston.transports.File({
    filename: "test.log",
    maxsize: "10kb"
  })
);

if (process.env.NODE_ENV === "production") {
  logger.transports.console.level = "info";
}
if (process.env.NODE_ENV === "development") {
  logger.transports.console.level = "debug";
}

module.exports.info = function() {
  logger.info.apply(logger, formatLogArguments(arguments));
};
module.exports.log = function() {
  logger.log.apply(logger, formatLogArguments(arguments));
};
module.exports.warn = function() {
  logger.warn.apply(logger, formatLogArguments(arguments));
};
module.exports.debug = function() {
  logger.debug.apply(logger, formatLogArguments(arguments));
};
module.exports.verbose = function() {
  logger.verbose.apply(logger, formatLogArguments(arguments));
};

module.exports.error = function() {
  logger.error.apply(logger, formatLogArguments(arguments));
};

function formatLogArguments(args) {
  args = Array.prototype.slice.call(args);
  const stackInfo = getStackInfo(1);

  if (stackInfo) {
    const calleeStr = `(${stackInfo.relativePath}:${stackInfo.line})`;
    if (typeof args[0] === "string") {
      args[0] = args[0] + " " + calleeStr;
    } else {
      args.unshift(calleeStr);
    }
  }
  return args;
}

function getStackInfo(stackIndex) {
  const stacklist = new Error().stack.split("\n").slice(3);
  // http://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
  // do not remove the regex expresses to outside of this method (due to a BUG in node.js)
  const stackReg = /at\s+(.*)\s+\((.*):(\d*):(\d*)\)/gi;
  const stackReg2 = /at\s+()(.*):(\d*):(\d*)/gi;

  const s = stacklist[stackIndex] || stacklist[0];
  const sp = stackReg.exec(s) || stackReg2.exec(s);

  if (sp && sp.length === 5) {
    return {
      method: sp[1],
      relativePath: path.relative(PROJECT_ROOT, sp[2]),
      line: sp[3],
      pos: sp[4],
      file: path.basename(sp[2]),
      stack: stacklist.join("\n")
    };
  }
}

logger.exitOnError = false;

收到错误502

【问题讨论】:

    标签: javascript node.js node-modules


    【解决方案1】:

    您必须在bytes 中设置maxsize 选项,因此对于 5kb,它将是:

    logger.add(
      new winston.transports.File({
        filename: "test.log",
        maxsize: 5 * 1024
      })
    );
    

    如果您想根据时间轮换日志,请查看社区托管软件包,例如 this

    【讨论】:

    • cannot log to file without filename or stream
    • 删除consoleLogger,您在其中创建一个没有filename 的文件传输
    猜你喜欢
    • 2023-03-07
    • 2020-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-14
    • 1970-01-01
    相关资源
    最近更新 更多