【问题标题】:winston 3.2.1 version is not creating log filewinston 3.2.1 版本不创建日志文件
【发布时间】:2021-10-21 21:32:08
【问题描述】:

我尝试使用节点 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"}

【问题讨论】:

  • 你给日志目录写权限了吗?
  • 如何给日志目录写权限? @narayansharma91
  • chmod - R 777 /your/log/directory(如果您使用的是 ubuntu)
  • 不要破坏您的帖子。通过在本网站上发布,您已不可撤销地授予 Stack Exchange 网络以CC BY-SA 4.0 license 分发该内容的权利,只要它认为合适即可。有关删除的替代方法,请参阅:I've thought better of my question; can I delete it?

标签: node.js winston


【解决方案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}`));

【讨论】:

    【解决方案2】:

    或者,您可以修改 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');
    

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

    【讨论】:

      猜你喜欢
      • 2019-05-20
      • 2019-01-03
      • 2012-04-20
      • 2019-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-16
      • 1970-01-01
      相关资源
      最近更新 更多