forked from vanthome/winston-elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
120 lines (92 loc) · 3.25 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
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
117
118
119
var util = require('util')
var winston = require('winston');
var elastical = require('elastical');
var cluster = require('cluster');
var _basename = require('path').basename;
var _dirname = require('path').dirname;
/**
* Constructor
*
*
*/
var Elasticsearch = module.exports = winston.transports.Elasticsearch = function Elasticsearch( options ) {
options = options || {};
// Enforce context
if( !( this instanceof Elasticsearch ) ) {
return new Elasticsearch( options );
}
// Set defaults
this.level = options.level || 'info';
this.indexName = options.indexName || 'logs'
this.fireAndForget = !!options.fireAndForget;
// Only set typeName if provided, otherwise we will use "level" for types.
this.typeName = options.typeName || null;
// Could get more sexy and grab the name from the parent's package.
this.source = options.source || _dirname( process.mainModule.filename ) || module.filename;
// Automatically added entry fields
this.disable_fields = options.disable_fields || false;
// Set client and bail if ready
if( options.client ){
this.client = options.client;
return this;
}
// Create Elastical Client
this.client = new elastical.Client( options.host || 'localhost', {
port: options.port || 9200,
auth: options.auth || '',
protocol: options.protocol || 'http',
curlDebug: !!options.curlDebug,
basePath: options.basePath || '', // <- ?
timeout: options.timeout || 60000
});
// Return for good measure.
return this;
};
util.inherits( Elasticsearch, winston.Transport );
/**
* Handle Log Entries
*
*
*/
Elasticsearch.prototype.log = function log( level, msg, meta, callback ) {
var self = this;
var args = Array.prototype.slice.call( arguments, 0 );
// Not sure if Winston always passed a callback and regulates number of args, but we are on the safe side here
callback = 'function' === typeof args[ args.length - 1 ] ? args[ args.length - 1 ] : function fallback() {};
// Using some Logstash naming conventions. (https://gist.github.com/jordansissel/2996677) with some useful variables for debugging.
var entry = {
level: level,
source: self.source,
timestamp: +new Date, // double-check that format will not cause issues w/ ES
message: msg
}
// Add auto-generated fields unless disabled
if( !this.disable_fields ) {
entry.fields = {
worker: cluster.isWorker,
pid: process.pid,
path: module.parent.filename,
user: process.env.USER,
main: process.mainModule.filename,
uptime: process.uptime(),
rss: process.memoryUsage().rss,
heapTotal: process.memoryUsage().heapTotal,
heapUsed: process.memoryUsage().heapUsed
};
}
// Add tags only if they exist
if( meta && meta.tags ) {
entry.tags = meta && meta.tags;
}
// Need to debug callbacks, they seem to be always called in the incorect context.
this.client.index( this.indexName, this.typeName || entry.level || 'log', entry, function done( error, res ) {
// If we are ignoring callbacks
if( callback && self.fireAndForget ){
return callback( null );
}
if( callback ) {
return callback( error, res );
}
});
return this;
};