Skip to content
Permalink
Browse files

log timestamp #183 simple patch with --log-date-format

  • Loading branch information
Unitech committed Jul 29, 2014
1 parent dfff26d commit 57673937d985473a3bf93b34f1659c5c58febdab
Showing with 72 additions and 1 deletion.
  1. +1 −0 bin/pm2
  2. +2 −0 lib/CLI.js
  3. +6 −0 lib/God/ForkMode.js
  4. +10 −0 lib/ProcessContainer.js
  5. +1 −0 package.json
  6. +50 −0 test/bash/log-custom.sh
  7. +2 −1 test/main.sh
@@ -30,6 +30,7 @@ commander.version(pkg.version)
.option('-c --cron <cron_pattern>', 'restart a running process based on a cron pattern')
.option('-w --write', 'write configuration in local folder')
.option('--interpreter <interpreter>', 'the interpreter pm2 should use for executing app (bash, python...)')
.option('--log-date-format <momentjs format>', 'add custom prefix timestamp to logs')
.option('--no-daemon', "run pm2 daemon in the foreground if it doesn't exist already")
.option('--merge-logs', 'merge logs from different instances but keep error and out separated')
.option('--watch', 'watch application folder for changes')
@@ -65,6 +65,8 @@ CLI.start = function(script, opts, cb) {
appConf['run_as_user'] = opts.runAsUser;
if (opts.runAsGroup)
appConf['run_as_group'] = opts.runAsGroup;
if (opts.logDateFormat)
appConf['log_date_format'] = opts.logDateFormat;

if (opts.executeCommand) {
appConf['exec_mode'] = 'fork_mode';
@@ -10,6 +10,7 @@ var log = require('debug')('pm2:god');
var fs = require('fs');
var cst = require('../../constants.js');
var uidNumber = require('uid-number');
var moment = require('moment');

/**
* Description
@@ -109,6 +110,8 @@ module.exports = function(God) {
cspr.pm2_env.status = cst.ONLINE_STATUS;

cspr.stderr.on('data', function(data) {
if (pm2_env.log_date_format)
data = moment().format(pm2_env.log_date_format) + ' ' + data;

stderr.write(data);

@@ -119,6 +122,9 @@ module.exports = function(God) {
});

cspr.stdout.on('data', function(data) {
if (pm2_env.log_date_format)
data = moment().format(pm2_env.log_date_format) + ' ' + data;

stdout.write(data);
God.bus.emit('log:out', {
process : cspr,
@@ -92,6 +92,12 @@ function exec(script, outFile, errFile) {
}
});

var moment = null;

if (process.env.log_date_format)
moment = require('moment');


/**
* Description
* @method startLogging
@@ -107,6 +113,8 @@ function exec(script, outFile, errFile) {

process.stderr.write = (function(write) {
return function(string, encoding, fd) {
if (process.env.log_date_format && moment)
string = moment().format(process.env.log_date_format) + ' ' + string;
stderr.write(string);
process.send({
type : 'log:err',
@@ -118,6 +126,8 @@ function exec(script, outFile, errFile) {

process.stdout.write = (function(write) {
return function(string, encoding, fd) {
if (process.env.log_date_format && moment)
string = moment().format(process.env.log_date_format) + ' ' + string;
stdout.write(string);
process.send({
type : 'log:out',
@@ -122,6 +122,7 @@
"pm2-deploy": "~0.0.4",
"pm2-interface": "~0.1.3",
"pm2-multimeter": "~0.1.2",
"moment" : "~2.7.0",
"uid-number": "0.0.5"
},
"devDependencies": {
@@ -0,0 +1,50 @@

#!/usr/bin/env bash

SRC=$(cd $(dirname "$0"); pwd)
source "${SRC}/include.sh"

cd $file_path

$pm2 kill


# CLUSTERMODE YYYY
$pm2 start echo.js --log-date-format "YYYY" -o out-rel.log --merge-logs

>out-rel.log

sleep 2

grep "2014" out-rel.log
spec "Should have written year in log file according to format YYYY"

rm out-rel.log

$pm2 delete all

# CLUSTERMODE Wrong format
$pm2 start echo.js --log-date-format "YYYY asdsd asd asd sad asd " -o out-rel.log --merge-logs

sleep 1
should 'should has not restarted' 'restart_time: 0' 1
spec "Should have not fail with random format"

rm out-rel.log

$pm2 delete all


# CLUSTERMODE YYYY
$pm2 start echo.js --log-date-format "YYYY" -o out-rel.log --merge-logs -x

>out-rel.log

sleep 2

grep "2014" out-rel.log
spec "Should have written year in log file according to format YYYY"

rm out-rel.log

$pm2 delete all
@@ -13,6 +13,7 @@ echo "###################### !DEBUG! ###########################"
bash ./test/bash/cli.sh
bash ./test/bash/json_file.sh
bash ./test/bash/harmony.sh
bash ./test/bash/log-custom.sh
bash ./test/bash/reload.sh
bash ./test/bash/right-exit-code.sh
bash ./test/bash/log-reload.sh
@@ -24,4 +25,4 @@ bash ./test/bash/misc.sh
bash ./test/bash/fork.sh
bash ./test/bash/infinite_loop.sh

$pm2 kill
$pm2 kill

0 comments on commit 5767393

Please sign in to comment.
You can’t perform that action at this time.