From 3616157e5de58f7f9b112299dc6c11157eaa47f5 Mon Sep 17 00:00:00 2001 From: butlerx Date: Tue, 2 Jan 2018 11:42:12 +0000 Subject: [PATCH] use logger to catch fatal errors rather then console --- config/config.js | 2 +- package.json | 2 +- service.js | 71 ++++++++++++++++++++++++------------------------ yarn.lock | 8 +++--- 4 files changed, 41 insertions(+), 42 deletions(-) diff --git a/config/config.js b/config/config.js index f52e788b..9286799d 100644 --- a/config/config.js +++ b/config/config.js @@ -51,7 +51,7 @@ module.exports = function (options) { } } }, - + log: options && options.log ? options.log : undefined, 'recaptcha_secret_key': process.env.RECAPTCHA_SECRET_KEY || '6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe', transport: { type: 'web', diff --git a/package.json b/package.json index 4a929947..20ad595e 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "covrep": "bash test/covrep.sh", "lint": "./node_modules/.bin/semistandard *.js config/config.js", "start": "node service.js", - "dev": "nodemon service.js" + "dev": "nodemon service.js | bunyan" }, "pre-commit": [ "test" diff --git a/service.js b/service.js index a8fa83b4..3ae8b127 100644 --- a/service.js +++ b/service.js @@ -4,20 +4,21 @@ require('events').EventEmitter.prototype._maxListeners = 100; const newrelic = process.env.NEW_RELIC_ENABLED === 'true' ? require('newrelic') : undefined; const senecaNR = require('seneca-newrelic'); -var config = require('./config/config.js')(); + +const service = 'cp-users-service'; +const { logger, log } = require('cp-logs-lib')({ + name: service, + level: process.env.NODE_ENV === 'production' ? 'warn' : 'info' +}); +const config = require('./config/config.js')({ log }); var seneca = require('seneca')(config); var _ = require('lodash'); var store = require('seneca-postgresql-store'); var storeQuery = require('seneca-store-query'); -var service = 'cp-users-service'; -var log = require('cp-logs-lib')({name: service, level: 'warn'}); -config.log = log.log; var util = require('util'); var dgram = require('dgram'); -if (process.env.NODE_ENV !== 'production') { - seneca.log.info('using config', JSON.stringify(config, null, 4)); -} +logger.info(config, 'config'); seneca.options(config); seneca.decorate('customValidatorLogFormatter', require('./lib/custom-validator-log-formatter')); @@ -31,45 +32,39 @@ if (process.env.MAILDEV_ENABLED === 'true') { function shutdown (err) { if (err !== undefined) { - var error = { - date: new Date().toString(), - msg: err.stack !== undefined + logger.error( + err, + err.stack !== undefined ? 'FATAL: UncaughtException, please report: ' + util.inspect(err.stack) - : 'FATAL: UncaughtException, no stack trace', - err: util.inspect(err) - }; - console.error(JSON.stringify(error)); + : 'FATAL: UncaughtException, no stack trace'); process.exit(1); } process.exit(0); } require('./migrate-psql-db.js')(function (err) { - if (err) { - console.error(err); - process.exit(-1); - } - console.log('Migrations ok'); + if (err) shutdown(err); + logger.info('Migrations ok'); seneca.use(require('./email-notifications.js')); seneca.use(require('./lib/agreements')); - seneca.use(require('./profiles.js'), - { postgresql: config['postgresql-store'], - logger: log.logger - }); - seneca.use(require('./oauth2.js'), {clients: config.oauth2.clients}); + seneca.use(require('./profiles.js'), { + postgresql: config['postgresql-store'], + logger + }); + seneca.use(require('./oauth2.js'), { clients: config.oauth2.clients }); seneca.use('user'); seneca.use('auth'); - seneca.use(require('./users.js'), - { 'email-notifications': config['email-notifications'], - 'postgresql': config['postgresql-store'], - 'users': config['users'], - 'logger': log.logger - }); - seneca.use(require('./user-profile.js'), - { postgresql: config['postgresql-store'], - logger: log.logger - }); + seneca.use(require('./users.js'), { + 'email-notifications': config['email-notifications'], + postgresql: config['postgresql-store'], + users: config['users'], + logger + }); + seneca.use(require('./user-profile.js'), { + postgresql: config['postgresql-store'], + logger + }); seneca.use(require('./nodebb-api.js'), config.nodebb); seneca.use(require('cp-permissions-plugin'), { config: __dirname + '/config/permissions' @@ -106,7 +101,11 @@ require('./migrate-psql-db.js')(function (err) { seneca.wrap('role: entity, cmd: ' + cmd, function filterFields (args, cb) { try { ['limit$', 'skip$'].forEach(function (field) { - if (args.q[field] && args.q[field] !== 'NULL' && !/^[0-9]+$/g.test(args.q[field] + '')) { + if ( + args.q[field] && + args.q[field] !== 'NULL' && + !/^[0-9]+$/g.test(args.q[field] + '') + ) { throw new Error('Expect limit$, skip$ to be a number'); } }); @@ -124,7 +123,7 @@ require('./migrate-psql-db.js')(function (err) { } if (args.q.fields$) { args.q.fields$.forEach(function (field, index) { - args.q.fields$[index] = '\"' + escape(field) + '\"'; + args.q.fields$[index] = '"' + escape(field) + '"'; }); } // Loop over each props diff --git a/yarn.lock b/yarn.lock index f62cef8c..d64a45a6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -915,10 +915,10 @@ core-util-is@1.0.2, core-util-is@~1.0.0: async "^2.0.0-rc.5" lodash "^4.13.1" -cp-translations@^1.0.146: - version "1.0.146" - resolved "https://registry.yarnpkg.com/cp-translations/-/cp-translations-1.0.146.tgz#dee5c55263fb31c7c5c3e4e8fd573f3a05921f8d" - integrity sha512-h2DRtFybsA2tvcnWg8FI5ZmxyBgavMDr9F+Cp0e78VgyJJusJLPvuSSHHtUxwnYKNsrdgCWZKnPwRkwJXXJtsQ== +cp-translations@^1.0.149: + version "1.0.149" + resolved "https://registry.yarnpkg.com/cp-translations/-/cp-translations-1.0.149.tgz#e09845115a80380cf19c49dfd003d8ed75578690" + integrity sha512-AQa/d/MDNAM8y70gWbE6a65R9ZUYP11WWOF056Eq8Ttngdzg6j1WsmkV4VuZQB5NSIYrZIuM2UoCWIOfC20kmw== cross-spawn-async@^2.0.0, cross-spawn-async@^2.1.8: version "2.2.5"