Winston-logger不在模块中工作

时间:2018-02-27 10:11:20

标签: node.js logging winston

我制作了一个winston模块,我将其导入到需要记录的所有模块中。但是它只会在我的应用程序入口点(我开始使用的那个)中记录条目。

我的实施做错了什么?

logger.js(winston模块)

var winston = require('winston');

var logger = new (winston.Logger)({
  transports: [
    new winston.transports.File({ 
      filename: __dirname + '/logger.log', 
      json: false,
      timestamp: function () {
        return new Date()
      } 
    })
  ],
  exceptionHandlers: [
    new winston.transports.File({ filename: __dirname + '/exceptions.log', json: false })
  ],
  exitOnError: false
});

module.exports = logger;

主节点文件(此处的日志记录正常):

var logger = require('./utils/logger');

需要记录的其他模块(记录不工作,没有错误消息)

var logger = require('../utils/logger');

logger.error('Error to log')

任何想法我做错了什么?

1 个答案:

答案 0 :(得分:0)

我尝试根据您的要求创建示例。这可能有助于找到您的问题。

  

的package.json

{
  "name": "winston",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "express": "4.16.2",
    "http": "0.0.0",
    "nodemon": "1.15.1",
    "winston": "2.4.0"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}
  

index.js

const express = require('express');
const app = express();
const logger = require("./logger");

app.listen(3000, () => {
  logger.log("info", 'Example app listening on port 3000!');
  const data = require("./data")(app);
});
  

logger.js

var winston = require('winston');
var logger = new (winston.Logger)({
  transports: [
    new winston.transports.File({
      filename: __dirname + '/logger.log',
      json: false,
      timestamp: function () {
        return new Date()
      },
      handleExceptions: true,
      humanReadableUnhandledException: true,
    })
  ],
  exceptionHandlers: [
    new winston.transports.File({ filename: __dirname + '/exceptions.log', json: false })
  ],
  exitOnError: false
});
module.exports = logger;
module.exports.stream = {
  write: function (message, encoding) {
    logger.info(message);
  }
};
  

data.js

const logger = require("./logger");
logger.log("info", 'executes data.js');

module.exports = ((app) => {

    app.get('/', (req, res) => {
        logger.log("info", "Default route executes");
        res.send("hello world from data file");
    });

    app.get('/hello', (req, res) => {
        logger.log("info", "hello route executes");
        res.send("hello route executes");
    });

    app.use(function (err, req, res, next) {
        logger.log("info", "error routing called");
        logger.log("error", err);
        if (err.name === 'UnauthorizedError') {
            res.status(401).json({ status: 0, code: 401, type: "unauthorised", message: err.name + ": " + err.message });
        } else {
            res.status(404).json({ status: 0, code: 404, type: "ENOENT", message: "file not found" });
        }
    });
    return app;
});

在上面的代码中,您似乎应该使用logger.log并添加第一个参数作为日志类型。 有6个级别的日志。

  1. 错误
  2. 警告
  3. 信息
  4. 详细
  5. 调试