Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50675 views
1
/*
2
* webhook.js: Transport for logging to remote http endpoints ( POST / RECEIVE webhooks )
3
*
4
* (C) 2011 Marak Squires
5
* MIT LICENCE
6
*
7
*/
8
9
var events = require('events'),
10
http = require('http'),
11
https = require('https'),
12
util = require('util'),
13
cycle = require('cycle'),
14
common = require('../common'),
15
Transport = require('./transport').Transport;
16
17
//
18
// ### function WebHook (options)
19
// #### @options {Object} Options for this instance.
20
// Constructor function for the Console transport object responsible
21
// for making arbitrary HTTP requests whenever log messages and metadata
22
// are received.
23
//
24
var Webhook = exports.Webhook = function (options) {
25
Transport.call(this, options);
26
27
this.name = 'webhook';
28
this.host = options.host || 'localhost';
29
this.port = options.port || 8080;
30
this.method = options.method || 'POST';
31
this.path = options.path || '/winston-log';
32
33
if (options.auth) {
34
this.auth = {};
35
this.auth.username = options.auth.username || '';
36
this.auth.password = options.auth.password || '';
37
}
38
39
if (options.ssl) {
40
this.ssl = {};
41
this.ssl.key = options.ssl.key || null;
42
this.ssl.cert = options.ssl.cert || null;
43
this.ssl.ca = options.ssl.ca;
44
}
45
};
46
47
//
48
// Inherit from `winston.Transport`.
49
//
50
util.inherits(Webhook, Transport);
51
52
//
53
// Expose the name of this Transport on the prototype
54
//
55
Webhook.prototype.name = 'webhook';
56
57
//
58
// ### function log (level, msg, [meta], callback)
59
// #### @level {string} Level at which to log the message.
60
// #### @msg {string} Message to log
61
// #### @meta {Object} **Optional** Additional metadata to attach
62
// #### @callback {function} Continuation to respond to when complete.
63
// Core logging method exposed to Winston. Metadata is optional.
64
//
65
Webhook.prototype.log = function (level, msg, meta, callback) {
66
if (this.silent) {
67
return callback(null, true);
68
}
69
70
var self = this,
71
meta = cycle.decycle(meta),
72
message = common.clone(meta),
73
options,
74
req;
75
76
// Prepare options for outgoing HTTP request
77
options = {
78
host: this.host,
79
port: this.port,
80
path: this.path,
81
method: this.method,
82
headers: { 'Content-Type': 'application/json' }
83
};
84
85
if (this.ssl) {
86
options.ca = this.ssl.ca;
87
options.key = this.ssl.key;
88
options.cert = this.ssl.cert;
89
}
90
91
if (this.auth) {
92
// Encode `Authorization` header used by Basic Auth
93
options.headers['Authorization'] = 'Basic ' + new Buffer(
94
this.auth.username + ':' + this.auth.password, 'utf8'
95
).toString('base64');
96
}
97
98
// Perform HTTP logging request
99
req = (self.ssl ? https : http).request(options, function (res) {
100
// TODO: emit 'logged' correctly,
101
// keep track of pending logs.
102
self.emit('logged');
103
if (callback) callback(null, true);
104
callback = null;
105
});
106
107
req.on('error', function (err) {
108
//
109
// Propagate the `error` back up to the `Logger` that this
110
// instance belongs to.
111
//
112
self.emit('error', err);
113
if (callback) callback(err, false);
114
callback = null;
115
});
116
117
//
118
// Write logging event to the outgoing request body
119
//
120
// jsonMessage is currently conforming to JSON-RPC v1.0,
121
// but without the unique id since there is no anticipated response
122
// see: http://en.wikipedia.org/wiki/JSON-RPC
123
//
124
125
var params = common.clone(meta) || {};
126
params.timestamp = new Date();
127
params.message = msg;
128
params.level = level;
129
130
req.write(JSON.stringify({
131
method: 'log',
132
params: params
133
}));
134
135
req.end();
136
};
137
138