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 Winstonjs将json打印到控制台_Node.js_Logging_Winston - Fatal编程技术网

Node.js Winstonjs将json打印到控制台

Node.js Winstonjs将json打印到控制台,node.js,logging,winston,Node.js,Logging,Winston,logger.js var winston = require('winston'); var logger = new winston.createLogger({ transports: [ new winston.transports.Console({ level: 'info', handleExceptions: true, json: false, colori

logger.js

var winston = require('winston');

var logger = new winston.createLogger({
    transports: [
        new winston.transports.Console({
            level: 'info',
            handleExceptions: true,
            json: false,
            colorize: true,
            timestamp: true
        }),
        new winston.transports.Console({
            level: 'error',
            handleExceptions: true,
            json: false,
            colorize: true,
            timestamp: true
        }),
    ],
    exitOnError: false
});

module.exports = logger;
module.exports.stream = {
    write: function(message, encoding){
        logger.info(message);
    }
};

无论何时调用
logger.info
logger.error
,它都会将一个json对象记录到屏幕上,不带颜色。导致此问题的记录器有什么问题?时间戳也没有打印在上面。

看起来您将旧语法与v3语法混合在一起。根据这本书,你可以这样写:

const { createLogger, format, transports } = require('winston');
const { combine, timestamp, printf, colorize } = format;

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

const logger = createLogger({
   format: combine(
    colorize(),
    timestamp(),
    myFormat
  ),
  transports: [
    new transports.Console({
        level: 'info',
        handleExceptions: true
    }),
    new transports.Console({
        level: 'error',
        handleExceptions: true
    }),
  ],
  exitOnError: false
});

module.exports = logger;
module.exports.stream = {
    write: function(message, encoding){
        logger.info(message);
    }
};
这将显示时间戳和颜色


还有一个升级指南显示了v2和v3之间的差异。

看起来您将旧语法与v3语法混为一谈。根据这本书,你可以这样写:

const { createLogger, format, transports } = require('winston');
const { combine, timestamp, printf, colorize } = format;

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

const logger = createLogger({
   format: combine(
    colorize(),
    timestamp(),
    myFormat
  ),
  transports: [
    new transports.Console({
        level: 'info',
        handleExceptions: true
    }),
    new transports.Console({
        level: 'error',
        handleExceptions: true
    }),
  ],
  exitOnError: false
});

module.exports = logger;
module.exports.stream = {
    write: function(message, encoding){
        logger.info(message);
    }
};
这将显示时间戳和颜色

还有一个升级指南显示了v2和v3之间的差异