Skip to content

Commit

Permalink
Merge pull request #380 from nodejitsu/timestamp-logs
Browse files Browse the repository at this point in the history
Add timestamp support to forever log
  • Loading branch information
indexzero committed Feb 12, 2013
2 parents 2e52eaa + d3ff4bd commit 3b3d04b
Showing 1 changed file with 51 additions and 35 deletions.
86 changes: 51 additions & 35 deletions lib/forever.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ forever.log = new (winston.Logger)({

forever.log.cli();

//
// Setup `forever out` for logEvents with `winston` custom logger.
//
forever.out = new (winston.Logger)({
transports: [
new (winston.transports.Console)()
]
});

//
// ### Export Components / Settings
// Export `version` and important Prototypes from `lib/forever/*`
Expand All @@ -46,6 +55,8 @@ forever.Forever = forever.Monitor = require('forever-monitor').Monitor;
forever.Worker = require('./forever/worker').Worker;
forever.cli = require('./forever/cli');



//
// Expose version through `pkginfo`
//
Expand All @@ -67,7 +78,7 @@ forever.__defineGetter__('service', function () {
//
function getSockets(sockPath, callback) {
var sockets;

try {
sockets = fs.readdirSync(sockPath);
}
Expand Down Expand Up @@ -106,10 +117,10 @@ function getAllProcesses(callback) {
next(null, data);
socket.end();
});

socket.send(['data']);
});

socket.on('error', function (err) {
if (err.code === 'ECONNREFUSED') {
fs.unlink(fullPath, function () {
Expand Down Expand Up @@ -153,7 +164,7 @@ function stopOrRestart(action, event, format, target) {

function sendAction(proc, next) {
var socket = new nssocket.NsSocket();

socket.connect(proc.socket, function (err) {
if (err) {
next(err);
Expand All @@ -163,17 +174,17 @@ function stopOrRestart(action, event, format, target) {
next();
socket.end();
});

socket.send([action]);
});

socket.on('error', function (err) {
next(err);
});
}

getAllProcesses(function (processes) {
var procs = processes;
var procs = processes;

if (target !== undefined && target !== null) {
procs = forever.findByIndex(target, processes)
Expand All @@ -186,7 +197,7 @@ function stopOrRestart(action, event, format, target) {
if (err) {
emitter.emit('error', err);
}

emitter.emit(event, forever.format(format, procs));
});
}
Expand Down Expand Up @@ -251,6 +262,11 @@ forever.load = function (options) {
forever.config.set('loglength', options.loglength);
forever.config.set('columns', options.columns);

//
// Setup timestamp to event logger
//
forever.out.transports.console.timestamp = forever.config.get('timestamp') === 'true';

//
// Attempt to see if `forever` has been configured to
// run in debug mode.
Expand Down Expand Up @@ -354,11 +370,11 @@ forever.start = function (script, options) {
if (!options.uid) {
options.uid = options.uid || utile.randomString(4).replace(/^\-/, '_');
}

if (!options.logFile) {
options.logFile = forever.logFilePath(options.uid + '.log');
}

//
// Create the monitor, log events, and start.
//
Expand All @@ -381,12 +397,12 @@ forever.startDaemon = function (script, options) {

var monitor, outFD, errFD, workerPath;

//
//
// This log file is forever's log file - the user's outFile and errFile
// options are not taken into account here. This will be an aggregate of all
// the app's output, as well as messages from the monitor process, where
// applicable.
//
//
outFD = fs.openSync(options.logFile, 'a');
errFD = fs.openSync(options.logFile, 'a');
monitorPath = path.resolve(__dirname, '..', 'bin', 'monitor');
Expand Down Expand Up @@ -435,7 +451,7 @@ forever.startServer = function () {
sockPath: forever.config.get('sockPath'),
exitOnStop: true
});

worker.start(function (err) {
return err ? next(err) : next(null, worker);
});
Expand Down Expand Up @@ -496,7 +512,7 @@ forever.list = function (format, callback) {
};

//
// ### function tail (target, length, callback)
// ### function tail (target, length, callback)
// #### @target {string} Target script to list logs for
// #### @length {number} **Optional** Length of the logs to tail.
// #### @callback {function} Continuation to respond to when complete.
Expand All @@ -509,7 +525,7 @@ forever.tail = function (target, length, callback) {
callback = length;
length = 0;
}

length = length || forever.config.get('loglength');
if (!length) {
return callback(new Error('Cannot tail logs without a specified length'));
Expand All @@ -520,26 +536,26 @@ forever.tail = function (target, length, callback) {
if (err) {
return next(err);
}

proc.logs = stdout.split('\n');
proc.logs.pop();

return err ? next(err) : next(null, proc);
});
}

getAllProcesses(function (processes) {
if (!processes) {
return callback(new Error('Cannot find forever process: ' + target));
}

var procs = forever.findByIndex(target, processes)
|| forever.findByScript(target, processes);

if (!procs) {
return callback(new Error('No logs available for process: ' + target));
}

async.mapSeries(procs, tailProcess, function (err, procs) {
return err
? callback(err)
Expand Down Expand Up @@ -605,12 +621,12 @@ forever.format = function (format, procs) {
if (!procs || procs.length === 0) {
return null;
}

var index = 0,
columns = forever.config.get('columns'),
rows = [[' '].concat(columns)],
formatted;

function mapColumns(prefix, mapFn) {
return [prefix].concat(columns.map(mapFn));
}
Expand Down Expand Up @@ -653,7 +669,7 @@ forever.cleanUp = function (cleanLogs, allowManager) {
if (cleanLogs) {
forever.cleanLogsSync(processes);
}

function unlinkProcess(proc, done) {
fs.unlink(path.join(pidPath, proc.uid + '.pid'), function () {
//
Expand Down Expand Up @@ -698,7 +714,7 @@ forever.cleanUp = function (cleanLogs, allowManager) {

done();
}

function checkProcess(proc, next) {
proc.child = forever.checkProcess(proc.pid);
proc.manager = forever.checkProcess(proc.foreverPid);
Expand Down Expand Up @@ -735,11 +751,11 @@ forever.cleanLogsSync = function (processes) {
files = fs.readdirSync(root),
running,
runningLogs;

running = processes && processes.filter(function (p) {
return p && p.logFile;
});

runningLogs = running && running.map(function (p) {
return p.logFile.split('/').pop();
});
Expand Down Expand Up @@ -780,20 +796,20 @@ forever.pidFilePath = function (pidFile) {
//
forever.logEvents = function (monitor) {
monitor.on('watch:error', function (info) {
console.error(info.message);
console.error(info.error);
forever.out.error(info.message);
forever.out.error(info.error);
});

monitor.on('watch:restart', function (info) {
console.error('restarting script because ' + info.file + ' changed')
forever.out.error('restarting script because ' + info.file + ' changed')
});

monitor.on('restart', function () {
console.error('Forever restarting script for ' + monitor.times + ' time')
forever.out.error('Forever restarting script for ' + monitor.times + ' time')
});

monitor.on('exit:code', function (code) {
console.error('Forever detected script exited with code: ' + code);
forever.out.error('Forever detected script exited with code: ' + code);
});
};

Expand Down

0 comments on commit 3b3d04b

Please sign in to comment.