如何避免错误信息存储信息日志文件?

时间:2019-07-16 08:44:38

标签: node.js winston

我尝试使用存储在info.log文件中的节点js.sucess消息和存储在error.log文件中的错误消息来尝试winston日志文件,但是将成功消息存储在info.log文件中但将错误消息存储在这两个文件中。如何修复

winston.js


/**
 * @file Winston Logging Configuration file.
 */

// Import Modules
const {getMongodbConnectionString} = require('../helper/helper_funtions');
const winston = require('winston');
require('winston-mongodb');
require('express-async-errors');

/**
 * Function used to configure logging for the app.
 * @returns {void} - none.
 */
const winstonLogger = function () {

    // Raise error on unhandled rejections
    process.on('unhandledRejection', (exception) => {
        throw exception;
    });

    if (process.env.NODE_ENV !== 'production') {
        // Non-production log messages
        // Console log messages
        winston.add(new winston.transports.Console({
            format: winston.format.combine(
                winston.format.simple(),
                winston.format.timestamp(),
                winston.format.prettyPrint(),
                winston.format.colorize()
            ),
            handleExceptions: true
        }));
    } 

    // Info Log messages to file
    winston.add(new winston.transports.File({
        filename: 'logs/info.log',
        level: 'info',
        handleExceptions: false,
    }));
    // Error Log Messages file
    winston.add(new winston.transports.File({
        filename: 'logs/error.log',
        level: 'error',
        handleExceptions: true,
    }));
};

module.exports = winstonLogger;

info.log

{"message":"Server Started and Listening on port 4202","level":"info"}
{"isJoi":true,"name":"ValidationError","details":[{"message":"\"user_full_name\" length must be at least 3 characters long","path":["user_full_name"],"type":"string.min","context":{"limit":3,"value":"s","key":"user_full_name","label":"user_full_name"}}],"_object":{"user_full_name":"s","user_email":"sudhar@techardors.com","user_phone":"9047060028"},"level":"error"}

error.log

{"isJoi":true,"name":"ValidationError","details":[{"message":"\"user_full_name\" length must be at least 3 characters long","path":["user_full_name"],"type":"string.min","context":{"limit":3,"value":"s","key":"user_full_name","label":"user_full_name"}}],"_object":{"user_full_name":"s","user_email":"sudhar@techardors.com","user_phone":"9047060028"},"level":"error"}

0 个答案:

没有答案