(tests) add test for json log feature #2663

This commit is contained in:
vmarchaud 2017-01-31 16:26:10 +01:00
parent 77ce1cd7ee
commit 613979da2f
9 changed files with 309 additions and 15 deletions

View File

@ -7,6 +7,7 @@
- `pm2 ecosystem simple` to generate a simple ecosystem file
- aliasing: `pm2-dev <script>` <=> `pm2-dev start <script>`
- fix git parsing when using cwd
- #2663 allow to directly output json when logging
## 2.3.0

View File

@ -122,11 +122,11 @@ module.exports = function ForkMode(God) {
if (pm2_env.log_type && pm2_env.log_type === 'json') {
log_data = JSON.stringify({
message : data.toString(),
timestamp : pm2_env.log_date_format ? moment().format(pm2_env.log_date_format) : moment(packet.at),
timestamp : pm2_env.log_date_format ? moment().format(pm2_env.log_date_format) : new Date().toISOString(),
type : 'err',
process_id : cspr.pm2_env.pm_id,
app_name : cspr.pm2_env.name
})
}) + '\n';
}
else if (pm2_env.log_date_format)
log_data = moment().format(pm2_env.log_date_format) + ': ' + data.toString();
@ -157,17 +157,17 @@ module.exports = function ForkMode(God) {
if (pm2_env.log_type && pm2_env.log_type === 'json') {
log_data = JSON.stringify({
message : data.toString(),
timestamp : pm2_env.log_date_format ? moment().format(pm2_env.log_date_format) : moment(),
timestamp : pm2_env.log_date_format ? moment().format(pm2_env.log_date_format) : new Date().toISOString(),
type : 'out',
process_id : cspr.pm2_env.pm_id,
app_name : cspr.pm2_env.name
})
}) + '\n';
}
else if (pm2_env.log_date_format)
log_data = moment().format(pm2_env.log_date_format) + ': ' + data.toString();
else
log_data = data.toString();
stds.std && stds.std.write && stds.std.write(log_data);
// hardcoded values of special log path to not write on disk

View File

@ -194,13 +194,14 @@ function exec(script, stds) {
if (pm2_env.log_type && pm2_env.log_type === 'json') {
log_data = JSON.stringify({
message : string.toString(),
timestamp : pm2_env.log_date_format ? moment().format(pm2_env.log_date_format) : moment(),
timestamp : pm2_env.log_date_format && moment ?
moment().format(pm2_env.log_date_format) : new Date().toISOString(),
type : 'err',
process_id : pm2_env.pm_id,
app_name : pm2_env.name
})
}) + '\n';
}
else if (pm2_env.log_date_format)
else if (pm2_env.log_date_format && moment)
log_data = moment().format(pm2_env.log_date_format) + ': ' + string.toString();
else
log_data = string.toString();
@ -227,13 +228,14 @@ function exec(script, stds) {
if (pm2_env.log_type && pm2_env.log_type === 'json') {
log_data = JSON.stringify({
message : string.toString(),
timestamp : pm2_env.log_date_format ? moment().format(pm2_env.log_date_format) : moment(),
timestamp : pm2_env.log_date_format && moment ?
moment().format(pm2_env.log_date_format) : new Date().toISOString(),
type : 'out',
process_id : cspr.pm2_env.pm_id,
app_name : cspr.pm2_env.name
})
process_id : pm2_env.pm_id,
app_name : pm2_env.name
}) + '\n';
}
else if (pm2_env.log_date_format)
else if (pm2_env.log_date_format && moment)
log_data = moment().format(pm2_env.log_date_format) + ': ' + string.toString();
else
log_data = string.toString();

View File

@ -136,8 +136,9 @@ var Utility = module.exports = {
flows.push(function(next){
var file = stds[io];
if (!file) return false;
// if file contains ERR or /dev/null, dont try to create stream since he dont want logs
if (!file || file.indexOf('NULL') > -1 || file.indexOf('/dev/null') > -1)
return next();
stds[io] = fs.createWriteStream(file, {flags: 'a'})
.on('error', function(err){
next(err);

72
test/bash/log-json.sh Normal file
View File

@ -0,0 +1,72 @@
#!/usr/bin/env bash
SRC=$(cd $(dirname "$0"); pwd)
source "${SRC}/include.sh"
cd $file_path/log-json/
rm output.log
# fork mode json logs
$pm2 start ecosystem.json --only one-echo
! test -f output.log
sleep 2
node -pe 'JSON.parse(process.argv[1])' `cat output.log`
spec 'should have parsed valid json'
$pm2 delete all
rm output.log
# cluster mode json logs
$pm2 start ecosystem.json -i 2 --only one-echo-cluster
! test -f output.log
sleep 2
node -pe 'JSON.parse(process.argv[1])' `cat output.log`
spec 'should have parsed valid json'
$pm2 delete all
rm output.log
CURRENT_YEAR=`date +"%Y"`
# fork mode with date
$pm2 start ecosystem.json --only one-echo-date
! test -f output.log
sleep 2
node -pe 'JSON.parse(process.argv[1])' `cat output.log`
spec 'should have parsed valid json'
OUT=`cat output.log | grep -o "$CURRENT_YEAR" | wc -l`
[ $OUT -eq 1 ] || fail "should contains custom timestamp"
success "should contains custom timestamp"
$pm2 delete all
rm output.log
# cluster mode with date
$pm2 start ecosystem.json --only one-echo-cluster-date
! test -f output.log
sleep 2
node -pe 'JSON.parse(process.argv[1])' `cat output.log`
spec 'should have parsed valid json'
OUT=`cat output.log | grep -o "$CURRENT_YEAR" | wc -l`
[ $OUT -eq 1 ] || fail "should contains custom timestamp in cluster mode"
success "should contains custom timestamp in cluster mode"
$pm2 delete all
rm output.log

169
test/bash/log-null.sh Normal file
View File

@ -0,0 +1,169 @@
#!/usr/bin/env bash
SRC=$(cd $(dirname "$0"); pwd)
source "${SRC}/include.sh"
cd $file_path
rm ~/.pm2/logs/echo-out.log
rm ~/.pm2/logs/echo-error.log
echo ">>>>>>>>>>>>>>>>>>>> LOG PATH SET TO NULL"
# set error log to null in fork
$pm2 start echo.js -o out.log -e NULL --merge-logs
sleep 1
test -f out.log
spec "err log should exist with null in fork mode"
! test -f ~/.pm2/logs/echo-error.log
spec "err log shouldnt exist with null in fork mode"
$pm2 delete all
rm out.log
# set error log to null in cluster
$pm2 start echo.js -i 1 -o out.log -e NULL --merge-logs
sleep 1
test -f out.log
spec "err log should exist with null in cluster mode"
! test -f ~/.pm2/logs/echo-error.log
spec "err log shouldnt exist with null in cluster mode"
$pm2 delete all
rm out.log
# set out log to null in fork
$pm2 start echo.js -o NULL -e err.log --merge-logs
sleep 1
test -f err.log
spec "err log should exist with null in fork mode"
! test -f ~/.pm2/logs/echo-out.log
spec "output log shouldnt exist with null in fork mode"
$pm2 delete all
rm err.log
# set out log to null in cluster
$pm2 start echo.js -i 1 -o NULL -e err.log --merge-logs
sleep 1
test -f err.log
spec "err log should exist with null in cluster mode"
! test -f ~/.pm2/logs/echo-out.log
spec "output log shouldnt exis with null in cluster mode"
$pm2 delete all
rm err.log
# set error AND out log to null in cluster
$pm2 start echo.js -i 1 -o NULL -e NULL --merge-logs
sleep 1
! test -f ~/.pm2/logs/echo-out.log
spec "out log shouldnt exist with null in cluster mode"
! test -f ~/.pm2/logs/echo-error.log
spec "error log shouldnt exist with null in cluster mode"
$pm2 delete all
# set error AND out log to null in fork
$pm2 start echo.js -o NULL -e NULL --merge-logs
sleep 1
! test -f ~/.pm2/logs/echo-out.log
spec "out log shouldnt exist with null in fork mode"
! test -f ~/.pm2/logs/echo-error.log
spec "error log shouldnt exist with null in fork mode"
$pm2 delete all
rm ~/.pm2/logs/echo-out.log
rm ~/.pm2/logs/echo-error.log
echo ">>>>>>>>>>>>>>>>>>>> LOG PATH SET TO /dev/null"
# set error log to null in fork
$pm2 start echo.js -o out.log -e /dev/null --merge-logs
sleep 1
test -f out.log
spec "err log should exist with /dev/null in fork mode"
! test -f ~/.pm2/logs/echo-error.log
spec "err log shouldnt exist with /dev/null in fork mode"
$pm2 delete all
rm out.log
# set error log to null in cluster
$pm2 start echo.js -i 1 -o out.log -e /dev/null --merge-logs
sleep 1
test -f out.log
spec "err log should exist with /dev/null in cluster mode"
! test -f ~/.pm2/logs/echo-error.log
spec "err log shouldnt exist with /dev/null in cluster mode"
$pm2 delete all
rm out.log
# set out log to null in fork
$pm2 start echo.js -o /dev/null -e err.log --merge-logs
sleep 1
test -f err.log
spec "err log should exist with /dev/null in fork mode"
! test -f ~/.pm2/logs/echo-out.log
spec "output log shouldnt exist with /dev/null in fork mode"
$pm2 delete all
rm err.log
# set out log to null in cluster
$pm2 start echo.js -i 1 -o /dev/null -e err.log --merge-logs
sleep 1
test -f err.log
spec "err log should exist with /dev/null in cluster mode"
! test -f ~/.pm2/logs/echo-out.log
spec "output log shouldnt exis with /dev/null in cluster mode"
$pm2 delete all
rm err.log
# set error AND out log to null in cluster
$pm2 start echo.js -i 1 -o /dev/null -e /dev/null --merge-logs
sleep 1
! test -f ~/.pm2/logs/echo-out.log
spec "out log shouldnt exist with /dev/null in cluster mode"
! test -f ~/.pm2/logs/echo-error.log
spec "error log shouldnt exist with /dev/null in cluster mode"
$pm2 delete all
# set error AND out log to null in fork
$pm2 start echo.js -o /dev/null -e /dev/null --merge-logs
sleep 1
! test -f ~/.pm2/logs/echo-out.log
spec "out log shouldnt exist with /dev/null in fork mode"
! test -f ~/.pm2/logs/echo-error.log
spec "error log shouldnt exist with /dev/null in fork mode"
$pm2 delete all

44
test/fixtures/log-json/ecosystem.json vendored Normal file
View File

@ -0,0 +1,44 @@
[{
"name" : "one-echo",
"script" : "one-echo.js",
"log_type": "json",
"log_file": "output.log",
"merge_logs": true,
"out_file": "NULL",
"error_file": "NULL",
"autorestart": false
},
{
"name" : "one-echo-cluster",
"script" : "one-echo.js",
"exec_mode": "cluster_mode",
"log_type": "json",
"log_file": "output.log",
"merge_logs": true,
"out_file": "NULL",
"error_file": "NULL",
"autorestart": false
},
{
"name" : "one-echo-date",
"script" : "one-echo.js",
"log_type": "json",
"log_file": "output.log",
"log_date_format": "YYYY",
"merge_logs": true,
"out_file": "NULL",
"error_file": "NULL",
"autorestart": false
},
{
"name" : "one-echo-cluster-date",
"script" : "one-echo.js",
"exec_mode": "cluster_mode",
"log_type": "json",
"log_file": "output.log",
"log_date_format": "YYYY",
"merge_logs": true,
"out_file": "NULL",
"error_file": "NULL",
"autorestart": false
}]

1
test/fixtures/log-json/one-echo.js vendored Normal file
View File

@ -0,0 +1 @@
console.log('echo')

View File

@ -117,6 +117,10 @@ bash ./test/bash/wait-ready-event.sh
spec "Wait for application ready event"
bash ./test/bash/serve.sh
spec "pm2 serve CLI method"
bash ./test/bash/log-null.sh
spec "Logging path set to null"
bash ./test/bash/log-json.sh
spec "Logging directly to file in json"
# Issues related
bash ./test/bash/issues/2337.sh