Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Winston-未添加时间戳_Node.js_Logging_Winston - Fatal编程技术网

Node.js Winston-未添加时间戳

Node.js Winston-未添加时间戳,node.js,logging,winston,Node.js,Logging,Winston,我正在使用Winston作为NodeJS项目的记录器。我尝试向日志消息添加时间戳,并将Winston配置为以非Json方式编写控制台日志消息,但没有成功。我的配置如下 const appRoot = require('app-root-path'); const winston = require('winston'); const options = { file: { level: 'info', filename: `${appRoot}/logs

我正在使用Winston作为NodeJS项目的记录器。我尝试向日志消息添加时间戳,并将Winston配置为以非Json方式编写控制台日志消息,但没有成功。我的配置如下

const appRoot = require('app-root-path');
const winston = require('winston');

const options = {
    file: {
        level: 'info',
        filename: `${appRoot}/logs/app.log`,
        timestamp: true,
        handleExceptions: true,
        json: true,
        maxsize: 5242880, // 5MB
        maxFiles: 15,
        colorize: false,
    },
    console: {
        level: 'debug',
        timestamp: true,
        handleExceptions: true,
        json: false,
        colorize: true,
    },
};

const logger = winston.createLogger({
    transports: [
        new winston.transports.File(options.file),
        new winston.transports.Console(options.console)
    ],
    exitOnError: false,
});

module.exports = logger;
这是我在其他文件中导入Winston的方式(Winston配置文件位于我项目的根目录下):


知道它为什么起作用吗?

我在winston 3上犯了这个错误。查看指定记录器格式的文档。这将解决问题。

我在winston 3上犯了这个错误。查看指定记录器格式的文档。这将解决问题。

这是一个带时间戳的工作winston logger模块:

const { createLogger, format, transports } = require("winston");
const { combine, timestamp, label, printf } = format;
const appRoot = require("app-root-path");

const myFormat = printf(({ level, message, label, timestamp }) => {
    return `${timestamp} ${level}: ${message}`;
});

const options = {
    file: {
        level: "info",
        filename: `${appRoot}/logs/app.log`,
        handleExceptions: true,
        json: true,
        maxsize: 5242880, // 5MB
        maxFiles: 5,
        colorize: false,
        timestamp: true,
    },
    console: {
        level: "debug",
        handleExceptions: true,
        json: false,
        colorize: true,
    },
};
const logger = createLogger({
    format: combine(label({ label: this.level }), timestamp(), myFormat),
    transports: [new transports.Console(options.console), new transports.File(options.file)],
});

module.exports = logger;


以下是一个带时间戳的winston logger模块:

const { createLogger, format, transports } = require("winston");
const { combine, timestamp, label, printf } = format;
const appRoot = require("app-root-path");

const myFormat = printf(({ level, message, label, timestamp }) => {
    return `${timestamp} ${level}: ${message}`;
});

const options = {
    file: {
        level: "info",
        filename: `${appRoot}/logs/app.log`,
        handleExceptions: true,
        json: true,
        maxsize: 5242880, // 5MB
        maxFiles: 5,
        colorize: false,
        timestamp: true,
    },
    console: {
        level: "debug",
        handleExceptions: true,
        json: false,
        colorize: true,
    },
};
const logger = createLogger({
    format: combine(label({ label: this.level }), timestamp(), myFormat),
    transports: [new transports.Console(options.console), new transports.File(options.file)],
});

module.exports = logger;


下面是一个帮助在输出(日志文件、控制台)上打印时间戳的示例

用于此示例的版本:

├── express@4.17.1 ├── 快速异步-errors@3.1.1 └── winston@3.3.3

// Declare winston
const winston = require("winston");
// Import all needed using Object Destructuring
const { createLogger, format, transports } = require("winston");
const { combine, timestamp, printf } = format;
// Export the module
module.exports = function (err, req, res, next) {


  const logger = createLogger({
    level: "error",
    format: combine(
      format.errors({ stack: true }), // log the full stack
      timestamp(), // get the time stamp part of the full log message
      printf(({ level, message, timestamp, stack }) => { // formating the log outcome to show/store 
        return `${timestamp} ${level}: ${message} - ${stack}`;
      })
    ),
    transports: [
      new transports.Console(), // show the full stack error on the console
      new winston.transports.File({ // log full stack error on the file
        filename: "logfile.log",
        format: format.combine(
          format.colorize({
            all: false,
          })
        ),
      }),
    ],
  });

  logger.log({
    level: "error",
    message: err,
  });
  // Response sent to client but nothing related to winston
  res.status(500).json(err.message);
};

下面是一个帮助在输出(日志文件、控制台)上打印时间戳的示例

用于此示例的版本:

├── express@4.17.1 ├── 快速异步-errors@3.1.1 └── winston@3.3.3

// Declare winston
const winston = require("winston");
// Import all needed using Object Destructuring
const { createLogger, format, transports } = require("winston");
const { combine, timestamp, printf } = format;
// Export the module
module.exports = function (err, req, res, next) {


  const logger = createLogger({
    level: "error",
    format: combine(
      format.errors({ stack: true }), // log the full stack
      timestamp(), // get the time stamp part of the full log message
      printf(({ level, message, timestamp, stack }) => { // formating the log outcome to show/store 
        return `${timestamp} ${level}: ${message} - ${stack}`;
      })
    ),
    transports: [
      new transports.Console(), // show the full stack error on the console
      new winston.transports.File({ // log full stack error on the file
        filename: "logfile.log",
        format: format.combine(
          format.colorize({
            all: false,
          })
        ),
      }),
    ],
  });

  logger.log({
    level: "error",
    message: err,
  });
  // Response sent to client but nothing related to winston
  res.status(500).json(err.message);
};

const options为奇数,稍后仅使用options.file刚刚更新示例以包含选项。控制台道具:)const options为奇数,稍后仅使用options.file刚刚更新示例以包含选项。控制台道具:)