Skip to content

Commit

Permalink
[logging] Support configuration reloads for logging. Fixes #4407.
Browse files Browse the repository at this point in the history
  • Loading branch information
bevacqua committed Apr 1, 2016
1 parent d28cb32 commit b7dbfb9
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 64 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@
"d3": "3.5.6",
"elasticsearch": "10.1.2",
"elasticsearch-browser": "10.1.2",
"even-better": "7.0.1",
"expiry-js": "0.1.7",
"exports-loader": "0.6.2",
"expose-loader": "0.7.0",
"extract-text-webpack-plugin": "0.8.2",
"file-loader": "0.8.4",
"font-awesome": "4.4.0",
"glob-all": "3.0.1",
"good": "6.3.0",
"good-squeeze": "2.1.0",
"gridster": "0.5.6",
"hapi": "8.8.1",
Expand Down
17 changes: 12 additions & 5 deletions src/cli/serve/serve.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import _ from 'lodash';
const { isWorker } = require('cluster');
const { resolve } = require('path');
import { isWorker } from 'cluster';
import { resolve } from 'path';
import { fromRoot } from '../../utils';

const cwd = process.cwd();
import { fromRoot } from '../../utils';

let canCluster;
try {
Expand All @@ -24,7 +24,7 @@ const pathCollector = function () {
const pluginDirCollector = pathCollector();
const pluginPathCollector = pathCollector();

function initServerSettings(opts, extraCliOptions) {
function readServerSettings(opts, extraCliOptions) {
const readYamlConfig = require('./read_yaml_config');
const settings = readYamlConfig(opts.config);
const set = _.partial(_.set, settings);
Expand Down Expand Up @@ -116,7 +116,8 @@ module.exports = function (program) {

command
.action(async function (opts) {
const settings = initServerSettings(opts, this.getUnknownOptions());
const getCurrentSettings = () => readServerSettings(opts, this.getUnknownOptions());
const settings = getCurrentSettings();

if (canCluster && opts.dev && !isWorker) {
// stop processing the action and handoff to cluster manager
Expand All @@ -141,6 +142,12 @@ module.exports = function (program) {
process.exit(1); // eslint-disable-line no-process-exit
}

process.on('SIGHUP', function reloadConfig() {
const settings = getCurrentSettings();
kbnServer.server.log(['info', 'config'], 'Reloading logging configuration due to SIGHUP.');
kbnServer.applyLoggingConfiguration(settings);
});

return kbnServer;
});
};
14 changes: 14 additions & 0 deletions src/server/kbn_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { constant, once, compact, flatten } from 'lodash';
import { promisify, resolve, fromNode } from 'bluebird';
import { isWorker } from 'cluster';
import { fromRoot, pkg } from '../utils';
import Config from './config/config';
import loggingConfiguration from './logging/configuration';

let rootDir = fromRoot('.');

Expand Down Expand Up @@ -104,4 +106,16 @@ module.exports = class KbnServer {
}
});
}

applyLoggingConfiguration(settings) {
const config = Config.withDefaultSchema(settings);
const loggingOptions = loggingConfiguration(config);
const subset = {
ops: config.get('ops'),
logging: config.get('logging')
};
const plain = JSON.stringify(subset, null, 2);
this.server.log(['info', 'config'], 'New logging configuration:\n' + plain);
this.server.plugins['even-better'].monitor.reconfigure(loggingOptions);
}
};
61 changes: 61 additions & 0 deletions src/server/logging/configuration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import _ from 'lodash';
import logReporter from './log_reporter';

export default function loggingConfiguration(config) {
let events = config.get('logging.events');

if (config.get('logging.silent')) {
_.defaults(events, {});
}
else if (config.get('logging.quiet')) {
_.defaults(events, {
log: ['listening', 'error', 'fatal'],
request: ['error'],
error: '*'
});
}
else if (config.get('logging.verbose')) {
_.defaults(events, {
log: '*',
ops: '*',
request: '*',
response: '*',
error: '*'
});
}
else {
_.defaults(events, {
log: ['info', 'warning', 'error', 'fatal'],
response: config.get('logging.json') ? '*' : '!',
request: ['info', 'warning', 'error', 'fatal'],
error: '*'
});
}

const options = {
opsInterval: config.get('ops.interval'),
requestHeaders: true,
requestPayload: true,
reporters: [
{
reporter: logReporter,
config: {
json: config.get('logging.json'),
dest: config.get('logging.dest'),
// I'm adding the default here because if you add another filter
// using the commandline it will remove authorization. I want users
// to have to explicitly set --logging.filter.authorization=none to
// have it show up int he logs.
filter: _.defaults(config.get('logging.filter'), {
authorization: 'remove'
})
},
events: _.transform(events, function (filtered, val, key) {
// provide a string compatible way to remove events
if (val !== '!') filtered[key] = val;
}, {})
}
]
};
return options;
}
63 changes: 5 additions & 58 deletions src/server/logging/index.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,15 @@
import _ from 'lodash';
import { fromNode } from 'bluebird';
import evenBetter from 'even-better';
import loggingConfiguration from './configuration';

module.exports = function (kbnServer, server, config) {
export default function (kbnServer, server, config) {
// prevent relying on kbnServer so this can be used with other hapi servers
kbnServer = null;

return fromNode(function (cb) {
let events = config.get('logging.events');

if (config.get('logging.silent')) {
_.defaults(events, {});
}
else if (config.get('logging.quiet')) {
_.defaults(events, {
log: ['listening', 'error', 'fatal'],
request: ['error'],
error: '*'
});
}
else if (config.get('logging.verbose')) {
_.defaults(events, {
log: '*',
ops: '*',
request: '*',
response: '*',
error: '*'
});
}
else {
_.defaults(events, {
log: ['info', 'warning', 'error', 'fatal'],
response: config.get('logging.json') ? '*' : '!',
request: ['info', 'warning', 'error', 'fatal'],
error: '*'
});
}

server.register({
register: require('good'),
options: {
opsInterval: config.get('ops.interval'),
requestHeaders: true,
requestPayload: true,
reporters: [
{
reporter: require('./log_reporter'),
config: {
json: config.get('logging.json'),
dest: config.get('logging.dest'),
// I'm adding the default here because if you add another filter
// using the commandline it will remove authorization. I want users
// to have to explicitly set --logging.filter.authorization=none to
// have it show up int he logs.
filter: _.defaults(config.get('logging.filter'), {
authorization: 'remove'
})
},
events: _.transform(events, function (filtered, val, key) {
// provide a string compatible way to remove events
if (val !== '!') filtered[key] = val;
}, {})
}
]
}
register: evenBetter,
options: loggingConfiguration(config)
}, cb);
});
};
1 change: 1 addition & 0 deletions src/server/logging/log_format_string.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ let typeColors = {
req: 'green',
res: 'green',
ops: 'cyan',
config: 'cyan',
err: 'red',
info: 'green',
error: 'red',
Expand Down

0 comments on commit b7dbfb9

Please sign in to comment.