Merge pull request #117 from probot/raven-log

Logs to Sentry
This commit is contained in:
Brandon Keepers 2017-04-08 17:41:47 -05:00 committed by GitHub
commit 215f2b6387
4 changed files with 43 additions and 24 deletions

View File

@ -1,7 +1,11 @@
const bunyan = require('bunyan');
const bunyanFormat = require('bunyan-format');
const sentryStream = require('bunyan-sentry-stream');
const cacheManager = require('cache-manager');
const createWebhook = require('github-webhook-handler');
const createIntegration = require('github-integration');
const createWebhook = require('github-webhook-handler');
const Raven = require('raven');
const createRobot = require('./lib/robot');
const createServer = require('./lib/server');
@ -11,6 +15,12 @@ module.exports = options => {
ttl: 60 * 60 // 1 hour
});
const logger = bunyan.createLogger({
name: 'PRobot',
level: process.env.LOG_LEVEL || 'debug',
stream: bunyanFormat({outputMode: process.env.LOG_FORMAT || 'short'})
});
const webhook = createWebhook({path: '/', secret: options.secret});
const integration = createIntegration({
id: options.id,
@ -18,18 +28,20 @@ module.exports = options => {
debug: process.env.LOG_LEVEL === 'trace'
});
const server = createServer(webhook);
const robot = createRobot(integration, webhook, cache);
const robot = createRobot({integration, webhook, cache, logger});
if (process.env.SENTRY_URL) {
Raven.config(process.env.SENTRY_URL, {
captureUnhandledRejections: true
captureUnhandledRejections: true,
autoBreadcrumbs: true
}).install({});
logger.addStream(sentryStream(Raven));
}
// Handle case when webhook creation fails
webhook.on('error', err => {
Raven.captureException(err);
robot.log.error(err);
logger.error(err);
});
return {
@ -38,7 +50,7 @@ module.exports = options => {
start() {
server.listen(options.port);
robot.log.trace('Listening on http://localhost:' + options.port);
logger.trace('Listening on http://localhost:' + options.port);
},
load(plugin) {

View File

@ -1,20 +1,13 @@
const bunyan = require('bunyan');
const bunyanFormat = require('bunyan-format');
const GitHubApi = require('github');
const Bottleneck = require('bottleneck');
const Context = require('./context');
const logger = bunyan.createLogger({
name: 'PRobot',
level: process.env.LOG_LEVEL || 'debug',
stream: bunyanFormat({outputMode: process.env.LOG_FORMAT || 'short'})
});
class Robot {
constructor(integration, webhook, cache) {
constructor({integration, webhook, cache, logger}) {
this.integration = integration;
this.webhook = webhook;
this.cache = cache;
this.log = wrapLogger(logger);
this.webhook.on('*', event => this.log.trace(event, 'webhook received'));
}
@ -46,10 +39,6 @@ class Robot {
return probotEnhancedClient(github);
}
log(...args) {
return logger.debug(...args);
}
}
function probotEnhancedClient(github) {
@ -71,9 +60,21 @@ function rateLimitedClient(github) {
return github;
}
// Add level methods on the logger
['trace', 'debug', 'info', 'warn', 'error', 'fatal'].forEach(level => {
Robot.prototype.log[level] = logger[level].bind(logger);
});
// Return a function that defaults to "debug" level, and has properties for
// other levels:
//
// robot.log("debug")
// robot.log.trace("verbose details");
//
function wrapLogger(logger) {
const fn = logger.debug.bind(logger);
// Add level methods on the logger
['trace', 'debug', 'info', 'warn', 'error', 'fatal'].forEach(level => {
fn[level] = logger[level].bind(logger);
});
return fn;
}
module.exports = (...args) => new Robot(...args);

View File

@ -17,6 +17,7 @@
"bottleneck": "^1.15.1",
"bunyan": "^1.8.5",
"bunyan-format": "^0.2.1",
"bunyan-sentry-stream": "^1.1.0",
"cache-manager": "^2.4.0",
"commander": "^2.9.0",
"dotenv": "~4.0.0",

View File

@ -3,13 +3,18 @@ const expect = require('expect');
const Context = require('../lib/context');
const createRobot = require('../lib/robot');
const nullLogger = {};
['trace', 'debug', 'info', 'warn', 'error', 'fatal'].forEach(level => {
nullLogger[level] = function () { };
});
describe('Robot', () => {
let webhook;
let robot;
beforeEach(() => {
webhook = new EventEmitter();
robot = createRobot({}, webhook);
robot = createRobot({webhook, logger: nullLogger});
});
describe('on', () => {