Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50665 views
1
var util = require('util'),
2
winston = require('../../winston'),
3
request = require('request'),
4
Stream = require('stream').Stream;
5
6
//
7
// ### function Http (options)
8
// #### @options {Object} Options for this instance.
9
// Constructor function for the Http transport object responsible
10
// for persisting log messages and metadata to a terminal or TTY.
11
//
12
var Http = exports.Http = function (options) {
13
options = options || {};
14
15
this.name = 'http';
16
this.ssl = !!options.ssl;
17
this.host = options.host || 'localhost';
18
this.port = options.port;
19
this.auth = options.auth;
20
this.path = options.path || '';
21
22
if (!this.port) {
23
this.port = this.ssl ? 443 : 80;
24
}
25
};
26
27
util.inherits(Http, winston.Transport);
28
29
//
30
// Expose the name of this Transport on the prototype
31
//
32
Http.prototype.name = 'http';
33
34
//
35
// ### function _request (options, callback)
36
// #### @callback {function} Continuation to respond to when complete.
37
// Make a request to a winstond server or any http server which can
38
// handle json-rpc.
39
//
40
Http.prototype._request = function (options, callback) {
41
var options = options || {},
42
auth = options.auth || this.auth,
43
path = options.path || this.path || '';
44
45
delete options.auth;
46
delete options.path;
47
48
options = { json: options };
49
options.method = 'POST';
50
options.url = 'http'
51
+ (this.ssl ? 's' : '')
52
+ '://'
53
+ (auth ? auth.username + ':' : '')
54
+ (auth ? auth.password + '@' : '')
55
+ this.host
56
+ ':'
57
+ this.port
58
+ '/'
59
+ path;
60
61
return request(options, callback);
62
};
63
64
//
65
// ### function log (level, msg, [meta], callback)
66
// #### @level {string} Level at which to log the message.
67
// #### @msg {string} Message to log
68
// #### @meta {Object} **Optional** Additional metadata to attach
69
// #### @callback {function} Continuation to respond to when complete.
70
// Core logging method exposed to Winston. Metadata is optional.
71
//
72
Http.prototype.log = function (level, msg, meta, callback) {
73
var self = this;
74
75
if (typeof meta === 'function') {
76
callback = meta;
77
meta = {};
78
}
79
80
var options = {
81
method: 'collect',
82
params: {
83
level: level,
84
message: msg,
85
meta: meta
86
}
87
};
88
89
// hack
90
if (meta.auth) {
91
options.auth = meta.auth;
92
delete meta.auth;
93
}
94
95
// hack
96
if (meta.path) {
97
options.path = meta.path;
98
delete meta.path;
99
}
100
101
this._request(options, function (err, res, body) {
102
if (res && res.statusCode !== 200) {
103
err = new Error('HTTP Status Code: ' + res.statusCode);
104
}
105
106
if (err) return callback(err);
107
108
// TODO: emit 'logged' correctly,
109
// keep track of pending logs.
110
self.emit('logged');
111
112
if (callback) callback(null, true);
113
});
114
};
115
116
//
117
// ### function query (options, callback)
118
// #### @options {Object} Loggly-like query options for this instance.
119
// #### @callback {function} Continuation to respond to when complete.
120
// Query the transport. Options object is optional.
121
//
122
Http.prototype.query = function (options, callback) {
123
if (typeof options === 'function') {
124
callback = options;
125
options = {};
126
}
127
128
var self = this,
129
options = this.normalizeQuery(options);
130
131
options = {
132
method: 'query',
133
params: options
134
};
135
136
this._request(options, function (err, res, body) {
137
if (res && res.statusCode !== 200) {
138
err = new Error('HTTP Status Code: ' + res.statusCode);
139
}
140
141
if (err) return callback(err);
142
143
if (typeof body === 'string') {
144
try {
145
body = JSON.parse(body);
146
} catch (e) {
147
return callback(e);
148
}
149
}
150
151
callback(null, body);
152
});
153
};
154
155
//
156
// ### function stream (options)
157
// #### @options {Object} Stream options for this instance.
158
// Returns a log stream for this transport. Options object is optional.
159
//
160
Http.prototype.stream = function (options) {
161
var self = this,
162
options = options || {},
163
stream = new Stream,
164
req,
165
buff;
166
167
stream.destroy = function () {
168
req.destroy();
169
};
170
171
options = {
172
method: 'stream',
173
params: options
174
};
175
176
req = this._request(options);
177
buff = '';
178
179
req.on('data', function (data) {
180
var data = (buff + data).split(/\n+/),
181
l = data.length - 1,
182
i = 0;
183
184
for (; i < l; i++) {
185
try {
186
stream.emit('log', JSON.parse(data[i]));
187
} catch (e) {
188
stream.emit('error', e);
189
}
190
}
191
192
buff = data[l];
193
});
194
195
req.on('error', function (err) {
196
stream.emit('error', err);
197
});
198
199
return stream;
200
};
201
202