Skip to content

Commit

Permalink
A few stability improvements, more to come.
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed May 12, 2010
1 parent 423d9b2 commit 19381d9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 25 deletions.
39 changes: 28 additions & 11 deletions example/loggingWebServer.js
@@ -1,7 +1,8 @@
var http = require("http"), var http = require("http"),
elf = require("../lib/elf-logger"), 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) { var httpServer = http.createServer(function (req, res) {
res.writeHead(200, { res.writeHead(200, {
"Content-Type": "text/plain;charset=utf-8", "Content-Type": "text/plain;charset=utf-8",
Expand All @@ -10,23 +11,28 @@ var httpServer = http.createServer(function (req, res) {
res.write(responseString, "utf8"); res.write(responseString, "utf8");
res.end(); res.end();
}); });
// Begin listening on port 8080.
httpServer.listen(8080); httpServer.listen(8080);




// You may pass an 'options' argument with a 'stream' // You may pass an 'options' argument with a 'stream' property. It's value
// property. It's value should be a WritableStream to // should be a WritableStream to write logging output to. If a 'stream' prop
// write logging output to. If a 'stream' prop is // is present, only the 'stream' and 'fields' are valid.
// present, only the 'stream' and 'fields' are valid. var stdoutLog = elf.createLogger(httpServer, {
//var stdoutLog = elf.createLogger(httpServer, {
// This logger will print all logging output to 'stdout' // This logger will print all logging output to 'stdout'
// stream: process.stdout, stream: process.stdout,
// Log only a few fields // 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 // For better organization, and log-splitting, etc., you would probably rather
// a logger instance with elf.defaultOptions // 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, { var filesystemLog = elf.createLogger(httpServer, {
template: function(fields) { template: function(fields) {
var name = ""; var name = "";
Expand All @@ -45,3 +51,14 @@ var filesystemLog = elf.createLogger(httpServer, {
dir: 'logs', dir: 'logs',
fields: ['date','time','c-ip','cs-method','cs-uri','sc-status'] 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)']
});
41 changes: 27 additions & 14 deletions lib/elf-logger.js
Expand Up @@ -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 // We need to modify the standard 'http' library a little bit, to hook into
// logging events that otherwise aren't exposed. // logging events that otherwise aren't exposed.
Expand Down Expand Up @@ -82,6 +83,15 @@ exports.createLogger = function(httpServer, options) {
} }
return rtn; 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")); })(require("http"));




Expand All @@ -98,6 +108,8 @@ exports.createLogger = function(httpServer, options) {
// function. Any number of ElfLoggers can be created for a single // function. Any number of ElfLoggers can be created for a single
// HttpServer instance. // HttpServer instance.
function ElfLogger(httpServer, options) { function ElfLogger(httpServer, options) {
if (!isServer(httpServer)) throw new Error('"createLogger" expects an \'http.Server\' instance.');

var self = this, i=options.fields.length; var self = this, i=options.fields.length;
extend(self, options); extend(self, options);
while (i--) { while (i--) {
Expand Down Expand Up @@ -166,7 +178,6 @@ extend(ElfEntry.prototype, {


} else { // go through the standard filename templating } else { // go through the standard filename templating
var logPath = logger.getEntryFilename(this); var logPath = logger.getEntryFilename(this);
//sys.puts(logPath);
writeToLog(logPath, this, logger); writeToLog(logPath, this, logger);
} }
}, this); }, this);
Expand Down Expand Up @@ -206,18 +217,16 @@ function ElfLog(filepath, logger) {
logName = path.basename(filepath), logName = path.basename(filepath),
dirs = getDirs(filepath); dirs = getDirs(filepath);


//sys.puts(JSON.stringify(dirs));
//sys.puts(logName);

if (dirs.length > 0) { if (dirs.length > 0) {

var dCount = 0, dirExists = true; var dCount = 0, dirExists = true;

var checkDir = function() { var checkDir = function() {
var subDirs = dirs.slice(0, dCount+1); var subDirs = dirs.slice(0, dCount+1);
subDirs = subDirs.length === 1 && subDirs[0] === '' ? subDirs = subDirs.length === 1 ?
'/' : subDirs.join('/'); (subDirs[0] || '/') :
path.join.apply(path, subDirs);

if (dirExists) { if (dirExists) {
//sys.puts('stat: ' + subDirs);
fs.stat(subDirs, function(err, stat) { fs.stat(subDirs, function(err, stat) {
if (stat) { if (stat) {
dCount++; dCount++;
Expand All @@ -232,7 +241,6 @@ function ElfLog(filepath, logger) {
self.createWriteStream(); self.createWriteStream();
}); });
} else { } else {
sys.puts('mkdir: ' + subDirs);
fs.mkdir(subDirs, 0777, function(err) { fs.mkdir(subDirs, 0777, function(err) {
dCount++; dCount++;
if (dCount < dirs.length) if (dCount < dirs.length)
Expand All @@ -243,9 +251,17 @@ function ElfLog(filepath, logger) {
} }
} }
checkDir(); checkDir();

} else { } else {
this.createWriteStream();
fs.stat(subDirs, function(err, stat) {
if (stat) {
self.createWriteStream();
} else {
self.createWriteStream();
}
});

} }
} }


Expand All @@ -257,9 +273,6 @@ extend(ElfLog.prototype, {
'mode': 0666 'mode': 0666
}); });
this.ready = true; this.ready = true;
this.flushQueue();
},
flushQueue: function() {
this.queuedEntries.forEach(function(entry) { this.queuedEntries.forEach(function(entry) {
this.writeEntry(entry); this.writeEntry(entry);
}, this); }, this);
Expand Down

0 comments on commit 19381d9

Please sign in to comment.