Skip to content

Commit

Permalink
Merge pull request #7 from shinymayhem/emit-errors
Browse files Browse the repository at this point in the history
Emit errors
  • Loading branch information
thalesfsp committed Mar 31, 2016
2 parents 9518e7e + 114cd0e commit 1ab14f4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 29 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ npm install bunyan-elasticsearch

## Logstash Template

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:
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:

```
curl -XPUT localhost:9200/_template/logstash -d @template.json
Expand All @@ -25,7 +25,10 @@ var Elasticsearch = require('bunyan-elasticsearch');
var esStream = new Elasticsearch({
indexPattern: '[logstash-]YYYY.MM.DD',
type: 'logs',
host: 'localhost:9200'
host: 'localhost:9200'
});
esStream.on('error', function (err) {
console.log('Elasticsearch Stream Error:', err.stack);
});
var logger = bunyan.createLogger({
Expand All @@ -40,3 +43,11 @@ var logger = bunyan.createLogger({
logger.info('Starting application on port %d', app.get('port'));
```

## Options

* `client`: Elasticsearch client. Defaults to new client created with current set of options as an argument
* `type` {string|function}: Elasticsearch `type` field. Default: `'logs'`
* `indexPattern` {string}: Used to generate index if `index` option not set. Default: `'[logstash-]YYYY.MM.DD'`
* `index` {string|function}: Elasticsearch index. Defaults to index generated using index pattern

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
47 changes: 21 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var Writable = require('stream').Writable;
var domain = require('domain');
var util = require('util');
var elasticsearch = require('elasticsearch');
var moment = require('moment');
Expand Down Expand Up @@ -41,36 +40,32 @@ ElasticsearchStream.prototype._write = function (entry, encoding, callback) {
var index = this._index;
var type = this._type;

var d = domain.create();
d.on('error', function (err) {
console.log("Elasticsearch Error", err.stack);
});
d.run(function () {
entry = JSON.parse(entry.toString('utf8'));

// Reassign these fields so them match what the default Kibana dashboard
// expects to see.
entry['@timestamp'] = entry.time;
entry.level = levels[entry.level];
entry.message = entry.msg;
entry = JSON.parse(entry.toString('utf8'));

// remove duplicate fields
delete entry.time;
delete entry.msg;
// Reassign these fields so them match what the default Kibana dashboard
// expects to see.
entry['@timestamp'] = entry.time;
entry.level = levels[entry.level];
entry.message = entry.msg;

var datestamp = moment(entry.timestamp).format('YYYY.MM.DD');
// remove duplicate fields
delete entry.time;
delete entry.msg;

var options = {
index: callOrString(index, entry),
type: callOrString(type, entry),
body: entry
};
var datestamp = moment(entry.timestamp).format('YYYY.MM.DD');

client.create(options, function (err, resp) {
if (err) console.log('Elasticsearch Stream Error:', err.stack);
callback();
});
var options = {
index: callOrString(index, entry),
type: callOrString(type, entry),
body: entry
};

var self = this;
client.create(options, function (err, resp) {
if (err) {
self.emit('error', err);
}
callback();
});
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bunyan-elasticsearch",
"version": "0.0.5",
"version": "1.0.0",
"description": "A Bunyan stream for sending log data to Elasticsearch",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 1ab14f4

Please sign in to comment.