Skip to content

Commit 1ab14f4

Browse files
committed
Merge pull request #7 from shinymayhem/emit-errors
Emit errors
2 parents 9518e7e + 114cd0e commit 1ab14f4

File tree

3 files changed

+35
-29
lines changed

3 files changed

+35
-29
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ npm install bunyan-elasticsearch
1111

1212
## Logstash Template
1313

14-
By default Logstash will create a dynamic template that will take care of crating `.raw` fields for your data. In order to replicate this behaivor you will need to create the dynamic template manually. You will need to [download template.json](https://raw.github.com/ccowan/bunyan-elasticsearch/master/template.json) and run the following command from the same directory as that file:
14+
By default Logstash will create a dynamic template that will take care of crating `.raw` fields for your data. In order to replicate this behavior you will need to create the dynamic template manually. You will need to [download template.json](https://raw.github.com/ccowan/bunyan-elasticsearch/master/template.json) and run the following command from the same directory as that file:
1515

1616
```
1717
curl -XPUT localhost:9200/_template/logstash -d @template.json
@@ -25,7 +25,10 @@ var Elasticsearch = require('bunyan-elasticsearch');
2525
var esStream = new Elasticsearch({
2626
indexPattern: '[logstash-]YYYY.MM.DD',
2727
type: 'logs',
28-
host: 'localhost:9200'
28+
host: 'localhost:9200'
29+
});
30+
esStream.on('error', function (err) {
31+
console.log('Elasticsearch Stream Error:', err.stack);
2932
});
3033
3134
var logger = bunyan.createLogger({
@@ -40,3 +43,11 @@ var logger = bunyan.createLogger({
4043
logger.info('Starting application on port %d', app.get('port'));
4144
```
4245

46+
## Options
47+
48+
* `client`: Elasticsearch client. Defaults to new client created with current set of options as an argument
49+
* `type` {string|function}: Elasticsearch `type` field. Default: `'logs'`
50+
* `indexPattern` {string}: Used to generate index if `index` option not set. Default: `'[logstash-]YYYY.MM.DD'`
51+
* `index` {string|function}: Elasticsearch index. Defaults to index generated using index pattern
52+
53+
Options `type` and `index` can be either a string or function. For these options, when the option is set to a function, the function is passed the log entry object as an argument

index.js

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
var Writable = require('stream').Writable;
2-
var domain = require('domain');
32
var util = require('util');
43
var elasticsearch = require('elasticsearch');
54
var moment = require('moment');
@@ -41,36 +40,32 @@ ElasticsearchStream.prototype._write = function (entry, encoding, callback) {
4140
var index = this._index;
4241
var type = this._type;
4342

44-
var d = domain.create();
45-
d.on('error', function (err) {
46-
console.log("Elasticsearch Error", err.stack);
47-
});
48-
d.run(function () {
49-
entry = JSON.parse(entry.toString('utf8'));
50-
51-
// Reassign these fields so them match what the default Kibana dashboard
52-
// expects to see.
53-
entry['@timestamp'] = entry.time;
54-
entry.level = levels[entry.level];
55-
entry.message = entry.msg;
43+
entry = JSON.parse(entry.toString('utf8'));
5644

57-
// remove duplicate fields
58-
delete entry.time;
59-
delete entry.msg;
45+
// Reassign these fields so them match what the default Kibana dashboard
46+
// expects to see.
47+
entry['@timestamp'] = entry.time;
48+
entry.level = levels[entry.level];
49+
entry.message = entry.msg;
6050

61-
var datestamp = moment(entry.timestamp).format('YYYY.MM.DD');
51+
// remove duplicate fields
52+
delete entry.time;
53+
delete entry.msg;
6254

63-
var options = {
64-
index: callOrString(index, entry),
65-
type: callOrString(type, entry),
66-
body: entry
67-
};
55+
var datestamp = moment(entry.timestamp).format('YYYY.MM.DD');
6856

69-
client.create(options, function (err, resp) {
70-
if (err) console.log('Elasticsearch Stream Error:', err.stack);
71-
callback();
72-
});
57+
var options = {
58+
index: callOrString(index, entry),
59+
type: callOrString(type, entry),
60+
body: entry
61+
};
7362

63+
var self = this;
64+
client.create(options, function (err, resp) {
65+
if (err) {
66+
self.emit('error', err);
67+
}
68+
callback();
7469
});
7570
};
7671

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bunyan-elasticsearch",
3-
"version": "0.0.5",
3+
"version": "1.0.0",
44
"description": "A Bunyan stream for sending log data to Elasticsearch",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)