-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
45 lines (41 loc) · 1.47 KB
/
index.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
"use strict";
var uuid = require('node-uuid');
module.exports = function Splunk(config){
// config -> { namespace: 'something', timestamp: true }
return {
txid: function(){
return uuid.v4();
},
log: function(obj){
var msg = config.timestamp ? 'ts=' + (new Date()).toISOString() + ' ': '';
msg += config.namespace ? 'ns=' + config.namespace + ' ' : '';
msg += 'env=' + process.env.NODE_ENV;
delete obj.contextConfig;
for(var key in obj){
if(obj.hasOwnProperty([key])) {
var value = obj[key];
if(value === null || value === undefined) continue;
if(typeof value != 'object'){
value = '' + value;
value = value.replace(/\n|\r/g, ' ');
}
else {
try {
value = JSON.stringify(value);
} catch(e){
value = 'some recursive object';
}
}
if(/[ "]/.test(value))
value = '"' + value.replace(/"/g, '\\"') + '"';
msg += ' ' + key + '=' + value;
}
}
try{
console.log('METRIC ' + msg);
} catch(e){
console.log('error', e);
}
}
};
};