mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 09:48:58 -04:00
Adding filtering to the logger
- Closes #5036 - Add `applyFilterToKey()` - Add test for `applyFilterToKey()` - Add `filter` attribute to config for reporters - Add `this.filter` method to `LogFormat` class
This commit is contained in:
parent
05628ab4cf
commit
21a6660103
5 changed files with 91 additions and 4 deletions
|
@ -6,6 +6,7 @@ let ansicolors = require('ansicolors');
|
|||
let stringify = require('json-stringify-safe');
|
||||
let querystring = require('querystring');
|
||||
let inspect = require('util').inspect;
|
||||
let applyFilterToKey = require('./applyFilterToKey');
|
||||
|
||||
function serializeError(err) {
|
||||
return {
|
||||
|
@ -24,16 +25,27 @@ let levelColor = function (code) {
|
|||
return ansicolors.red(code);
|
||||
};
|
||||
|
||||
|
||||
module.exports = class TransformObjStream extends Stream.Transform {
|
||||
constructor() {
|
||||
constructor(config) {
|
||||
super({
|
||||
readableObjectMode: false,
|
||||
writableObjectMode: true
|
||||
});
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
filter(data) {
|
||||
if (this.config.filter) {
|
||||
_.each(this.config.filter, (action, key) => {
|
||||
applyFilterToKey(data, key, action);
|
||||
});
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
_transform(event, enc, next) {
|
||||
var data = this.readEvent(event);
|
||||
var data = this.filter(this.readEvent(event));
|
||||
this.push(this.format(data) + '\n');
|
||||
next();
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ let LogFormatString = require('./LogFormatString');
|
|||
module.exports = class KbnLogger {
|
||||
constructor(events, config) {
|
||||
this.squeeze = new Squeeze(events);
|
||||
this.format = config.json ? new LogFormatJson() : new LogFormatString();
|
||||
this.format = config.json ? new LogFormatJson(config) : new LogFormatString(config);
|
||||
|
||||
if (config.dest === 'stdout') {
|
||||
this.dest = process.stdout;
|
||||
|
|
49
src/server/logging/__tests__/applyFilterToKey.js
Normal file
49
src/server/logging/__tests__/applyFilterToKey.js
Normal file
|
@ -0,0 +1,49 @@
|
|||
var applyFilterToKey = require('../applyFilterToKey');
|
||||
var expect = require('expect.js');
|
||||
|
||||
function fixture() {
|
||||
return {
|
||||
req: {
|
||||
headers: {
|
||||
authorization: 'Basic dskd939k2i'
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
describe('applyFilterToKey(obj, key, action)', function () {
|
||||
|
||||
it('should remove a key from an object recursivly', function () {
|
||||
var data = fixture();
|
||||
applyFilterToKey(data, 'authorization', 'remove');
|
||||
expect(data).to.eql({
|
||||
req: { headers: {} }
|
||||
});
|
||||
});
|
||||
|
||||
it('should censor a key in an object recursivly', function () {
|
||||
var data = fixture();
|
||||
applyFilterToKey(data, 'authorization', 'censor');
|
||||
expect(data).to.eql({
|
||||
req: {
|
||||
headers: {
|
||||
authorization: 'XXXXXXXXXXXXXXXX'
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should censor key with a RegEx in an object recursivly', function () {
|
||||
var data = fixture();
|
||||
var regex = /([^\s]+)$/;
|
||||
applyFilterToKey(data, 'authorization', regex);
|
||||
expect(data).to.eql({
|
||||
req: {
|
||||
headers: {
|
||||
authorization: 'Basic XXXXXXXXXX'
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
});
|
23
src/server/logging/applyFilterToKey.js
Normal file
23
src/server/logging/applyFilterToKey.js
Normal file
|
@ -0,0 +1,23 @@
|
|||
function replacer(match, group) {
|
||||
return (new Array(group.length + 1).join('X'));
|
||||
}
|
||||
|
||||
module.exports = function applyFilterToKey(obj, key, action) {
|
||||
for (let k in obj) {
|
||||
if (obj.hasOwnProperty(k)) {
|
||||
let val = obj[k];
|
||||
if (typeof val === 'object') {
|
||||
applyFilterToKey(val, key, action);
|
||||
} else if (k === key) {
|
||||
val = '' + val;
|
||||
if (action === 'remove') delete obj[k];
|
||||
if (action === 'censor') {
|
||||
obj[k] = val.replace(/./g, 'X');
|
||||
};
|
||||
if (action instanceof RegExp) {
|
||||
obj[k] = val.replace(action, replacer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
|
@ -42,7 +42,10 @@ module.exports = function (kbnServer, server, config) {
|
|||
reporter: require('./LogReporter'),
|
||||
config: {
|
||||
json: config.get('logging.json'),
|
||||
dest: config.get('logging.dest')
|
||||
dest: config.get('logging.dest'),
|
||||
filter: {
|
||||
authorization: 'remove'
|
||||
}
|
||||
},
|
||||
events: _.transform(events, function (filtered, val, key) {
|
||||
// provide a string compatible way to remove events
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue