forked from winstonjs/winston
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loggly.js
116 lines (98 loc) · 3.01 KB
/
loggly.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* loggly.js: Transport for logginh to remote Loggly API
*
* (C) 2010 Charlie Robbins
* MIT LICENCE
*
*/
var events = require('events'),
loggly = require('loggly'),
util = require('util'),
internal = require('../internal');
//
// ### function Loggly (options)
// #### @options {Object} Options for this instance.
// Constructor function for the Loggly transport object responsible
// for persisting log messages and metadata to Loggly; 'LaaS'.
//
var Loggly = exports.Loggly = function (options) {
events.EventEmitter.call(this);
options = options || {};
if (!options.subdomain) {
throw new Error('Loggly Subdomain is required');
}
if (!options.inputToken && !options.inputName) {
throw new Error('Target input token or name is required.');
}
if (!options.auth && options.inputName) {
throw new Error('Loggly authentication is required');
}
this.name = 'loggly';
this.level = options.level || 'info';
this.logBuffer = [];
this.client = loggly.createClient({
subdomain: options.subdomain,
auth: options.auth || null
});
if (options.inputToken) {
this.inputToken = options.inputToken;
this.ready = true;
}
else if (options.inputName) {
this.ready = false;
this.inputName = options.inputName;
var self = this;
this.client.getInput(this.inputName, function (err, input) {
if (err) {
throw err;
}
self.inputToken = input.input_token;
self.ready = true;
});
}
};
//
// Inherit from `events.EventEmitter`.
//
util.inherits(Loggly, events.EventEmitter);
//
// ### function log (level, msg, [meta], callback)
// #### @level {string} Level at which to log the message.
// #### @msg {string} Message to log
// #### @meta {Object} **Optional** Additional metadata to attach
// #### @callback {function} Continuation to respond to when complete.
// Core logging method exposed to Winston. Metadata is optional.
//
Loggly.prototype.log = function (level, msg, meta, callback) {
var message = internal.clone(meta);
message.level = level;
message.message = msg;
// If we haven't gotten the input token yet
// add this message to the log buffer.
if (!this.ready) {
this.logBuffer.push(message);
return callback(null, true);
}
// Otherwise if we have buffered messages
// add this message to the buffer and flush them.
if (this.ready && this.logBuffer.length > 0) {
this.logBuffer.push(message);
return this.flush(callback);
}
// Otherwise just log the message as normal
this.client.log(this.inputToken, message, callback);
};
Loggly.prototype.flush = function (callback) {
var self = this, flushed = 0,
length = this.logBuffer.length;
// Close over each message
this.logBuffer.forEach(function (msg) {
process.nextTick(function () {
this.client.log(self.inputToken, msg, function () {
if (flushed++ === count) return callback(null, true);
});
});
});
// Then quickly truncate the list (thanks isaacs)
this.logBuffer.length = 0;
};