Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
33 changes: 32 additions & 1 deletion lib/winston-graylog2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

/**
* Remapping winston level on graylog
Expand Down Expand Up @@ -62,6 +63,36 @@ class Graylog2 extends Transport {
});
}

/**
* 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}
*/
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;
});
}

meta = _.merge(meta, staticMeta);
return meta;
}

/**
* Log a message to Graylog2.
*
Expand All @@ -70,7 +101,7 @@ class Graylog2 extends Transport {
*/
log(info, callback) {
const {message, level, metadata} = info;
const meta = Object.assign({}, metadata, this.staticMeta);
const meta = this.prepareMeta(metadata, this.staticMeta);
const cleanedMessage = message || '';
const shortMessage = cleanedMessage.substring(0, 100);

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
],
"dependencies": {
"graylog2": "^0.2.1",
"lodash": "^4.17.11",
"winston": "^3.2.0",
"winston-transport": "^4.3.0"
},
Expand Down