summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--logger.js35
-rwxr-xr-xwebserver.js25
3 files changed, 60 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore
index 87b62ea..ce159bb 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,3 +4,4 @@ node_modules/
persist/
out.log
pid.txt
+request_log.txt.*
diff --git a/logger.js b/logger.js
new file mode 100644
index 0000000..592342d
--- /dev/null
+++ b/logger.js
@@ -0,0 +1,35 @@
+var fs=require("fs");
+
+var MAX_VOLUME=16*1024*1024;
+
+function Logger(fname){
+ if(!(this instanceof Logger))throw new Error("Logger() called without 'new'");
+ this._fname=fname;
+ this._logstart=new Date();
+ this._currentfname=fname+"."+this._logstart.getTime();
+ this._volume=0;
+ this._stream=fs.createWriteStream(this._currentfname);
+ this._ok=true;
+}
+
+Logger.prototype.log=function log(line){
+ if(!this._ok)return;
+ var now=new Date();
+ var data=now.toISOString()+": "+line+"\n";
+ if(this._volume+data.length>MAX_VOLUME){
+ this._stream.end();
+ this._logstart=now;
+ this._currentfname=this._fname+"."+this._logstart.getTime();
+ this._volume=0;
+ this._stream=fs.createWriteStream(this._currentfname);
+ this._ok=true;
+ }
+ this._volume+=data.length;
+ if(!this._stream.write(data)){
+ console.log("WARNING: LOG STREAM '"+this._currentfname+"' NOT DRAINING!");
+ this._ok=false;
+ this._stream.once("drain",function(){this._ok=true;}.bind(this));
+ }
+};
+
+module.exports=Logger;
diff --git a/webserver.js b/webserver.js
index a190a4c..89585c3 100755
--- a/webserver.js
+++ b/webserver.js
@@ -9,7 +9,8 @@ var app=require("express")(),
util=require("util"),
node_path=require("path"),
bodyParser=require("body-parser"),
- basicAuth=require("basic-auth");
+ basicAuth=require("basic-auth"),
+ Logger=require("./logger");
if(process.argv.length>3){
@@ -19,6 +20,9 @@ if(process.argv.length>3){
var PORT=process.argv.length==3?+process.argv[2]:80;
+var reqlogger=new Logger("request_log.txt");
+
+
var whatpulse={"keys":"<??>","clicks":"<??>"};
function refreshWhatpulse(){
http.get("http://api.whatpulse.org/user.php?user=tomsmeding&format=json&formatted=yes",function(res){
@@ -35,6 +39,25 @@ function refreshWhatpulse(){
setInterval(refreshWhatpulse,6*3600*1000); //every 6 hours
refreshWhatpulse();
+function simpleLogEscape(str){
+ return str
+ .replace(/\\/g,"\\\\")
+ .replace(/\n/g,"\\n")
+ .replace(/ /g,"\\_");
+}
+
+app.use(function(req,res,next){
+ var line=simpleLogEscape(req.ip)+" "+
+ simpleLogEscape(req.method)+" "+
+ simpleLogEscape(req.protocol)+" "+
+ simpleLogEscape(req.hostname)+" "+
+ simpleLogEscape(req.url);
+ if(req.headers["dnt"])line+=" dnt="+simpleLogEscape(req.headers["dnt"]);
+ if(req.headers["referer"])line+=" referer="+simpleLogEscape(req.headers["referer"]);
+ reqlogger.log(line);
+ next();
+});
+
/*app.use(function (req, res, next) {
console.log(req.subdomains);
next();