Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates winston-graylog2 to use [email protected] #67

Merged
merged 30 commits into from
Jan 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3eab79c
Updated packages to latest versions.
jeremy-j-ackso Dec 27, 2018
486a2f5
Corrected linting errors for `prefer-const`.
jeremy-j-ackso Dec 27, 2018
94fb8bb
Updated to use ES6 class and be winston3 compliant.
jeremy-j-ackso Dec 27, 2018
c4e83a6
Updated tests for ES6 & winston3 compliance.
jeremy-j-ackso Dec 27, 2018
3bceb8d
Updated tests to reflect change to `log()` usage.
jeremy-j-ackso Dec 27, 2018
40fa75b
prelog and processMeta should be handled by formatters at the winston…
jeremy-j-ackso Dec 27, 2018
4c862fd
Added log levels as functions.
jeremy-j-ackso Dec 27, 2018
9ed1a88
Correct the log signature to match Winston 3.
jeremy-j-ackso Dec 27, 2018
71b4f78
Re-added staticMeta option and updated eslint options.
jeremy-j-ackso Dec 28, 2018
1269e17
Corrected staticMeta.
jeremy-j-ackso Dec 28, 2018
1fa40b1
Updated readme for new usage.
jeremy-j-ackso Dec 28, 2018
e88539d
Changed the `graylog2` property to `graylog2Client`.
jeremy-j-ackso Jan 4, 2019
01f416f
Forgot to pull before making other changes, caused a conflict.
jeremy-j-ackso Jan 4, 2019
cc331d1
Change engine to require Node >= 8.0.0
jeremy-j-ackso Jan 4, 2019
e9e7f17
Changed access pattern of `options`, allowing removal of lodash!!!
jeremy-j-ackso Jan 4, 2019
09403c3
Added format instructions and metadata handling.
jeremy-j-ackso Jan 9, 2019
29b77ac
Flatten metadata.
jeremy-j-ackso Jan 9, 2019
fd2dd50
Update readme.md
jeremy-j-ackso Jan 9, 2019
b28824b
Update readme.md
jeremy-j-ackso Jan 9, 2019
b50d4b9
Update .travis.yml
jeremy-j-ackso Jan 9, 2019
139253b
Update .travis.yml
jeremy-j-ackso Jan 9, 2019
5b93d25
Updated to require a minimum of Node 8.6.
jeremy-j-ackso Jan 9, 2019
6ee4ade
Update readme.md
jeremy-j-ackso Jan 10, 2019
0983497
Updated for Node 6.4.0 support.
jeremy-j-ackso Jan 18, 2019
bc07d3a
Updated package versions again, and updated package.json for new way …
jeremy-j-ackso Jan 18, 2019
78a6f28
Updated readme example to include passing metadata as a separate object.
jeremy-j-ackso Jan 18, 2019
aaacee4
Roll back Node version in travis.
jeremy-j-ackso Jan 18, 2019
9a35a70
Added end-to-end test with mocked udp listener.
jeremy-j-ackso Jan 19, 2019
d12f290
Update readme.md
jeremy-j-ackso Jan 22, 2019
6b54b34
Correcting TypeError that occurs when metadata and staticMeta are nul…
jeremy-j-ackso Jan 24, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
},
"rules": {
"quote-props": ["error", "as-needed"],
"new-cap": ["error", {"properties": false}], // TODO: remove once newcap errors are fixed.
"quotes": ["error", "single", { "avoidEscape": true }],
"max-len": [
"error",
Expand All @@ -30,10 +29,7 @@
]
},
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module",
"ecmaFeatures": {
"experimentalObjectRestSpread": true
}
"ecmaVersion": 2018,
"sourceType": "module"
}
}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
.svn
npm-debug.log
npm-debug.log
*.swp
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ language: node_js
sudo: false

node_js:
- 6.10
- 6.4.0

install:
- npm install --dev
- npm install

script:
- yarn lint
Expand Down
141 changes: 59 additions & 82 deletions lib/winston-graylog2.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
'use strict';

const util = require('util');
const winston = require('winston-transport');
const graylog2 = require('graylog2');
const _ = require('lodash');
const Transport = require('winston-transport');
const {graylog: Graylog2Client} = require('graylog2');

/**
* Remapping winston level on graylog
*
* @param {String} winstonLevel
* @return {String}
*/
let getMessageLevel = (function() {
let levels = {
const getMessageLevel = (function() {
const levels = {
emerg: 'emergency',
alert: 'alert',
crit: 'critical',
Expand All @@ -28,86 +26,65 @@ let getMessageLevel = (function() {
};
})();

/**
* Preparing metadata for graylog
* If we send a javascript `Error` object
* to gray log we'll end up with the
* `[object Object]` string.
* To have our infos we need to get the stack out of the error.
* Here we remap metadata to handle this kind of situation
*
* @param {Object} meta
* @param {Object} staticMeta
* @return {Object}
*/
function prepareMeta(meta, staticMeta) {
meta = meta || {};

if (meta instanceof Error) {
meta = {error: meta.stack};
} else if (typeof meta === 'object') {
meta = _.mapValues(meta, function(value) {
if (value instanceof Error) {
return value.stack;
}

return value;
/** Class representing the Graylog2 Winston Transport */
class Graylog2 extends Transport {
/**
* Create the transport
* @param {Object} options - The options for configuring the transport.
*/
constructor(options) {
super(options);

options = options || {};
this.graylog = options.graylog;
if (!this.graylog) {
this.graylog = {
servers: [
{
host: 'localhost',
port: 12201,
},
],
};
}

this.name = options.name || 'graylog2';
this.exceptionsLevel = options.exceptionsLevel || 'not-default';

this.silent = options.silent || false;
this.handleExceptions = options.handleExceptions || false;

this.graylog2Client = new Graylog2Client(this.graylog);
this.staticMeta = options.staticMeta || {};

this.graylog2Client.on('error', function(error) {
console.error('Error while trying to write to graylog2:', error);
jeremy-j-ackso marked this conversation as resolved.
Show resolved Hide resolved
});
}

meta = _.merge(meta, staticMeta);

return meta;
}

let Graylog2 = (winston.Graylog2 = function(options) {
winston.call(this, options);

options = options || {};
this.graylog = _.get(options, 'graylog');
if (!this.graylog) {
this.graylog = {
servers: [
{
host: 'localhost',
port: 12201,
},
],
};
/**
* Log a message to Graylog2.
*
* @param {Object} info - An object containing the `message` and `info`.
* @param {function} callback - Winston's callback to itself.
*/
log(info, callback) {
const {message, level, metadata} = info;
const meta = Object.assign({}, metadata, this.staticMeta);

// prettier-ignore
setImmediate(() => {
this.graylog2Client[getMessageLevel(level)](message.substring(0, 100), message, meta);
});
callback();
}

this.name = options.name || 'graylog2';
this.exceptionsLevel = options.exceptionsLevel || 'not-default';

this.silent = options.silent || false;
this.handleExceptions = options.handleExceptions || false;
this.prelog =
typeof options.prelog === 'function' ? options.prelog : _.identity;
this.processMeta =
typeof options.processMeta === 'function'
? options.processMeta
: _.identity;
this.staticMeta = options.staticMeta || {};

this.graylog2 = new graylog2.graylog(this.graylog); // TODO: Fix in relation to https://eslint.org/docs/rules/new-cap

this.graylog2.on('error', function(error) {
console.error('Error while trying to write to graylog2:', error);
});
});

util.inherits(Graylog2, winston);

Graylog2.prototype.log = function(level, msg, meta, callback) {
meta = this.processMeta(prepareMeta(meta, this.staticMeta));
msg = this.prelog(msg);

this.graylog2[getMessageLevel(level)](msg.substring(0, 100), msg, meta);
callback(null, true);
};

Graylog2.prototype.close = function() {
this.graylog2.close();
};
/**
* Closes the Graylog2 Winston Transport.
*/
close() {
this.graylog2Client.close();
}
}

module.exports = Graylog2;
Loading