diff --git a/box.js b/box.js index 927bb0a26..5460919d4 100755 --- a/box.js +++ b/box.js @@ -7,23 +7,17 @@ let async = require('async'), fs = require('fs'), ldap = require('./src/ldap.js'), paths = require('./src/paths.js'), - server = require('./src/server.js'), - util = require('util'); + server = require('./src/server.js'); const NOOP_CALLBACK = function () { }; function setupLogging(callback) { if (process.env.BOX_ENV === 'test') return callback(); - fs.open(paths.BOX_LOG_FILE, 'a', function (error, fd) { - if (error) return callback(error); + var logfileStream = fs.createWriteStream(paths.BOX_LOG_FILE, { flags:'a' }); + process.stdout.write = process.stderr.write = logfileStream.write.bind(logfileStream); - require('debug').log = function (...args) { - fs.appendFileSync(fd, util.format(...args) + '\n'); - }; - - callback(); - }); + callback(); } async.series([ @@ -37,7 +31,9 @@ async.series([ process.exit(1); } - const debug = require('debug')('box:box'); // require this here so that logging handler is already setup + // require those here so that logging handler is already setup + require('supererror'); + const debug = require('debug')('box:box'); process.on('SIGINT', function () { debug('Received SIGINT. Shutting down.'); @@ -57,5 +53,10 @@ async.series([ setTimeout(process.exit.bind(process), 3000); }); + process.on('uncaughtException', function (error) { + console.error((error && error.stack) ? error.stack : error); + setTimeout(process.exit.bind(process, 1), 3000); + }); + console.log(`Cloudron is up and running. Logs are at ${paths.BOX_LOG_FILE}`); // this goes to journalctl }); diff --git a/package-lock.json b/package-lock.json index 032b07c35..eb794a781 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4611,6 +4611,36 @@ } } }, + "supererror": { + "version": "0.7.2", + "resolved": "https://registry.npmjs.org/supererror/-/supererror-0.7.2.tgz", + "integrity": "sha512-eMrdX2uNcTW74XwOBUHnEwv+ZF5BCGqCpTxrcYN60oiUttF3wFBayhya5tTsYIOWKm9RpEkfWaYkVxq38oDL5w==", + "requires": { + "colors": "^1.2.1", + "prettyjson": "^1.2.1" + }, + "dependencies": { + "colors": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/colors/-/colors-1.2.1.tgz", + "integrity": "sha512-s8+wktIuDSLffCywiwSxQOMqtPxML11a/dtHE17tMn4B1MSWw/C22EKf7M2KGUBcDaVFEGT+S8N02geDXeuNKg==" + }, + "minimist": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", + "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=" + }, + "prettyjson": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/prettyjson/-/prettyjson-1.2.1.tgz", + "integrity": "sha1-/P+rQdGcq0365eV15kJGYZsS0ok=", + "requires": { + "colors": "1.2.1", + "minimist": "1.2.0" + } + } + } + }, "supports-color": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-6.0.0.tgz", diff --git a/package.json b/package.json index 7bb3883c2..d5842dd58 100644 --- a/package.json +++ b/package.json @@ -61,6 +61,7 @@ "speakeasy": "^2.0.0", "split": "^1.0.1", "superagent": "^5.2.2", + "supererror": "^0.7.2", "tar-fs": "github:cloudron-io/tar-fs#ignore_stat_error", "tar-stream": "^2.1.2", "tldjs": "^2.3.1",