Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/API/LogManagement.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ module.exports = function(CLI) {
Common.printError(err);
return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT);
}

var found = false;

list.forEach(function(l) {
if (typeof api == 'undefined') {
found = true;
Common.printOut(cst.PREFIX_MSG + 'Flushing:');
Common.printOut(cst.PREFIX_MSG + l.pm2_env.pm_out_log_path);
Common.printOut(cst.PREFIX_MSG + l.pm2_env.pm_err_log_path);
Expand All @@ -42,6 +46,7 @@ module.exports = function(CLI) {
fs.closeSync(fs.openSync(l.pm2_env.pm_err_log_path, 'w'));
}
else if (l.pm2_env.pm_id == api || l.pm2_env.name === api) {
found = true;
Common.printOut(cst.PREFIX_MSG + 'Flushing:');

if (l.pm2_env.pm_log_path && fs.existsSync(l.pm2_env.pm_log_path)) {
Expand All @@ -61,7 +66,12 @@ module.exports = function(CLI) {
}
});

Common.printOut(cst.PREFIX_MSG + 'Logs flushed');
if (found) {
Common.printOut(cst.PREFIX_MSG + 'Logs flushed');
} else if (typeof api !== 'undefined') {
Common.printError(cst.PREFIX_MSG_ERR + 'No process found with name or id \'' + api + '\'');
return cb ? cb(Common.retErr('process not found')) : that.exitCli(cst.ERROR_EXIT);
}
return cb ? cb(null, list) : that.exitCli(cst.SUCCESS_EXIT);
});
};
Expand Down
45 changes: 45 additions & 0 deletions test/programmatic/flush.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,50 @@ describe('Programmatic flush feature test', function() {
});
});
});
it('flush logs by pm_id', function(done) {
pm2.start({
script: './echo.js',
name: 'first',
error_file : 'error-first.log',
out_file : 'out-first.log',
merge_logs: false
}, function(err, procs) {
should(err).be.null();
var out_file = procs[0].pm2_env.pm_out_log_path;
var err_file = procs[0].pm2_env.pm_err_log_path;
var pm_id = String(procs[0].pm2_env.pm_id);
pm2.start({
script: './001-test.js',
name: 'second',
error_file : 'error-second.log',
out_file : 'out-second.log',
merge_logs: false
}, function(err, procs) {
should(err).be.null();
var out_file1 = procs[0].pm2_env.pm_out_log_path;
var err_file1 = procs[0].pm2_env.pm_err_log_path;
pm2.flush(pm_id, function(){
fs.readFileSync(out_file, "utf8").toString().should.be.empty();
fs.readFileSync(err_file, "utf8").toString().should.be.empty();
fs.readFileSync(out_file1, "utf8").toString().should.not.be.empty();
fs.readFileSync(err_file1, "utf8").toString().should.not.be.empty();
done();
});
});
});
});
it('should return error when process not found', function(done) {
pm2.start({
script: './echo.js',
name: 'myapp',
merge_logs: false
}, function(err) {
should(err).be.null();
pm2.flush('nonexistent', function(err) {
should(err).not.be.null();
done();
});
});
});
});
});