Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.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中设置自定义错误日志格式_Javascript_Node.js_Express_Winston - Fatal编程技术网

Javascript 如何在winston中设置自定义错误日志格式

Javascript 如何在winston中设置自定义错误日志格式,javascript,node.js,express,winston,Javascript,Node.js,Express,Winston,winston错误日志的默认格式太大了,它包含许多类似 日期、流程、内存、操作系统、跟踪等 如何从日志中删除不需要的东西 logging.js const express = require('express'); const winston = require('winston'); module.exports = function() { winston.handleExceptions( new winston.transports.File({ filename: 'un

winston错误日志的默认格式太大了,它包含许多类似
日期、流程、内存、操作系统、跟踪等
如何从日志中删除不需要的东西

logging.js

const express = require('express');
const winston = require('winston');

module.exports = function() {
  winston.handleExceptions(
    new winston.transports.File({ filename: 'uncaughtExceptions.log' }));

  process.on('unhandledRejection', (ex) => {
    throw ex;
  });

  winston.add(winston.transports.File, { filename: 'request.log' });

}
const express = require('express');
const winston = require("winston");
const app = express();

//to Log errors
require('./startup/logging')();
//routes will contains all the routes list
require('./startup/routes')(app);

//PORT
const port = process.env.PORT || 3000;
app.listen(port,() => winston.info(`Listening on port ${port}....`));
const express = require('express');
module.exports = function(app){
   app.get('/', async (req, res) => {
        res.json("testing"+a);
    });
});
server.js

const express = require('express');
const winston = require('winston');

module.exports = function() {
  winston.handleExceptions(
    new winston.transports.File({ filename: 'uncaughtExceptions.log' }));

  process.on('unhandledRejection', (ex) => {
    throw ex;
  });

  winston.add(winston.transports.File, { filename: 'request.log' });

}
const express = require('express');
const winston = require("winston");
const app = express();

//to Log errors
require('./startup/logging')();
//routes will contains all the routes list
require('./startup/routes')(app);

//PORT
const port = process.env.PORT || 3000;
app.listen(port,() => winston.info(`Listening on port ${port}....`));
const express = require('express');
module.exports = function(app){
   app.get('/', async (req, res) => {
        res.json("testing"+a);
    });
});
routes.js

const express = require('express');
const winston = require('winston');

module.exports = function() {
  winston.handleExceptions(
    new winston.transports.File({ filename: 'uncaughtExceptions.log' }));

  process.on('unhandledRejection', (ex) => {
    throw ex;
  });

  winston.add(winston.transports.File, { filename: 'request.log' });

}
const express = require('express');
const winston = require("winston");
const app = express();

//to Log errors
require('./startup/logging')();
//routes will contains all the routes list
require('./startup/routes')(app);

//PORT
const port = process.env.PORT || 3000;
app.listen(port,() => winston.info(`Listening on port ${port}....`));
const express = require('express');
module.exports = function(app){
   app.get('/', async (req, res) => {
        res.json("testing"+a);
    });
});
上面的路由有错误,比如我可以记录的未定义变量“a”,但文件名和行号是错误的

uncaughtExceptions.log

{"date":"Wed Oct 31 2018 16:45:33 GMT+0530 (India Standard Time)","process":{"pid":9680,"uid":null,"gid":null,"cwd":"D:\\nodejs\\synchronizer","execPath":"C:\\Program Files\\nodejs\\node.exe","version":"v8.11.2","argv":["C:\\Program Files\\nodejs\\node.exe","D:\\nodejs\\synchronizer\\server.js"],"memoryUsage":{"rss":40591360,"heapTotal":23990272,"heapUsed":19075672,"external":18278915}},"os":{"loadavg":[0,0,0],"uptime":3474.217938},"trace":[{"column":28,"file":"D:\\nodejs\\synchronizer\\startup\\routes.js","function":"app.get","line":52,"method":"get","native":false},{"column":5,"file":"D:\\nodejs\\synchronizer\\node_modules\\express\\lib\\router\\layer.js","function":"Layer.handle [as handle_request]","line":95,"method":"handle [as handle_request]","native":false},{"column":13,"file":"D:\\nodejs\\synchronizer\\node_modules\\express\\lib\\router\\route.js","function":"next","line":137,"method":null,"native":false},{"column":3,"file":"D:\\nodejs\\synchronizer\\node_modules\\express\\lib\\router\\route.js","function":"Route.dispatch","line":112,"method":"dispatch","native":false},{"column":5,"file":"D:\\nodejs\\synchronizer\\node_modules\\express\\lib\\router\\layer.js","function":"Layer.handle [as handle_request]","line":95,"method":"handle [as handle_request]","native":false},{"column":22,"file":"D:\\nodejs\\synchronizer\\node_modules\\express\\lib\\router\\index.js","function":null,"line":281,"method":null,"native":false},{"column":12,"file":"D:\\nodejs\\synchronizer\\node_modules\\express\\lib\\router\\index.js","function":"Function.process_params","line":335,"method":"process_params","native":false},{"column":10,"file":"D:\\nodejs\\synchronizer\\node_modules\\express\\lib\\router\\index.js","function":"next","line":275,"method":null,"native":false},{"column":5,"file":"D:\\nodejs\\synchronizer\\middlewares\\reqreslog.js","function":"module.exports","line":12,"method":"exports","native":false},{"column":5,"file":"D:\\nodejs\\synchronizer\\node_modules\\express\\lib\\router\\layer.js","function":"Layer.handle [as handle_request]","line":95,"method":"handle [as handle_request]","native":false}],"stack":["ReferenceError: a is not defined","    at app.get (D:\\nodejs\\synchronizer\\startup\\routes.js:52:28)","    at Layer.handle [as handle_request] (D:\\nodejs\\synchronizer\\node_modules\\express\\lib\\router\\layer.js:95:5)","    at next (D:\\nodejs\\synchronizer\\node_modules\\express\\lib\\router\\route.js:137:13)","    at Route.dispatch (D:\\nodejs\\synchronizer\\node_modules\\express\\lib\\router\\route.js:112:3)","    at Layer.handle [as handle_request] (D:\\nodejs\\synchronizer\\node_modules\\express\\lib\\router\\layer.js:95:5)","    at D:\\nodejs\\synchronizer\\node_modules\\express\\lib\\router\\index.js:281:22","    at Function.process_params (D:\\nodejs\\synchronizer\\node_modules\\express\\lib\\router\\index.js:335:12)","    at next (D:\\nodejs\\synchronizer\\node_modules\\express\\lib\\router\\index.js:275:10)","    at module.exports (D:\\nodejs\\synchronizer\\middlewares\\reqreslog.js:12:5)","    at Layer.handle [as handle_request] (D:\\nodejs\\synchronizer\\node_modules\\express\\lib\\router\\layer.js:95:5)"],"level":"error","message":"uncaughtException: a is not defined","timestamp":"2018-10-31T11:15:33.714Z"}
问题是如何记录用户定义的错误日志格式,如:

timestamp:filename:errorlinenumber:errormessage:

上面的日志格式是默认格式,其中包括不必要的内容。

您需要创建一个winston,并从它提供的
info
对象中提取相关字段

以下是解决方案:

logger.js

输出:

2018-11-01T13:42:54.927Z | /home/runner/routes.js:4 | uncaughtException: a is not defined

如果需要其他字段,只需在
myFormat
中提取所需的
console.log(info)
。干杯

获取错误,如“const{combine,timestamp,label,printf}=format;^TypeError:无法对“undefined”或“null”的属性
combine
进行分解。”@JunedAnsari尝试更新winston。我已经测试了此代码,非常适合我。如果现在遇到其他错误,请检查是否已更新question@JunedAnsari我不能提供调试帮助。请参见此处操作中的应答代码:。试着问一个不同的问题来解释你所犯的错误。