Skip to content
This repository has been archived by the owner on Feb 1, 2019. It is now read-only.

Commit

Permalink
rewrite for good v7.x.x (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjihrig committed Jun 24, 2016
1 parent aa3f8dc commit 7a3c5fc
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 147 deletions.
2 changes: 2 additions & 0 deletions .npmignore
@@ -0,0 +1,2 @@
*
!lib/**
7 changes: 1 addition & 6 deletions .travis.yml
@@ -1,10 +1,5 @@
sudo: false
language: node_js
node_js:
- "0.10"
- "0.12"
- "4"
- "5"
- "iojs"
- "iojs-v1"
- "iojs-v2"
- "6"
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,7 +1,7 @@
The MIT License (MIT)

Copyright (c) 2014 Frederic Hemberger
Copyright (c) 2015 Continuation Labs
Copyright (c) 2015-2016 Continuation Labs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
6 changes: 5 additions & 1 deletion README.md
Expand Up @@ -10,7 +10,11 @@ Loggly broadcasting for Good.

**Credit:** This module was originally written and maintained by [fhemberger](https://github.com/fhemberger).

Version >=1.x.x requires `good@6.x.x`. For older versions of `good`, please use [v0.1.4](https://github.com/continuationlabs/good-loggly/releases/tag/v0.1.4) instead.
## Compatibility

- `good@7.x.x` is compatible with version >=3.x.x.
- `good@6.x.x` is compatible with version 1.x.x and 2.x.x.
- For older versions of `good`, use [v0.1.4](https://github.com/continuationlabs/good-loggly/releases/tag/v0.1.4).

## Usage

Expand Down
91 changes: 38 additions & 53 deletions lib/index.js
@@ -1,46 +1,49 @@
'use strict';
var Assert = require('assert');
var GoodSqueeze = require('good-squeeze');
var Loggly = require('loggly');
var Merge = require('lodash.merge');


function GoodLoggly (events, config) {
if (!(this instanceof GoodLoggly)) {
return new GoodLoggly(events, config);
const Assert = require('assert');
const Stream = require('stream');
const Loggly = require('loggly');
const Merge = require('lodash.merge');


class GoodLoggly extends Stream.Writable {
constructor (config) {
config = Merge({}, config);

Assert(typeof config.token === 'string', 'Loggly API token required');
Assert(typeof config.subdomain === 'string', 'Loggly subdomain required');

super({ objectMode: true, decodeStrings: false });
this._config = config;
this._client = Loggly.createClient({
token: config.token,
subdomain: config.subdomain,
json: true,
// Tags in JSON logs don't seem to appear properly in Loggly when
// sent with the X-LOGGLY-TAG header
useTagHeader: false,
tags: Array.isArray(config.tags) ? config.tags : []
});
}
_write (data, encoding, callback) {
// Map hapi event data fields to Loggly fields
if (this._config.name) {
data.name = this._config.name;
}

if (this._config.hostname) {
data.hostname = this._config.hostname;
}

data.timestamp = GoodLoggly.timeString(data.timestamp);
data.msg = GoodLoggly.getMessage(data);
this._client.log(data, data.tags, callback);
}

config = config || {};

Assert(!(events && events.ops), 'ops events are not supported by Loggly');
Assert(typeof config.token === 'string', 'Loggly API token required');
Assert(typeof config.subdomain === 'string', 'Loggly subdomain required');

this._client = Loggly.createClient({
token: config.token,
subdomain: config.subdomain,
json: true,

// Tags in JSON logs don't seem to appear properly in Loggly when sent with the X-LOGGLY-TAG header
useTagHeader: false,
tags: Array.isArray(config.tags) ? config.tags : []
});
this._config = Merge({}, config);
this._streams = {
squeeze: GoodSqueeze.Squeeze(events)
};
}

module.exports = GoodLoggly;


GoodLoggly.prototype.init = function (stream, emitter, callback) {
this._streams.squeeze.on('data', this._report.bind(this));
stream.pipe(this._streams.squeeze);
callback();
};


GoodLoggly.timeString = function (timestamp) {
return new Date(timestamp).toISOString();
};
Expand All @@ -49,21 +52,3 @@ GoodLoggly.timeString = function (timestamp) {
GoodLoggly.getMessage = function (event) {
return event.data ? event.data.message || event.data.error || event.data : '';
};


GoodLoggly.prototype._report = function (report) {
var data = Merge({}, report);

// Map hapi event data fields to Loggly fields
if (this._config.name) {
data.name = this._config.name;
}

if (this._config.hostname) {
data.hostname = this._config.hostname;
}

data.timestamp = GoodLoggly.timeString(data.timestamp);
data.msg = GoodLoggly.getMessage(data);
this._client.log(data, data.tags);
};
15 changes: 7 additions & 8 deletions package.json
Expand Up @@ -16,20 +16,19 @@
"test": "belly-button && lab -v -t 100 -a code"
},
"dependencies": {
"good-squeeze": "2.x.x",
"lodash.merge": "3.3.x",
"loggly": "1.0.x"
"lodash.merge": "4.4.x",
"loggly": "1.1.x"
},
"peerDependencies": {
"good": ">= 6.x.x"
"good": ">=7.0.0"
},
"devDependencies": {
"belly-button": "1.x.x",
"code": "1.x.x",
"lab": "6.x.x"
"belly-button": "2.x.x",
"code": "3.x.x",
"lab": "10.x.x"
},
"engines": {
"node": ">=0.10.x"
"node": ">=4.0.0"
},
"keywords": [
"good",
Expand Down

0 comments on commit 7a3c5fc

Please sign in to comment.