Skip to content
This repository has been archived by the owner on Jul 16, 2018. It is now read-only.

Commit

Permalink
Bug 882969 - Use Messina and Bunyan for pretty GELF logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher De Cairos committed Nov 15, 2013
1 parent 24c0915 commit 0a6c7b5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 37 deletions.
50 changes: 16 additions & 34 deletions app.js
Expand Up @@ -24,7 +24,9 @@ var app = express(),
}),
NODE_ENV = env.get("NODE_ENV"),
WWW_ROOT = path.resolve(__dirname, "public"),
server;
server,
messina,
logger;

nunjucksEnv.addFilter("instantiate", function (input) {
var tmpl = new nunjucks.Template(input);
Expand Down Expand Up @@ -82,40 +84,20 @@ var routes = require("./routes");
nunjucksEnv.express(app);
app.disable("x-powered-by");

app.use(express.logger(NODE_ENV === "development" ? "dev" : ""));
if (env.get("ENABLE_GELF_LOGS")) {
messina = require("messina");
logger = messina("webmaker.org-" + env.get("NODE_ENV") || "development");
logger.init();
app.use(logger.middleware());
} else {
app.use(express.logger());
}

if ( !! env.get("FORCE_SSL")) {
app.use(helmet.hsts());
app.enable("trust proxy");
}

/**
* Crash isolation and error handling, logging
*/
if (env.get("GRAYLOG_HOST")) {
GLOBAL.graylogHost = env.get("GRAYLOG_HOST");
GLOBAL.graylogFacility = env.get("GRAYLOG_FACILITY");
require("graylog");
}

function reportError(error, isFatal) {
try {
var severity = isFatal ? "CRASH" : "ERROR";
console.error(severity + ": " + error.stack);
if (!GLOBAL.graylogHost) {
return;
}
log("[" + severity + "] webmaker.org failure.",
error.message, {
level: isFatal ? LOG_CRIT : LOG_ERR,
stack: error.stack,
_fullStack: error.stack
}
);
} catch (err) {
console.error("Internal Error: unable to report error to graylog, err=" + err);
}
}

app.use(function (req, res, next) {
var guard = domain.create();
guard.add(req);
Expand All @@ -140,8 +122,8 @@ app.use(function (req, res, next) {
// But don't keep the process open just for that!
killtimer.unref();

// Try and report this crash to graylog
reportError(err, true);
// report this crash
console.error(err);

// Try and shutdown the server, cluster worker
isolate(function () {
Expand All @@ -166,7 +148,7 @@ app.use(function (req, res, next) {

guard.dispose();
} catch (err2) {
console.error('Internal error shutting down domain: ', err2.stack);
logger.error('Internal error shutting down domain: ', err2.stack);
}

process.exit(1);
Expand Down Expand Up @@ -254,7 +236,7 @@ app.use(function (req, res, next) {
// Final error-handling middleware
app.use(function (err, req, res, next) {
err.status = err.status || 500;
reportError(err);
console.error(err);
res.status(err.status);
res.render('error.html', {
message: err.message,
Expand Down
4 changes: 2 additions & 2 deletions env.dist
Expand Up @@ -78,8 +78,8 @@ export AWS_SECRET_KEY="irrelephent"
# Disable sending the event creation email by default until we're allowed to
export ALLOW_SEND_EVENT_CREATE_EMAIL=false

# GRAYLOG (optional, leave blank if not using Graylog)
export GRAYLOG_HOST="hostname-for-graylog-server"
# Set to true to enable GELF formatted logging. setting to false will enable express.logger for logging.
export ENABLE_GELF_LOGS=false

# What domains to enable SSO on. Space-delimited
export SSO_DOMAINS="http://localhost:3000 http://localhost:3500 http://localhost:5000 http://localhost:7777 http://localhost:8888 http://localhost:12416"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -17,14 +17,14 @@
"commander": "2.0.x",
"csv": "0.3.x",
"express": "3.2.0",
"graylog": "0.1.3",
"habitat": "0.4.1",
"helmet": "0.0.11",
"knox": "0.8.x",
"less-middleware": "0.1.12",
"makeapi-client": "https://github.com/mozilla/makeapi-client/tarball/v0.5.11",
"mapquest": "0.0.1",
"markdown": "0.4.x",
"messina": "0.1.1",
"moment": "2.2.1",
"mox-server": "https://github.com/Pomax/mox-server/archive/master.tar.gz",
"mysql": "2.0.x",
Expand Down

0 comments on commit 0a6c7b5

Please sign in to comment.