Switch to bunyan for logging

This commit is contained in:
Brandon Keepers 2016-12-05 22:18:52 -06:00
parent d58c5d8f75
commit 8be66d9bc4
No known key found for this signature in database
GPG Key ID: F9533396D5FACBF6
6 changed files with 21 additions and 17 deletions

View File

@ -1,13 +1,13 @@
const debug = require('debug')('PRobot');
const Sandbox = require('./sandbox');
const Workflow = require('./workflow');
const url = require('./util/github-url');
const log = require('./log');
module.exports = class Configuration {
static load(context, path) {
const options = url(path);
debug('Fetching %s from %s', path, context.payload.repository.full_name);
return context.github.repos.getContent(context.toRepo(options)).then(data => {
const options = context.toRepo(url(path));
log.debug(options, 'Fetching config');
return context.github.repos.getContent(options).then(data => {
return new Configuration(context).parse(new Buffer(data.content, 'base64').toString());
});
}
@ -43,7 +43,7 @@ module.exports = class Configuration {
contents(path) {
const options = this.context.toRepo(url(path));
debug('Getting contents', options);
log.debug(options, 'Getting contents');
return this.context.github.repos.getContent(options).then(data => {
return new Buffer(data.content, 'base64').toString();
});

View File

@ -1,12 +1,11 @@
const process = require('process');
const debug = require('debug')('PRobot');
const GitHubApi = require('github');
const jwt = require('./jwt');
const log = require('./log');
module.exports = {auth};
const tokens = {};
const enableDebug = process.env.DEBUG && process.env.DEBUG.includes('github');
const enableDebug = log.level() <= 10;
// Authenticate as the given installation
function auth(id) {
@ -26,7 +25,7 @@ function createToken(id) {
const github = new GitHubApi({debug: enableDebug});
github.authenticate({type: 'integration', token: jwt()});
debug('creating token for installation', id);
log.debug({installation: id}, 'creating token for installation');
return github.integrations.createInstallationToken({
installation_id: id

7
lib/log.js Normal file
View File

@ -0,0 +1,7 @@
const process = require('process');
const bunyan = require('bunyan');
module.exports = bunyan.createLogger({
name: 'PRobot',
level: process.env.LOG_LEVEL || 'info'
});

View File

@ -1,4 +1,4 @@
const debug = require('debug')('PRobot');
const log = require('./log');
const installations = require('./installations');
const Context = require('./context');
const Configuration = require('./configuration');
@ -9,7 +9,7 @@ class Robot {
}
receive(event) {
debug('webhook', event);
log.trace('webhook', event);
if (event.payload.repository) {
installations.auth(event.payload.installation.id).then(github => {

View File

@ -10,7 +10,7 @@
"author": "Brandon Keepers",
"license": "ISC",
"dependencies": {
"debug": "2.2.0",
"bunyan": "^1.8.5",
"dotenv": "^2.0.0",
"expect": "^1.20.2",
"github": "^6.0.4",

View File

@ -3,18 +3,16 @@ require('dotenv').config({silent: true});
const process = require('process');
const http = require('http');
const createHandler = require('github-webhook-handler');
const debug = require('debug')('PRobot');
const log = require('./lib/log');
const robot = require('./lib/robot');
const PORT = process.env.PORT || 3000;
const webhook = createHandler({path: '/', secret: process.env.WEBHOOK_SECRET || 'development'});
debug('Starting');
http.createServer((req, res) => {
webhook(req, res, err => {
if (err) {
console.error(err);
log.error(err);
res.statusCode = 500;
res.end('Something has gone terribly wrong.');
} else {
@ -28,7 +26,7 @@ robot.listen(webhook);
// Show trace for any unhandled rejections
process.on('unhandledRejection', reason => {
console.error(reason);
log.error(reason);
});
console.log('Listening on http://localhost:' + PORT);