chore(logging): add Newrelic and Email transports for Winston

This will send an email on errors logged with winston and thus removes
the old error emailing code.
This commit is contained in:
Cole Gleason 2014-02-01 02:46:22 -06:00
parent ae847facda
commit 71f4371d9c
5 changed files with 29 additions and 15 deletions

View file

@ -11,6 +11,9 @@
"SMTP_USER":"user@domain.com",
"SMTP_PASS":"password",
"SMTP_SERVICE":"Gmail",
"SMTP_HOST":"smtp.gmail.com",
"SMTP_PORT": 587,
"SMTP_TLS": true,
"STRIPE_API_KEY":"aaaabbbbccccddddeeeeffff00001111",
"STRIPE_PUB_KEY":"22223333444455556666777788889999",
"PAYPAL_MERCHANT":"paypal-merchant@gmail.com",

View file

@ -46,7 +46,9 @@
"passport-facebook": "~1.0.2",
"newrelic": "~1.3.0",
"connect-ratelimit": "0.0.6",
"winston": "~0.7.2"
"winston": "~0.7.2",
"winston-mail": "~0.2.7",
"winston-newrelic": "~0.1.4"
},
"private": true,
"subdomain": "habitrpg",

View file

@ -1,10 +1,11 @@
var nconf = require('nconf');
var winston = require('winston');
require('winston-mail').Mail;
require('winston-newrelic');
var logger;
if (logger == null) {
// We currently don't support logging on Heroku
if (nconf.get('NODE_ENV') != 'production') {
logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({colorize: true}),
@ -12,6 +13,19 @@ if (logger == null) {
// TODO: Add email, loggly, or mongodb transports
]
});
if (nconf.get('NODE_ENV') == 'production') {
logger.add(winston.transport.newrelic, {});
logger.add(winston.transports.Mail, {
to: nconf.get('ADMIN_EMAIL') || nconf.get('SMTP_USER'),
from: "HabitRPG <" + nconf.get('SMTP_USER') + ">",
subject: "HabitRPG Error",
host: nconf.get('SMTP_HOST'),
port: nconf.get('SMTP_PORT'),
tls: nconf.get('SMTP_TLS'),
username: nconf.get('SMTP_USER'),
password: nconf.get('SMTP_PASS'),
level: 'error'
});
}
}

View file

@ -3,9 +3,8 @@ var cluster = require("cluster");
var _ = require('lodash');
var nconf = require('nconf');
var utils = require('./utils');
var logging = require('./logging');
utils.setupConfig();
var logging = require('./logging');
var isProd = nconf.get('NODE_ENV') === 'production';
var isDev = nconf.get('NODE_ENV') === 'development';

View file

@ -2,17 +2,17 @@ var nodemailer = require('nodemailer');
var nconf = require('nconf');
var crypto = require('crypto');
var path = require("path");
var logging = require('./logging');
module.exports.sendEmail = function(mailData) {
var smtpTransport = nodemailer.createTransport("SMTP",{
service: nconf.get('SMTP_SERVICE'),
service: nconf.get('SMTP_SERVICE'),
auth: {
user: nconf.get('SMTP_USER'),
pass: nconf.get('SMTP_PASS')
user: nconf.get('SMTP_USER'),
pass: nconf.get('SMTP_PASS')
}
});
smtpTransport.sendMail(mailData, function(error, response){
var logging = require('./logging');
if(error) logging.error(error);
else logging.info("Message sent: " + response.message);
smtpTransport.close(); // shut down the connection pool, no more messages
@ -49,6 +49,7 @@ module.exports.setupConfig = function(){
// // * https://developers.google.com/chrome-developer-tools/docs/heap-profiling
// // * https://developers.google.com/chrome-developer-tools/docs/memory-analysis-101
// agent = require('webkit-devtools-agent');
// var logging = require('./logging');
// logging.info("To debug memory leaks:" +
// "\n\t(1) Run `kill -SIGUSR2 " + process.pid + "`" +
// "\n\t(2) open http://c4milo.github.com/node-webkit-agent/21.0.1180.57/inspector.html?host=localhost:1337&page=0");
@ -70,12 +71,7 @@ module.exports.errorHandler = function(err, req, res, next) {
"\n\nheaders: " + JSON.stringify(req.headers) +
"\n\nbody: " + JSON.stringify(req.body) +
(res.locals.ops ? "\n\ncompleted ops: " + JSON.stringify(res.locals.ops) : "");
module.exports.sendEmail({
from: "HabitRPG <" + nconf.get('SMTP_USER') + ">",
to: nconf.get('ADMIN_EMAIL') || nconf.get('SMTP_USER'),
subject: "HabitRPG Error",
text: stack
});
var logging = require('./logging');
logging.error(stack);
var message = err.message ? err.message : err;
message = (message.length < 200) ? message : message.substring(0,100) + message.substring(message.length-100,message.length);