I created the Node JS REST service using Express. Each request that lands on this service has headers, such as "X-org-ReqId" and "X-org-Tid", which I need to log all the log lines that are written during this request. Basically, I need to write some contextual information to each line of the log to help me track transactions / requests across multiple services.
I am using winston logger initialized as follows:
var winston = require('winston');
var appLogger = new(winston.Logger)({
transports: [
new(winston.transports.Console)({
level: 'info',
colorize: true
}),
new(winston.transports.DailyRotateFile)({
filename: '/var/log/org/my-service.log',
datePattern: '.yyyy-MM-dd',
tailable: true,
json: true,
logstash: true
})
],
exitOnError: false
});
appLogger.on('error', function(err) {
console.error("Logger in error", err);
});
module.exports.logger = function() {
return appLogger;
};
and in separate classes, wherever I want to use it, I like the following:
var logger = require('../config/logger').logger();
myObject.on("error", function (err) {
logger.error("Error connecting bucket=" + bucketId , err);
});
This will create the log as follows:
{"level":"info","message":"Error connecting bucket=2.....","timestamp":"2015-06-10T06:44:48.690Z"}
Winston timestamp , , req.headers ['X-org-ReqId'] req.headers ['X-org-Tid'], , .
, :
{"level":"info","message":"Error connecting bucket=2....","timestamp":"2015-06-10T06:44:48.690Z", "tid":"a3e8b380-1caf-11e5-9a21-1697f925ec7b", "reqid":"aad28806-1caf-11e5-9a21-1697f925ec7b"}
java- NDC, Node JS?