I have the following code using with i was trying to log all the requests coming to my Node Server. I have used fs.appendFile
for that but this doesn't rollover the files. I have also used the logger.debug
to log the same content.
I have simulated some 1000 requests using the JMeter
. So, fs.appendFile
is able log all the request where as log4j is not able to log all the requests. Can, someone tell me the reason for this also the solution to log each and every request.
var http = require('http'),
fs = require('fs'),
url = require('url');
var log4js = require( "log4js" );
log4js.configure( "./log4js.json" );
var logger = log4js.getLogger( "test-file-appender" );
http.createServer(function(request,response){
//var path = url.parse(request.url).pathname;
if(request.url=="/qs")
{
console.log("request checked");
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("response end");
}
else
{
fs.appendFile("C:/Users/admin/Desktop/Output/log.txt","\n " + request.url + " " + new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '') + " " + (request.headers['x-forwarded-for'] || '').split(',')[0] || request.connection.remoteAddress || request.socket.remoteAddress || request.connection.socket.remoteAddress ,function(err){
if(err) {throw err; console.log(err)}
else{
console.log("Last Time File Was Saved on " + new Date());
}
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("response end");
});
}
logger.debug(request.url + " " + new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '') + " " + (request.headers['x-forwarded-for'] || '').split(',')[0] || request.connection.remoteAddress || request.socket.remoteAddress || request.connection.socket.remoteAddress);
}).listen(8081);
console.log("Server Initialized");