Зачем печатать журнал в формате json в формате node js - PullRequest
0 голосов
/ 08 января 2020

Почему журналы печатаются в формате json без имени файла (где записывается состояние журнала). Я слушаю winston отсюда

https://egghead.io/lessons/node-js-add-logging-to-a-node-js-application-using-winston

весь видеокод здесь

https://github.com/markbarton/nodejswinston/blob/master/logger.js

Я выполняю все шаги, но мои журналы не распечатываются, как ожидалось в видео

вот мой код

https://codesandbox.io/s/charming-cerf-z1lm8

ожидается имя файла с отметкой времени, затем сообщение , как показано на видео

Не могли бы вы сказать мне, где я делаю неправильно?

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

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

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

if (process.env.NODE_ENV === "production") {
  logger.transports.console.level = "info";
}
console.log("==cc==");
if (process.env.NODE_ENV === "development") {
  console.log("====");
  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;
module.exports.log = logger;
...