Winston 3.2.1版本未创建日志文件

时间:2019-07-01 06:14:04

标签: node.js winston

我试图使用节点js.winston 2.4.0版本创建winston(记录器文件),它已成功在日志文件中存储了信息和错误。但是最新版本创建了日志文件,但无法存储错误和信息消息。如何修复

Winston.js

var winston = require('winston');

const logger = winston.createLogger({
    level: 'info',
    format: winston.format.json(),
    defaultMeta: { service: 'user-service' },
    transports: [
      //
      // - Write to all logs with level `info` and below to `combined.log` 
      // - Write all logs error (and below) to `error.log`.
      //
      new winston.transports.File({ filename: 'error.log', level: 'error' }),
      new winston.transports.File({ filename: 'combined.log' })
    ]
  });

  //
  // If we're not in production then log to the `console` with the format:
  // `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
  // 
  if (process.env.NODE_ENV !== 'production') {
    logger.add(new winston.transports.Console({
      format: winston.format.simple()
    }));
  }

app.js

/**
 * @fileoverview Root file of the application.
 */

// Import ExpressJS
const winston = require('winston');
const express = require('express');
const app = express();
// StartUp Processes
require('./startup/logging_error_startup');
require('./startup/routes_startup')(app);
require('./startup/check_security_key_startup')();
require('./startup/validation_startup')();
require('./startup/swagger_startup')(app);
require('./startup/production_startup')(app);
// Start the server
const port = process.env.PORT || 4202;
app.listen(port, () => winston.info(`Server Started and Listening on port ${port}`));

我得到了输出

winston] Attempt to write logs with no transports {"message":"Server Started and Listening on port 4202","level":"info"}

2 个答案:

答案 0 :(得分:1)

您需要将记录器对象从Winston.js文件导入app.js。在您的app.js代码中,Winston导入适用于npm软件包,而不适用于在Winston.js中创建的记录器对象。

在Winston.js中,导出记录器对象:

var winston = require('winston');

const logger = winston.createLogger({
    level: 'info',
    format: winston.format.json(),
    defaultMeta: { service: 'user-service' },
    transports: [
      //
      // - Write to all logs with level `info` and below to `combined.log` 
      // - Write all logs error (and below) to `error.log`.
      //
      new winston.transports.File({ filename: 'error.log', level: 'error' }),
      new winston.transports.File({ filename: 'combined.log' })
    ]
  });

  //
  // If we're not in production then log to the `console` with the format:
  // `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
  // 
  if (process.env.NODE_ENV !== 'production') {
    logger.add(new winston.transports.Console({
      format: winston.format.simple()
    }));
  }

module.exports = logger;

在app.js中,导入后使用该记录器对象。

const logger = require('./Winston'); //assuming Winston.js is in the same folder level as app.js
const express = require('express');
const app = express();
// StartUp Processes
require('./startup/logging_error_startup');
require('./startup/routes_startup')(app);
require('./startup/check_security_key_startup')();
require('./startup/validation_startup')();
require('./startup/swagger_startup')(app);
require('./startup/production_startup')(app);
// Start the server
const port = process.env.PORT || 4202;
app.listen(port, () => logger.info(`Server Started and Listening on port ${port}`));

答案 1 :(得分:0)

或者,您可以通过{p>来修改winston.js中的默认记录器

var winston = require('winston');

const newLogger = winston.createLogger({
    level: 'info',
    format: winston.format.json(),
    defaultMeta: { service: 'user-service' },
    transports: [.........

winston.add(newLogger);

代替

var winston = require('winston');

const logger = winston.createLogger({....

module.exports = logger

并使用以下命令将默认记录器导入每个文件中

const logger = require('winston')

logger.info('log');

每次将记录器导入到不同文件时都无需处理相对路径。