Skip to content

Commit 07f98f2

Browse files
committed
Logging
1 parent bb1bd4e commit 07f98f2

File tree

4 files changed

+234
-72
lines changed

4 files changed

+234
-72
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
cache
22
cache/
33
log/*
4+
.project
5+
.settings
46
test/data-stream/out.*
57
!test/data-stream/out.*.0
68
node_modules*

.project

+26
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,34 @@
55
<projects>
66
</projects>
77
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
813
</buildSpec>
914
<natures>
1015
<nature>org.nodeclipse.ui.NodeNature</nature>
16+
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
1117
</natures>
18+
<filteredResources>
19+
<filter>
20+
<id>1432669158562</id>
21+
<name></name>
22+
<type>26</type>
23+
<matcher>
24+
<id>org.eclipse.ui.ide.multiFilter</id>
25+
<arguments>1.0-name-matches-false-false-cache</arguments>
26+
</matcher>
27+
</filter>
28+
<filter>
29+
<id>1432669158571</id>
30+
<name></name>
31+
<type>26</type>
32+
<matcher>
33+
<id>org.eclipse.ui.ide.multiFilter</id>
34+
<arguments>1.0-name-matches-false-false-node_modules</arguments>
35+
</matcher>
36+
</filter>
37+
</filteredResources>
1238
</projectDescription>

app.js

+65-34
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ var argv = require('yargs')
2929
.argv;
3030

3131
if (argv.help || argv.h) {
32-
console.log("Usage: node app.js --port=number --debug?, where ?={all,app,util,stream,plugin,template,scheduler,lineformatter}.");
32+
console.log("Usage: node app.js [--port=number --debug{all,app,util,stream,plugin,template,scheduler,lineformatter}=true.]");
3333
return;
3434
}
35+
3536
if (argv.debugall) {
3637
argv.debugapp = true;
3738
argv.debugutil = true;
@@ -46,6 +47,7 @@ var util = require('./util.js');
4647
var scheduler = require("./scheduler.js");
4748
var stream = require("./stream.js");
4849
var logger = require("./logger.js");
50+
var log = require("../tsds2/tsdsfe/log.js");
4951

5052
//console.log(argv.help());
5153
//require('v8-profiler');
@@ -133,10 +135,6 @@ app.get('/', function (req, res) {
133135
function (err, data) {res.send(data);});
134136
})
135137

136-
//app.get('/log', function (req, res) {
137-
// res.send(fs.readFileSync(__dirname+"/application.log", "utf8"));
138-
//})
139-
140138
app.get("/report", function (req,res) {
141139
fs.readFile(__dirname+"/report.htm", "utf8",
142140
function (err,data) {
@@ -173,7 +171,6 @@ app.post("/async", function (req, res) {
173171
})
174172

175173
app.get("/sync", function (req,res) {
176-
if (argv.debugapp) console.log("GET")
177174
req.setTimeout(1000*60*15);
178175
handleRequest(req,res);
179176
});
@@ -259,20 +256,27 @@ app.use("/cache", express.directory(__dirname+"/cache"));
259256
app.use("/cache", express.static(__dirname + "/cache"));
260257
app.use("/demo", express.directory(__dirname+"/demo"));
261258
app.use("/demo", express.static(__dirname + "/demo"));
262-
app.use("/test/data", express.static(__dirname + "/test/data"));
263259
app.use("/test/data", express.directory(__dirname + "/test/data"));
260+
app.use("/test/data", express.static(__dirname + "/test/data"));
264261
app.use("/asset", express.directory(__dirname + "/asset"));
265262
app.use("/asset", express.static(__dirname + "/asset"));
266263

267-
server.listen(argv.port); // Start the server
268-
269-
util.logc((new Date()).toISOString() + " - [datacache] listening on port "+argv.port,10);
270264
config = {};
271265
config.TIMEOUT = 60*1000*15;
266+
config.LOGDIR = __dirname+"/log/";
267+
config.LOGHEADER = 'x-datacache-log';
268+
269+
server.listen(argv.port); // Start the server
270+
271+
// Create directories if needed.
272+
config = log.init(config)
273+
274+
log.logc((new Date()).toISOString() + " - [datacache] listening on port "+argv.port,10)
275+
272276
server.setTimeout(config.TIMEOUT,
273277
function(obj) {
274-
console.log("DataCache server timeout ("+(config.TIMEOUT/(1000*60))+" minutes).");
275-
if (obj) console.log(obj.server._events.request);
278+
//console.log("DataCache server timeout ("+(config.TIMEOUT/(1000*60))+" minutes).");
279+
//if (obj) console.log(obj);
276280
});
277281

278282

@@ -323,31 +327,57 @@ function syncsummary(source,options,res) {
323327
res.contentType('application/json');
324328
res.send(JSON.stringify(results));
325329
} else {
326-
console.log("Unknown option");
330+
console.log("Unknown option for return.");
327331
res.send("");
328332
}
329333
});
330334
}
331335

332336
function handleRequest(req, res) {
333337

334-
var options = parseOptions(req);
335-
var source = parseSource(req);
336-
options.id = Math.random().toString().substring(1);
337-
var logcolor = Math.round(255*parseFloat(options.id));
338+
var message = req.connection.remoteAddress + "," + req.originalUrl;
339+
if (req.headers['X-Forwarded-For']) {
340+
var message = req.headers['X-Forwarded-For'].replace(",",";") + req.originalUrl + ",";
341+
}
342+
343+
// Create detailed log file name based on current time and other information.
344+
var loginfo = crypto.createHash("md5").update((new Date()).toISOString() + message).digest("hex");
345+
346+
// Set log file name as response header
347+
res.header(config.LOGHEADER,loginfo)
348+
349+
res.config = config;
350+
351+
log.logapp(loginfo + "," + message, res)
352+
353+
var options = parseOptions(req, res);
354+
var source = parseSource(req, res);
355+
options.id = Math.random().toString().substring(1);
356+
var logcolor = Math.round(255*parseFloat(options.id));
357+
358+
if (argv.debugapp) {
359+
//log.logres("Configuration file = "+JSON.stringify(config.CONFIGFILE), res)
360+
log.logres("Configuration = "+JSON.stringify(config), res)
361+
log.logres("req.headers = " + JSON.stringify(req.headers), res)
362+
log.logres("req.connection.remoteAddress = " + JSON.stringify(req.connection.remoteAddress), res)
363+
log.logres("req.originalUrl = " + JSON.stringify(req.originalUrl), res)
364+
log.logres("options = " + JSON.stringify(options), res)
365+
}
338366

339367
// Compress response if headers accept it and streamGzip is not requested.
340368
if (!options.streamGzip)
341369
app.use(express.compress());
342370

343-
// Return nothing if no urls were requested or if template did create any urls.
371+
// Return nothing if no URLs were requested or if template did create any urls.
344372
if (source.length === 0) {
373+
log.logres("source.length = 0. Sending res.end().", res)
345374
res.end();
346375
return;
347376
}
348377

349-
if (options.debugapp || options.debugstream)
350-
util.logc(options.id + " app.handleRequest() called with source="+source.toString().replace(/,/g,"\n\t"),logcolor);
378+
if (options.debugapp || options.debugstream) {
379+
util.logc(options.id + " app.handleRequest() called with source="+source.toString().replace(/,/g,"\n\t"),logcolor)
380+
}
351381

352382
if (options.return === "stream") {
353383
stream.stream(source,options,res);
@@ -356,7 +386,7 @@ function handleRequest(req, res) {
356386
}
357387
}
358388

359-
function parseOptions(req) {
389+
function parseOptions(req, res) {
360390

361391
function s2b(str) {if (str === "true") {return true} else {return false}}
362392
function s2i(str) {return parseInt(str)}
@@ -442,29 +472,28 @@ function parseOptions(req) {
442472
}
443473

444474
if (argv.debugapp) {
445-
console.log("\noptions after parseOptions:");
446-
console.log(options);
475+
log.logres("options after parseOptions:"+JSON.stringify(options), res);
447476
}
448477

449478
return options;
450479
}
451480

452-
function parseSource(req) {
481+
function parseSource(req, res) {
453482

454-
options = parseOptions(req);
483+
options = parseOptions(req, res);
455484

456485
var source = req.body.source || req.query.source || "";
457486
var prefix = req.body.prefix || req.query.prefix;
458487

459-
var template = req.body.template || req.query.template;
488+
var template = req.body.template || req.query.template;
460489
var timeRange = req.body.timeRange || req.query.timeRange;
461490
var indexRange = req.body.indexRange || req.query.indexRange;
462491

463492
if (!source && !template) return "";
464493

465494
var sourcet = [];
466495

467-
var opts = {};
496+
var opts = {};
468497

469498
if (template) {
470499
opts.template = template;
@@ -476,10 +505,14 @@ function parseSource(req) {
476505
opts.timeRange = timeRange;
477506
opts.indexRange = null;
478507
opts.debug = options.debugtemplate;
479-
sourcet = expandtemplate(opts);
508+
opts.log = true;
509+
ret = expandtemplate(opts);
510+
console.log(ret.log)
511+
sourcet = ret.files
512+
//sourcet = expandtemplate(opts)
480513
if (options.debugtemplate) {
481-
console.log("sourcet = ");
482-
console.log(sourcet);
514+
log.logres("expandtemplate.js: "+ret.log, res)
515+
//log.logres("sourcet = "+JSON.stringify(sourcet), res)
483516
}
484517

485518
}
@@ -498,8 +531,7 @@ function parseSource(req) {
498531
}
499532

500533
if (options.debugtemplate) {
501-
console.log("\noptions after parseSource:")
502-
console.log(options);
534+
log.logres("options after parseSource: "+JSON.stringify(options), res)
503535
}
504536

505537
if ((sourcet.length > 0) && (source.length > 0)) {
@@ -513,8 +545,7 @@ function parseSource(req) {
513545
for (i = 0; i < source.length; i++) {source[i] = prefix + source[i];}
514546

515547
if (options.debugapp) {
516-
console.log("source = ");
517-
console.log(source);
548+
log.logres("source = "+JSON.stringify(source), res)
518549
}
519550

520551
return source;

0 commit comments

Comments
 (0)