Browse files

Closes #180, if the option logDir is null or undefined, it won't writ…

…e log files anymore.

Had to make changes to the log dependency, so if the stream option was null it wouldn't write.
  • Loading branch information...
1 parent 42ffd3b commit 6aec4d078f55ba004b1d2c1bebb6f1d10100c834 @larzconwell larzconwell committed Sep 1, 2012
Showing with 45 additions and 27 deletions.
  1. +13 −3 deps/log.js
  2. +32 −24 lib/cluster/master.js
View
16 deps/log.js
@@ -20,13 +20,18 @@ var Log = exports = module.exports = function Log(level, stream, print, loggly)
if ('string' == typeof level) level = exports[level.toUpperCase()];
this.level = level || exports.DEBUG;
this.stream = stream || process.stdout;
+ this.writing = true;
+ if (stream === null) {
+ this.stream = stream;
+ this.writing = false;
+ }
this.print = print || false;
if (loggly && typeof loggly.log == 'function') {
this.loggly = loggly;
} else {
this.loggly = false;
}
- if (this.stream.readable) this.read(level);
+ if (this.writing && this.stream.readable) this.read(level);
};
/**
* System is unusable.
@@ -131,7 +136,10 @@ Log.prototype = {
*/
log: function(levelStr, msg) {
if (exports[levelStr] <= this.level) {
- this.stream.write('[' + new Date().toUTCString() + ']' + ' ' + levelStr + ' ' + msg + '\n');
+ if (this.writing) {
+ this.stream.write('[' + new Date().toUTCString() + ']' + ' ' + levelStr + ' ' + msg + '\n');
+ }
+
if (this.print) {
coloredLevelStr = '';
if (levelStr === 'ERROR') { coloredLevelStr = levelStr.red; }
@@ -145,7 +153,9 @@ Log.prototype = {
this.loggly.log({eventType: levelStr, message: msg});
}
} else if (levelStr == 'ACCESS') {
- this.stream.write(msg + '\n');
+ if (this.writing) {
+ this.stream.write(msg + '\n');
+ }
}
},
/**
View
56 lib/cluster/master.js
@@ -85,39 +85,40 @@ Master.prototype = new (function () {
, _initLogging = function (next) {
var self = this
- , levelsByType
- , stdoutLevel
, types = ['stdout', 'stderr', 'access']
, now = date.strftime(new Date(), '%FT%T')
- , loggly
, dir = this.config.logDir
+ , levelsByType = {access: 'access', stderr: 'error', stdout: 0}
+ , writing = dir != null
+ , loggly
, rotateAndInitByType;
- // Set the logging level for stdout
+ // Set stdout log level
if (this.config.debug) {
- stdoutLevel = Log.DEBUG;
+ levelsByType.stdout = Log.DEBUG;
}
else if (this.config.logLevel) {
- stdoutLevel = Log[this.config.logLevel]();
+ // If a custom log level is given, then
+ // get the level from Log
+ levelsByType.stdout = Log[this.config.logLevel];
}
else {
- stdoutLevel = Log.INFO; // Default to info
+ // Default to info
+ levelsByType.stdout = Log.INFO;
}
- // Now that we have the desired level for stdout, set up log levels by key
- levelsByType = {
- access: 'access'
- , stderr: 'error'
- , stdout: stdoutLevel
- };
-
- // Create the log directory if it doesn't exist
- file.mkdirP(dir);
+ // If we are writing, then attempt to create the directory
+ // if it doesn't exist
+ if (writing) {
+ file.mkdirP(dir);
+ }
types.forEach(function (type) {
var currentLog = path.join(dir, type + '.log')
, archivedLog = path.join(dir, type + '.' + now + '.log');
- if (file.existsSync(currentLog)) {
+
+ // If the main log file exists, then rename it to the archived log file
+ if (writing && file.existsSync(currentLog)) {
try {
fs.renameSync(currentLog, archivedLog);
}
@@ -128,15 +129,22 @@ Master.prototype = new (function () {
fs.unlinkSync(currentLog);
}
}
- // After the file is renmaed, create the new logger with the original filename
- // e.g., access.log
- self[type + 'Log'] = new Log(levelsByType[type],
- fs.createWriteStream(dir +
- '/' + type + '.log'), true, loggly);
+
+ // Create a new logger for the type and give it a name on this with appended 'Log'
+ // e.g., stdoutLog
+ if (writing) {
+ self[type + 'Log'] = new Log(levelsByType[type],
+ fs.createWriteStream(currentLog), true, loggly);
+ }
+ else {
+ self[type + 'Log'] = new Log(levelsByType[type], null, true, loggly);
+ }
+
self[type + 'Log'].type = type;
});
- this.stdoutLog.info('Server starting with config: ' +
- JSON.stringify(self.config, true, 2));
+
+ /*this.stdoutLog.info('Server starting with config: ' +
+ JSON.stringify(self.config, true, 2));*/
next();
}

5 comments on commit 6aec4d0

@mde

Holy shit, awesome.

@Techwraith

This reminds me, should we just get rid of loggly support? It seems like an unknown/unused feature.

@larzconwell

Yeah I think we can remove the loggly stuff, it doesn't use it if it isn't an object with a log function.

@joropenchevastea

For what it's worth, this doesn't work for me.
I get error on this line: var currentLog = path.join(dir, type + '.log')
claiming that only strings can be arguments to join
(which does make sense, i believe)

@mde

May be a regression. Our logging stuff really needs an overall revamp.

Please sign in to comment.