Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

A few stability improvements, more to come.

  • Loading branch information...
commit 19381d92166537e9c058bf8f8dce78afea0e6574 1 parent 423d9b2
@TooTallNate authored
Showing with 55 additions and 25 deletions.
  1. +28 −11 example/loggingWebServer.js
  2. +27 −14 lib/elf-logger.js
View
39 example/loggingWebServer.js
@@ -1,7 +1,8 @@
var http = require("http"),
elf = require("../lib/elf-logger"),
- responseString = "Hello World!\nNow go look in the './log' folder to see logging output.";
+ responseString = "Hello World!\nNow go look in the 'log' folder to see logging output.";
+// Create a simple, standard Node HTTP server.
var httpServer = http.createServer(function (req, res) {
res.writeHead(200, {
"Content-Type": "text/plain;charset=utf-8",
@@ -10,23 +11,28 @@ var httpServer = http.createServer(function (req, res) {
res.write(responseString, "utf8");
res.end();
});
+// Begin listening on port 8080.
httpServer.listen(8080);
-// You may pass an 'options' argument with a 'stream'
-// property. It's value should be a WritableStream to
-// write logging output to. If a 'stream' prop is
-// present, only the 'stream' and 'fields' are valid.
-//var stdoutLog = elf.createLogger(httpServer, {
+// You may pass an 'options' argument with a 'stream' property. It's value
+// should be a WritableStream to write logging output to. If a 'stream' prop
+// is present, only the 'stream' and 'fields' are valid.
+var stdoutLog = elf.createLogger(httpServer, {
// This logger will print all logging output to 'stdout'
-// stream: process.stdout,
+ stream: process.stdout,
// Log only a few fields
-// fields: ['date','time','c-ip','cs-method','cs-uri','sc-status']
-//});
+ fields: ['date','time','c-ip','cs-method','cs-uri','sc-status']
+});
-// Omitting the second 'options' argument creates
-// a logger instance with elf.defaultOptions
+// For better organization, and log-splitting, etc., you would probably rather
+// supply a 'template' (and maybe 'dir') properties. The 'template' should be
+// a function that is called for each log entry and should return the path and
+// name of file that the entry should log to. The function is passed the
+// entry's #Fields as an object literal for an argument. The 'dirs' prop, if
+// present, will prefix the 'dir' value before the 'template' value to
+// determine the actual log file location.
var filesystemLog = elf.createLogger(httpServer, {
template: function(fields) {
var name = "";
@@ -45,3 +51,14 @@ var filesystemLog = elf.createLogger(httpServer, {
dir: 'logs',
fields: ['date','time','c-ip','cs-method','cs-uri','sc-status']
});
+
+
+// And finally, you may pass a String to the 'template' property, which may
+// contain the names of individual #Fields values, surrounded by {}s. For
+// each entry, the {} field will be replaced with the entry's actual value in
+// order to determine which log file the entry should be recorded to.
+var methodLog = elf.createLogger(httpServer, {
+ template: "METHOD/{cs-method}.log",
+ dir: 'logs',
+ fields: ['date','time','c-ip','cs-uri','sc-status','cs(host)']
+});
View
41 lib/elf-logger.js
@@ -19,6 +19,7 @@ exports.createLogger = function(httpServer, options) {
};
+var isServer;
// We need to modify the standard 'http' library a little bit, to hook into
// logging events that otherwise aren't exposed.
@@ -82,6 +83,15 @@ exports.createLogger = function(httpServer, options) {
}
return rtn;
}
+
+ // TODO eventually remove sendHeader(), writeHeader(), (when ry does)
+ http.ServerResponse.prototype.sendHeader = http.ServerResponse.prototype.writeHead;
+ http.ServerResponse.prototype.writeHeader = http.ServerResponse.prototype.writeHead;
+
+ // We need to define this function while we're in the native 'http' scope
+ isServer = function(server) {
+ return server instanceof http.Server;
+ }
})(require("http"));
@@ -98,6 +108,8 @@ exports.createLogger = function(httpServer, options) {
// function. Any number of ElfLoggers can be created for a single
// HttpServer instance.
function ElfLogger(httpServer, options) {
+ if (!isServer(httpServer)) throw new Error('"createLogger" expects an \'http.Server\' instance.');
+
var self = this, i=options.fields.length;
extend(self, options);
while (i--) {
@@ -166,7 +178,6 @@ extend(ElfEntry.prototype, {
} else { // go through the standard filename templating
var logPath = logger.getEntryFilename(this);
- //sys.puts(logPath);
writeToLog(logPath, this, logger);
}
}, this);
@@ -206,18 +217,16 @@ function ElfLog(filepath, logger) {
logName = path.basename(filepath),
dirs = getDirs(filepath);
- //sys.puts(JSON.stringify(dirs));
- //sys.puts(logName);
-
if (dirs.length > 0) {
+
var dCount = 0, dirExists = true;
-
var checkDir = function() {
var subDirs = dirs.slice(0, dCount+1);
- subDirs = subDirs.length === 1 && subDirs[0] === '' ?
- '/' : subDirs.join('/');
+ subDirs = subDirs.length === 1 ?
+ (subDirs[0] || '/') :
+ path.join.apply(path, subDirs);
+
if (dirExists) {
- //sys.puts('stat: ' + subDirs);
fs.stat(subDirs, function(err, stat) {
if (stat) {
dCount++;
@@ -232,7 +241,6 @@ function ElfLog(filepath, logger) {
self.createWriteStream();
});
} else {
- sys.puts('mkdir: ' + subDirs);
fs.mkdir(subDirs, 0777, function(err) {
dCount++;
if (dCount < dirs.length)
@@ -243,9 +251,17 @@ function ElfLog(filepath, logger) {
}
}
checkDir();
-
+
} else {
- this.createWriteStream();
+
+ fs.stat(subDirs, function(err, stat) {
+ if (stat) {
+ self.createWriteStream();
+ } else {
+ self.createWriteStream();
+ }
+ });
+
}
}
@@ -257,9 +273,6 @@ extend(ElfLog.prototype, {
'mode': 0666
});
this.ready = true;
- this.flushQueue();
- },
- flushQueue: function() {
this.queuedEntries.forEach(function(entry) {
this.writeEntry(entry);
}, this);
Please sign in to comment.
Something went wrong with that request. Please try again.