From 35dbc080157f140a7762e5b17fd7cdca3b6760fe Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Fri, 18 Aug 2017 18:01:34 +0800 Subject: [PATCH 1/2] Use winston instead of npmlog --- config/default.js | 2 +- index.js | 2 +- package.json | 4 ++-- src/app/middleware.js | 4 ++-- src/dao/mongo.js | 2 +- src/email/email.js | 2 +- src/index.js | 2 +- src/service/pgp.js | 2 +- test/integration/app-test.js | 2 +- test/integration/mongo-test.js | 2 +- test/integration/public-key-test.js | 2 +- test/unit/email-test.js | 2 +- test/unit/pgp-test.js | 2 +- 13 files changed, 15 insertions(+), 15 deletions(-) diff --git a/config/default.js b/config/default.js index a6fb1dc..f9559ae 100644 --- a/config/default.js +++ b/config/default.js @@ -3,7 +3,7 @@ module.exports = { log: { - level: 'silly' + level: process.env.LOG_LEVEL || 'silly' }, server: { diff --git a/index.js b/index.js index fe6da9c..33fa395 100644 --- a/index.js +++ b/index.js @@ -20,7 +20,7 @@ const cluster = require('cluster'); const numCPUs = require('os').cpus().length; const config = require('config'); -const log = require('npmlog'); +const log = require('winston'); log.level = config.log.level; diff --git a/package.json b/package.json index 9e27f6a..0deb600 100644 --- a/package.json +++ b/package.json @@ -29,8 +29,8 @@ "mongodb": "^2.2.31", "nodemailer": "^2.4.2", "nodemailer-openpgp": "^1.0.2", - "npmlog": "^4.0.2", - "openpgp": "^2.3.0" + "openpgp": "^2.3.0", + "winston": "^2.3.1" }, "devDependencies": { "chai": "^4.1.1", diff --git a/src/app/middleware.js b/src/app/middleware.js index 533ca1b..d56fc74 100644 --- a/src/app/middleware.js +++ b/src/app/middleware.js @@ -17,7 +17,7 @@ 'use strict'; -const log = require('npmlog'); +const log = require('winston'); const config = require('config'); const koaBody = require('koa-body'); const util = require('../service/util'); @@ -58,7 +58,7 @@ exports.parseBody = () => koaBody({ exports.logUnknownError = function(error, ctx) { if (error.status) { - log.verbose('middleware', 'Request faild: %s, %s', error.status, error.message); + log.verbose('middleware', `Request failed: ${error.status} ${error.message}`); } else { log.error('middleware', 'Unknown error', error, ctx); } diff --git a/src/dao/mongo.js b/src/dao/mongo.js index 12f5170..8107a2d 100644 --- a/src/dao/mongo.js +++ b/src/dao/mongo.js @@ -17,7 +17,7 @@ 'use strict'; -const log = require('npmlog'); +const log = require('winston'); const MongoClient = require('mongodb').MongoClient; /** diff --git a/src/email/email.js b/src/email/email.js index dcaf996..86ee2a8 100644 --- a/src/email/email.js +++ b/src/email/email.js @@ -17,7 +17,7 @@ 'use strict'; -const log = require('npmlog'); +const log = require('winston'); const util = require('../service/util'); const nodemailer = require('nodemailer'); const openpgpEncrypt = require('nodemailer-openpgp').openpgpEncrypt; diff --git a/src/index.js b/src/index.js index a5986bd..05c247d 100644 --- a/src/index.js +++ b/src/index.js @@ -17,7 +17,7 @@ 'use strict'; -const log = require('npmlog'); +const log = require('winston'); const config = require('config'); const init = require('./app'); diff --git a/src/service/pgp.js b/src/service/pgp.js index f7c049a..95c8a2f 100644 --- a/src/service/pgp.js +++ b/src/service/pgp.js @@ -17,7 +17,7 @@ 'use strict'; -const log = require('npmlog'); +const log = require('winston'); const util = require('./util'); const openpgp = require('openpgp'); const addressparser = require('addressparser'); diff --git a/test/integration/app-test.js b/test/integration/app-test.js index bd8a09e..2edf598 100644 --- a/test/integration/app-test.js +++ b/test/integration/app-test.js @@ -5,7 +5,7 @@ const Mongo = require('../../src/dao/mongo'); const nodemailer = require('nodemailer'); const config = require('config'); const fs = require('fs'); -const log = require('npmlog'); +const log = require('winston'); describe('Koa App (HTTP Server) Integration Tests', function() { this.timeout(20000); diff --git a/test/integration/mongo-test.js b/test/integration/mongo-test.js index ef20411..1e88d08 100644 --- a/test/integration/mongo-test.js +++ b/test/integration/mongo-test.js @@ -1,6 +1,6 @@ 'use strict'; -const log = require('npmlog'); +const log = require('winston'); const config = require('config'); const Mongo = require('../../src/dao/mongo'); diff --git a/test/integration/public-key-test.js b/test/integration/public-key-test.js index cf5f8a0..2d88ef4 100644 --- a/test/integration/public-key-test.js +++ b/test/integration/public-key-test.js @@ -1,6 +1,6 @@ 'use strict'; -const log = require('npmlog'); +const log = require('winston'); const config = require('config'); const nodemailer = require('nodemailer'); const Email = require('../../src/email/email'); diff --git a/test/unit/email-test.js b/test/unit/email-test.js index 68375a9..83a93eb 100644 --- a/test/unit/email-test.js +++ b/test/unit/email-test.js @@ -1,6 +1,6 @@ 'use strict'; -const log = require('npmlog'); +const log = require('winston'); const Email = require('../../src/email/email'); const nodemailer = require('nodemailer'); diff --git a/test/unit/pgp-test.js b/test/unit/pgp-test.js index 338ef74..43e386f 100644 --- a/test/unit/pgp-test.js +++ b/test/unit/pgp-test.js @@ -1,7 +1,7 @@ 'use strict'; const fs = require('fs'); -const log = require('npmlog'); +const log = require('winston'); const openpgp = require('openpgp'); const PGP = require('../../src/service/pgp'); From ba6f75984e5d641e6de6982ad8113f130b438f2d Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Fri, 18 Aug 2017 18:04:43 +0800 Subject: [PATCH 2/2] Integrate winston-papertrail plugin --- config/default.js | 5 +++++ index.js | 2 ++ package.json | 3 ++- src/dao/papertrail.js | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 src/dao/papertrail.js diff --git a/config/default.js b/config/default.js index f9559ae..dc433ec 100644 --- a/config/default.js +++ b/config/default.js @@ -6,6 +6,11 @@ module.exports = { level: process.env.LOG_LEVEL || 'silly' }, + papertrail: { + host: process.env.PAPERTRAIL_HOST, + port: process.env.PAPERTRAIL_PORT + }, + server: { port: process.env.PORT || 8888, httpsUpgrade: process.env.HTTPS_UPGRADE, diff --git a/index.js b/index.js index 33fa395..12cdf53 100644 --- a/index.js +++ b/index.js @@ -21,8 +21,10 @@ const cluster = require('cluster'); const numCPUs = require('os').cpus().length; const config = require('config'); const log = require('winston'); +const papertrail = require('./src/dao/papertrail'); log.level = config.log.level; +papertrail.init(config.papertrail); // // Start worker cluster depending on number of CPUs diff --git a/package.json b/package.json index 0deb600..5c7550f 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,8 @@ "nodemailer": "^2.4.2", "nodemailer-openpgp": "^1.0.2", "openpgp": "^2.3.0", - "winston": "^2.3.1" + "winston": "^2.3.1", + "winston-papertrail": "^1.0.5" }, "devDependencies": { "chai": "^4.1.1", diff --git a/src/dao/papertrail.js b/src/dao/papertrail.js new file mode 100644 index 0000000..e41034c --- /dev/null +++ b/src/dao/papertrail.js @@ -0,0 +1,36 @@ +/** + * Mailvelope - secure email with OpenPGP encryption for Webmail + * Copyright (C) 2016 Mailvelope GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License version 3 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +'use strict'; + +const log = require('winston'); +const config = require('config'); +require('winston-papertrail'); + +log.exitOnError = false; +log.level = config.log.level; + +exports.init = function({host, port}) { + if (!host || !port) { + return; + } + log.add(log.transports.Papertrail, { + level: config.log.level, + host, + port + }); +};