cocalc/src / smc-project / node_modules / broadway / node_modules / winston / lib / winston / transports / webhook.js
50665 views/*1* webhook.js: Transport for logging to remote http endpoints ( POST / RECEIVE webhooks )2*3* (C) 2011 Marak Squires4* MIT LICENCE5*6*/78var events = require('events'),9http = require('http'),10https = require('https'),11util = require('util'),12cycle = require('cycle'),13common = require('../common'),14Transport = require('./transport').Transport;1516//17// ### function WebHook (options)18// #### @options {Object} Options for this instance.19// Constructor function for the Console transport object responsible20// for making arbitrary HTTP requests whenever log messages and metadata21// are received.22//23var Webhook = exports.Webhook = function (options) {24Transport.call(this, options);2526this.name = 'webhook';27this.host = options.host || 'localhost';28this.port = options.port || 8080;29this.method = options.method || 'POST';30this.path = options.path || '/winston-log';3132if (options.auth) {33this.auth = {};34this.auth.username = options.auth.username || '';35this.auth.password = options.auth.password || '';36}3738if (options.ssl) {39this.ssl = {};40this.ssl.key = options.ssl.key || null;41this.ssl.cert = options.ssl.cert || null;42this.ssl.ca = options.ssl.ca;43}44};4546//47// Inherit from `winston.Transport`.48//49util.inherits(Webhook, Transport);5051//52// Expose the name of this Transport on the prototype53//54Webhook.prototype.name = 'webhook';5556//57// ### function log (level, msg, [meta], callback)58// #### @level {string} Level at which to log the message.59// #### @msg {string} Message to log60// #### @meta {Object} **Optional** Additional metadata to attach61// #### @callback {function} Continuation to respond to when complete.62// Core logging method exposed to Winston. Metadata is optional.63//64Webhook.prototype.log = function (level, msg, meta, callback) {65if (this.silent) {66return callback(null, true);67}6869var self = this,70meta = cycle.decycle(meta),71message = common.clone(meta),72options,73req;7475// Prepare options for outgoing HTTP request76options = {77host: this.host,78port: this.port,79path: this.path,80method: this.method,81headers: { 'Content-Type': 'application/json' }82};8384if (this.ssl) {85options.ca = this.ssl.ca;86options.key = this.ssl.key;87options.cert = this.ssl.cert;88}8990if (this.auth) {91// Encode `Authorization` header used by Basic Auth92options.headers['Authorization'] = 'Basic ' + new Buffer(93this.auth.username + ':' + this.auth.password, 'utf8'94).toString('base64');95}9697// Perform HTTP logging request98req = (self.ssl ? https : http).request(options, function (res) {99// TODO: emit 'logged' correctly,100// keep track of pending logs.101self.emit('logged');102if (callback) callback(null, true);103callback = null;104});105106req.on('error', function (err) {107//108// Propagate the `error` back up to the `Logger` that this109// instance belongs to.110//111self.emit('error', err);112if (callback) callback(err, false);113callback = null;114});115116//117// Write logging event to the outgoing request body118//119// jsonMessage is currently conforming to JSON-RPC v1.0,120// but without the unique id since there is no anticipated response121// see: http://en.wikipedia.org/wiki/JSON-RPC122//123124var params = common.clone(meta) || {};125params.timestamp = new Date();126params.message = msg;127params.level = level;128129req.write(JSON.stringify({130method: 'log',131params: params132}));133134req.end();135};136137138