Skip to content

Commit

Permalink
for mozilla#55: convert console to mozlog
Browse files Browse the repository at this point in the history
  • Loading branch information
groovecoder committed Sep 21, 2018
1 parent 6852573 commit 9151cd8
Show file tree
Hide file tree
Showing 16 changed files with 2,259 additions and 2,178 deletions.
3 changes: 3 additions & 0 deletions .env-dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ SERVER_URL=http://localhost:6060
PORT=6060
COOKIE_SECRET=3895d33b5f9730f5eb2a2067fe0a690e298f55f5e382c032fd3656863412

# see https://www.npmjs.com/package/mozlog
MOZLOG_LEVEL=debug

# 1: disables the dockerflow endpoints
# see: https://github.com/mozilla-services/Dockerflow#containerized-app-requirements
DISABLE_DOCKERFLOW=
Expand Down
1 change: 1 addition & 0 deletions app-constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const kEnvironmentVariables = [
"BASKET_API_KEY",
"BASKET_NEWSLETTER",
"FXA_ENABLED",
"MOZLOG_LEVEL",
"OAUTH_AUTHORIZATION_URI",
"OAUTH_TOKEN_URI",
"OAUTH_PROFILE_URI",
Expand Down
7 changes: 5 additions & 2 deletions controllers/dockerflow.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ const path = require("path");

const {version, homepage} = require("../package.json");

const mozlog = require("../log");


const log = mozlog("dockerflow");
const versionJsonPath = path.join(__dirname, "..", "version.json");

// If the version.json file already exists (e.g., created by circle + docker),
// don't need to generate it
if (!fs.existsSync(versionJsonPath)) {
console.log("Could not find version.json, generating ...");
log.info("generating");
let commit;
try {
commit = require("git-rev-sync").short();
} catch (err) {
console.error("Error in git-rev-sync: ", err);
log.error("generating", {err: err});
}

const versionJson = {
Expand Down
9 changes: 7 additions & 2 deletions controllers/hibp.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ const EmailUtils = require("../email-utils");
const HIBP = require("../hibp");
const sha1 = require("../sha1-utils");
const HBSHelpers = require("../hbs-helpers");
const mozlog = require("../log");


const log = mozlog("controllers.hibp");


async function notify (req, res) {
if(!["breachName", "hashPrefix", "hashSuffixes"].every(req.body.hasOwnProperty, req.body)) {
Expand All @@ -29,7 +34,7 @@ async function notify (req, res) {
const hashes = req.body.hashSuffixes.map(suffix=>reqHashPrefix + suffix.toLowerCase());
const subscribers = await DB.getSubscribersByHashes(hashes);

console.log(`Found ${subscribers.length} subscribers in ${breachAlert.Name}. Notifying ...`);
log.info("notification", { length: subscribers.length, breachAlertName: breachAlert.Name });

const notifiedSubscribers = [];

Expand Down Expand Up @@ -57,7 +62,7 @@ async function notify (req, res) {
notifiedSubscribers.push(email);
}
}
console.log(`Notified ${notifiedSubscribers.length} unique subscribers.`);
log.info("notified", { length: notifiedSubscribers.length });
res.status(200);
res.json(
{info: "Notified subscribers of breach."}
Expand Down
9 changes: 6 additions & 3 deletions controllers/ses.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ const MessageValidator = require("sns-validator");

const DB = require("../db/DB");

const mozlog = require("../log");


const validator = new MessageValidator();
const log = mozlog("controllers.ses");


async function notification(req, res) {
const message = JSON.parse(req.body);
return new Promise((resolve, reject) => {
validator.validate(message, async (err, message) => {
if (err) {
console.error(err);
log.error(err);
const body = "Access denied. " + err.message;
res.status(401).send(body);
return reject(body);
Expand All @@ -31,7 +34,7 @@ async function notification(req, res) {


async function handleNotification(notification) {
console.log("Received SES message, ID: ", notification.MessageId);
log.info("received-SES", { id: notification.MessageId });
const message = JSON.parse(notification.Message);
switch (message.eventType) {
case "Bounce":
Expand All @@ -41,7 +44,7 @@ async function handleNotification(notification) {
await handleComplaintMessage(message);
break;
default:
console.log("Unhandled eventType: ", message.eventType);
log.info("unhandled-eventType", { type: message.eventType });
}
}

Expand Down
7 changes: 5 additions & 2 deletions db/DB.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ const AppConstants = require("../app-constants");
const HIBP = require("../hibp");
const Basket = require("../basket");
const getSha1 = require("../sha1-utils");
const mozlog = require("../log");

const knexConfig = require("./knexfile");

let knex = Knex(knexConfig);
const log = mozlog("DB");


const DB = {
Expand Down Expand Up @@ -131,10 +134,10 @@ const DB = {
await knex("subscribers")
.where("id", "=", aEntry.id)
.del();
console.log("Removed subscriber ID: ", aEntry.id);
log.info("removed-subscriber", { id: aEntry.id });
return aEntry;
}, async () => {
console.warn("removeSubscriber called with email not found in database.");
log.warn("removed-subscriber-not-found");
return;
});
},
Expand Down
7 changes: 5 additions & 2 deletions email-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ const nodemailer = require("nodemailer");
const hbs = require("nodemailer-express-handlebars");

const HBSHelpers = require("./hbs-helpers");
const mozlog = require("./log");


const log = mozlog("email-utils");

const hbsOptions = {
viewEngine: {
extname: ".hbs",
Expand All @@ -29,7 +32,7 @@ const EmailUtils = {

// Allow a debug mode that will log JSON instead of sending emails.
if (!smtpUrl) {
console.info("smtpUrl is empty, EmailUtils will log a JSON response instead of sending emails.");
log.info("smtpUrl-empty", { message: "EmailUtils will log a JSON response instead of sending emails." });
gTransporter = nodemailer.createTransport({jsonTransport: true});
return Promise.resolve(true);
}
Expand Down Expand Up @@ -67,7 +70,7 @@ const EmailUtils = {
return;
}
if (gTransporter.transporter.name === "JSONTransport") {
console.log(info.message.toString());
log.info("JSONTransport", { message: info.message.toString() });
}
resolve(info);
});
Expand Down
8 changes: 7 additions & 1 deletion hbs-helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
"use strict";

const mozlog = require("./log");


const log = mozlog("hbs-helpers");

function breachDataClasses(dataClasses) {
if (dataClasses.constructor === Array) {
return dataClasses.join(", ");
Expand Down Expand Up @@ -49,7 +54,8 @@ function ifCompare(v1, operator, v2, options) {
}
return options.inverse(this);
}
return console.error(`Error: ${operator} not found`);
log.error("ifCompare", {message: `${operator} not found`});
return;
}


Expand Down
12 changes: 7 additions & 5 deletions hibp.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ const createDOMPurify = require("dompurify");
const { JSDOM } = require("jsdom");

const AppConstants = require("./app-constants");
const mozlog = require("./log");
const pkg = require("./package.json");


const DOMPurify = createDOMPurify((new JSDOM("")).window);
const HIBP_USER_AGENT = `${pkg.name}/${pkg.version}`;
const log = mozlog("hibp");


const HIBP = {
Expand All @@ -29,14 +31,14 @@ const HIBP = {
response = await got(url, reqOptions);
return response;
} catch (err) {
console.error("got an error: " + err);
log.error("_throttledGot", {err: err});
if (err.statusCode === 404) {
// 404 can mean "no results", return undefined response; sorry calling code
return response;
} else if (err.statusCode === 429) {
console.log("got a 429, tryCount: ", tryCount);
log.info("_throttledGot", {err: "got a 429, tryCount: " + tryCount});
if (tryCount >= AppConstants.HIBP_THROTTLE_MAX_TRIES) {
console.error(err.message);
log.error("_throttledGot", {err: err});
throw new Error("Too many connections to HIBP.");
} else {
tryCount++;
Expand All @@ -63,7 +65,7 @@ const HIBP = {
},

async loadBreachesIntoApp(app) {
console.log("Loading breaches from HIBP into app.locals");
log.info("loadBreachesIntoApp", {});
try {
const breachesResponse = await this.req("/breaches");
const breaches = [];
Expand All @@ -80,7 +82,7 @@ const HIBP = {
} catch (error) {
throw new Error("Could not load breaches: " + error);
}
console.log("Done loading breaches.");
log.info("Done loading breaches");
},

async getUnsafeBreachesForEmail(sha1, allBreaches) {
Expand Down
14 changes: 14 additions & 0 deletions log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict";

const mozlog = require("mozlog");

const AppConstants = require("./app-constants");


const log = mozlog({
app: "fx-monitor",
level: AppConstants.MOZLOG_LEVEL,
});


module.exports = log;
5 changes: 4 additions & 1 deletion middleware.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"use strict";

const mozlog = require("./log");


const log = mozlog("middleware");
// Helps handle errors for all async route controllers
// See https://medium.com/@Abazhenov/using-async-await-in-express-with-node-8-b8af872c0016
function asyncMiddleware (fn) {
Expand All @@ -11,7 +14,7 @@ function asyncMiddleware (fn) {


function logErrors (err, req, res, next) {
console.error(err.stack);
log.error("error", {stack: err.stack});
next(err);
}

Expand Down
Loading

0 comments on commit 9151cd8

Please sign in to comment.