Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.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
Javascript winston日志记录只在回调中工作,不在catch块中工作_Javascript_Node.js_Logging_Winston - Fatal编程技术网

Javascript winston日志记录只在回调中工作,不在catch块中工作

Javascript winston日志记录只在回调中工作,不在catch块中工作,javascript,node.js,logging,winston,Javascript,Node.js,Logging,Winston,我正在设置一个服务器,希望在自定义文件中记录每个错误和信息。在catch块中捕获的所有错误都应该存储在“catch_error.log”文件中,所有这些信息性消息都应该存储在“console.log”中。所以我让Winston logger记录所有这些东西。记录器是提供各种方法的对象。logger.log(“某些文本”)在回调函数中调用消息时,在控制台上记录消息以及相应的日志文件。但是当我从catch块调用它时,它只在控制台终端上打印,而不登录到相应的日志文件中。为什么会发生这种情况??我想记录

我正在设置一个服务器,希望在自定义文件中记录每个错误和信息。在catch块中捕获的所有错误都应该存储在“catch_error.log”文件中,所有这些信息性消息都应该存储在“console.log”中。所以我让Winston logger记录所有这些东西。记录器是提供各种方法的对象。logger.log(“某些文本”)在回调函数中调用消息时,在控制台上记录消息以及相应的日志文件。但是当我从catch块调用它时,它只在控制台终端上打印,而不登录到相应的日志文件中。为什么会发生这种情况??我想记录catch块中的所有消息

const express = require(`express`);
const app = express();
const fs = require(`fs`);
const winston = require(`winston`);
const CONSOLE = new winston.transports.Console();
const ERROR = new winston.transports.File({ filename: './../../log/catch_error/catch_error.log', level: 'error' });
const INFO = new winston.transports.File({ filename: './../../log/console/console.log', level: 'info' });
const logger = winston.createLogger({
    format: winston.format.json(),
    transports: [
        CONSOLE,
        ERROR,
        INFO
    ],
    silent: false
});
let CONFIG = null
try{
    CONFIG = 
JSON.pase(fs.readFileSync('./../../../../config.json'));  //error generate pase instead of parse
}catch(err){
    logger.log({     //log on terminal but not in catch_error.log file

          level: `error`,
          message: err.message,
          stack: err.stack

    });
    //process.exit();
}
/**************************this is working*********************/
app.listen(8080, () => {

    logger.log({ //log on terminal as well as console.log file

        level: `info`,
        message: `server running at 8080`

    });

});

process.exit()
终止线程,因此Winston没有时间写入文件。

process.exit()
终止线程,因此Winston没有时间写入文件。

注释
process.exit()
注释
process.exit()后出现相同问题