From d3ff4bd664ef9541afd88e8854f4ac901688bf9d Mon Sep 17 00:00:00 2001 From: Julian Duque Date: Sun, 3 Feb 2013 01:47:46 -0500 Subject: [PATCH] Add timestamp support to forever log With `forever set timestamp true` the log will contain timestamps, fixes #309 and #314 --- lib/forever.js | 86 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 51 insertions(+), 35 deletions(-) diff --git a/lib/forever.js b/lib/forever.js index 35511e5a..0d893713 100644 --- a/lib/forever.js +++ b/lib/forever.js @@ -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/*` @@ -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` // @@ -67,7 +78,7 @@ forever.__defineGetter__('service', function () { // function getSockets(sockPath, callback) { var sockets; - + try { sockets = fs.readdirSync(sockPath); } @@ -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 () { @@ -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); @@ -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) @@ -186,7 +197,7 @@ function stopOrRestart(action, event, format, target) { if (err) { emitter.emit('error', err); } - + emitter.emit(event, forever.format(format, procs)); }); } @@ -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. @@ -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. // @@ -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'); @@ -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); }); @@ -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. @@ -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')); @@ -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) @@ -602,12 +618,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)); } @@ -650,7 +666,7 @@ forever.cleanUp = function (cleanLogs, allowManager) { if (cleanLogs) { forever.cleanLogsSync(processes); } - + function unlinkProcess(proc, done) { fs.unlink(path.join(pidPath, proc.uid + '.pid'), function () { // @@ -695,7 +711,7 @@ forever.cleanUp = function (cleanLogs, allowManager) { done(); } - + function checkProcess(proc, next) { proc.child = forever.checkProcess(proc.pid); proc.manager = forever.checkProcess(proc.foreverPid); @@ -732,11 +748,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(); }); @@ -777,20 +793,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); }); };