mirror of
https://github.com/Unitech/pm2.git
synced 2025-12-08 20:35:53 +00:00
Merge pull request #853 from Tjatse/development
feature: timestamp prefix of `pm2.log`
This commit is contained in:
commit
4a44f89aab
@ -530,6 +530,19 @@ $ pm2 monit
|
||||
<a name="a9"/>
|
||||
## Logs management
|
||||
|
||||
### Enable Timestamp Prefix of `pm2.log`
|
||||
|
||||
```
|
||||
export PM2_LOG_DATE_FORMAT="YYYY-MM-DD HH:mm Z"
|
||||
```
|
||||
|
||||
If this env-variable has been changed, you need to dump your processes and kill daemon, restart it again to take effect, e.g.:
|
||||
|
||||
```bash
|
||||
$ pm2 dump
|
||||
$ pm2 [resurrect|save]
|
||||
```
|
||||
|
||||
### Displaying logs in realtime
|
||||
|
||||

|
||||
|
||||
30
lib/God.js
30
lib/God.js
@ -40,6 +40,36 @@ var God = module.exports = {
|
||||
})
|
||||
};
|
||||
|
||||
/**
|
||||
* Hack Global Console Of PM2
|
||||
*/
|
||||
(function hackConsole(){
|
||||
if(process.env.PM2_LOG_DATE_FORMAT && typeof process.env.PM2_LOG_DATE_FORMAT == 'string'){
|
||||
var moment = require('moment');
|
||||
|
||||
// Generate timestamp prefix
|
||||
function timestamp(){
|
||||
return moment().format(process.env.log_date_format) + ': ';
|
||||
}
|
||||
|
||||
var hacks = ['info', 'log', 'error', 'warn'], consoled = {};
|
||||
|
||||
// store console functions.
|
||||
hacks.forEach(function(method){
|
||||
consoled[method] = console[method];
|
||||
});
|
||||
|
||||
// Hack Console.
|
||||
hacks.forEach(function(k){
|
||||
console[k] = function(){
|
||||
// do not destroy variable insertion
|
||||
arguments[0] && (arguments[0] = timestamp() + arguments[0]);
|
||||
consoled[k].apply(console, arguments);
|
||||
};
|
||||
});
|
||||
}
|
||||
})();
|
||||
|
||||
process.on('uncaughtException', function(err) {
|
||||
if (err && err.message == 'Resource leak detected.') {
|
||||
// Catch and ignore this error
|
||||
|
||||
102
test/bash/log-timestamp.sh
Normal file
102
test/bash/log-timestamp.sh
Normal file
@ -0,0 +1,102 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
SRC=$(cd $(dirname "$0"); pwd)
|
||||
source "${SRC}/include.sh"
|
||||
|
||||
function head {
|
||||
echo -e "\x1B[1;35m$1\x1B[0m"
|
||||
}
|
||||
function rm_pm2log {
|
||||
if [ "$1" -ne 1 ]; then
|
||||
$pm2 kill
|
||||
rm -rf ~/.pm2/pm2.log
|
||||
fi
|
||||
}
|
||||
function grep_log {
|
||||
if [ -z $TRAVIS ]; then
|
||||
echo "not travis"
|
||||
eval "$pm2 $1"
|
||||
sleep 0.3
|
||||
OUT=`cat ~/.pm2/pm2.log | grep -n "[0-9]\{4\}\-[0-9]\{2\}\-[0-9]\{2\}" | wc -l`
|
||||
else
|
||||
echo "travis"
|
||||
eval "$pm2 $1 >| pm2.log"
|
||||
sleep 0.3
|
||||
OUT=`cat pm2.log | grep -n "[0-9]\{4\}\-[0-9]\{2\}\-[0-9]\{2\}" | wc -l`
|
||||
fi
|
||||
}
|
||||
function no_prefix {
|
||||
eval "grep_log \"$1\""
|
||||
echo "line count: $OUT"
|
||||
[ $OUT -eq 0 ] || fail "expect no timestamp prefix in pm2.log, but currently existing."
|
||||
success "have no timestamp prefix"
|
||||
rm_pm2log "$2"
|
||||
}
|
||||
function prefix {
|
||||
eval "grep_log \"$1\""
|
||||
echo "line count: $OUT"
|
||||
[ $OUT -ne 0 ] || fail "expect have timestamp prefix in pm2.log, but currently does not exist."
|
||||
success "have timestamp prefix"
|
||||
|
||||
rm_pm2log "$2"
|
||||
}
|
||||
|
||||
cd $file_path
|
||||
|
||||
$pm2 kill
|
||||
|
||||
if [ -z $TRAVIS ]
|
||||
then
|
||||
echo "removing pm2.log"
|
||||
rm -rf ~/.pm2/pm2.log
|
||||
fi
|
||||
|
||||
unset PM2_LOG_DATE_FORMAT
|
||||
|
||||
head ">> LIST (NO PREFIX)"
|
||||
no_prefix "ls" 0
|
||||
|
||||
head ">> START (NO PREFIX)"
|
||||
no_prefix "start echo.js" 1
|
||||
|
||||
head ">> RESTART (NO PREFIX)"
|
||||
no_prefix "restart echo" 1
|
||||
|
||||
head ">> STOP (NO PREFIX)"
|
||||
no_prefix "stop echo" 0
|
||||
|
||||
head ">> START JSON (NO PREFIX)"
|
||||
no_prefix "start echo-pm2.json" 1
|
||||
|
||||
head ">> RESTART JSON (NO PREFIX)"
|
||||
no_prefix "restart echo-pm2.json" 1
|
||||
|
||||
head ">> STOP-JSON (NO PREFIX)"
|
||||
no_prefix "stop echo-pm2.json" 0
|
||||
|
||||
export PM2_LOG_DATE_FORMAT="YYYY-MM-DD HH:mm Z"
|
||||
|
||||
head ">> LIST (PREFIX)"
|
||||
prefix "ls" 0
|
||||
|
||||
head ">> START (PREFIX)"
|
||||
prefix "start echo.js" 1
|
||||
|
||||
head ">> RESTART (PREFIX)"
|
||||
prefix "restart echo" 1
|
||||
|
||||
head ">> STOP (PREFIX)"
|
||||
prefix "stop echo" 0
|
||||
|
||||
head ">> START JSON (PREFIX)"
|
||||
prefix "start echo-pm2.json" 1
|
||||
|
||||
head ">> RESTART JSON (PREFIX)"
|
||||
prefix "restart echo-pm2.json" 1
|
||||
|
||||
head ">> STOP-JSON (PREFIX)"
|
||||
prefix "restart echo-pm2.json" 0
|
||||
|
||||
rm -rf pm2.log
|
||||
unset PM2_LOG_DATE_FORMAT
|
||||
touch ~/.pm2/pm2.log
|
||||
@ -59,6 +59,8 @@ bash ./test/bash/binary.sh
|
||||
spec "binary test"
|
||||
bash ./test/bash/log-entire.sh
|
||||
spec "merge stdout && stderr"
|
||||
bash ./test/bash/log-timestamp.sh
|
||||
spec "timetstamp prefix of pm2.log"
|
||||
|
||||
bash ./test/bash/inside-pm2.sh
|
||||
spec "Starting a process inside a PM2 process"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user