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

Adds support for log rotation #49750

Merged
merged 100 commits into from
Dec 10, 2019
Merged
Show file tree
Hide file tree
Changes from 94 commits
Commits
Show all changes
100 commits
Select commit Hold shift + click to select a range
17555a3
chore(NA): add log options to config yml
mistic Oct 7, 2019
a5a06d0
chore(NA): remove unwanted option from config declaration
mistic Oct 10, 2019
3f56e58
chore(NA): add the bootstrap for the logging rotate feature
mistic Oct 11, 2019
cc0d404
Merge remote-tracking branch 'upstream/master' into log-rotation
mistic Oct 15, 2019
a87c080
feat(NA): base interface setup for log rotation feature
mistic Oct 16, 2019
7c739d4
docs(NA): add documentation for the new logging rotate options. chore…
mistic Oct 16, 2019
057f997
chore(NA): base lifecycle methods and logic
mistic Oct 16, 2019
ecd5436
feat(NA): monitor logic for log rotate feature
mistic Oct 17, 2019
0a0dd4a
fix(NA): basic log rotation lifecycle
mistic Oct 17, 2019
75d2ffa
chore(NA): fix typo on config file
mistic Oct 17, 2019
29779b9
feat(NA): add rotate files feature to log rotator
mistic Oct 18, 2019
73cc678
chore(NA): fix log rotate config
mistic Oct 19, 2019
59e7cca
chore(NA): some tests to try logging rotate lifecycle
mistic Oct 20, 2019
e41335b
feat(NA): correct log rotation logic
mistic Oct 22, 2019
08285f5
fix(NA): lifecycle for the log rotator
mistic Oct 26, 2019
efb64aa
test(NA): add a test case
mistic Oct 30, 2019
6c6010f
Merge remote-tracking branch 'upstream/master' into log-rotation
mistic Oct 30, 2019
5b8b46a
chore(NA): correctly add the new defaults to the config schema
mistic Oct 30, 2019
83bb1e3
Merge branch 'master' into log-rotation
mistic Oct 30, 2019
2d882ba
test(NA): change dir generation for test
mistic Oct 30, 2019
126b7c7
chore(NA): mock log rotate for logging service test
mistic Oct 30, 2019
0f7f47d
test(NA): fix temp dir permission issue
mistic Oct 30, 2019
bf72823
test(NA): try to fix test
mistic Oct 30, 2019
eaa381f
chore(NA): merge and solve conflicts with master
mistic Oct 30, 2019
22d4115
chore(NA): remove usage of mkdtemp
mistic Oct 31, 2019
ad670d1
Merge branch 'master' into log-rotation
mistic Oct 31, 2019
3ce125d
refact(NA): feature logging rotation reimplementation in order to mak…
mistic Nov 2, 2019
f39a80c
Merge remote-tracking branch 'upstream/master' into log-rotation
mistic Nov 2, 2019
de896b9
fix(NA): bug on file size monitor handle
mistic Nov 4, 2019
11917b7
fix(NA): remove wrong commented out code
mistic Nov 4, 2019
45017fd
Merge branch 'master' into log-rotation
mistic Nov 4, 2019
4cc2328
chore(NA): correctly identify if we should use polling
mistic Nov 6, 2019
b7a7bc1
chore(NA): fix some code comment
mistic Nov 6, 2019
d6173ad
refact(NA): minor implementation details
mistic Nov 7, 2019
8c8e018
chore(NA): change the order of logging mix
mistic Nov 7, 2019
8b26b75
test(NA): add some more test cases
mistic Nov 7, 2019
102d733
Merge remote-tracking branch 'upstream/master' into log-rotation
mistic Nov 7, 2019
c1b5b70
test(NA): add the majority of the test cases
mistic Nov 10, 2019
4307f03
Merge branch 'master' into log-rotation
mistic Nov 10, 2019
c91cc76
test(NA): add last test case
mistic Nov 10, 2019
8a969c4
test(NA): fallback conditions
mistic Nov 11, 2019
d09367e
chore(NA): add logging rotate config keys to the docker image
mistic Nov 12, 2019
dceb687
Merge branch 'master' into log-rotation
mistic Nov 13, 2019
d424362
Merge branch 'master' into log-rotation
mistic Nov 15, 2019
fb03cb2
chore(NA): move logging.rotate.enable setting to enabled
mistic Nov 15, 2019
118b09e
chore(NA): clarify documentation for logging rotate
mistic Nov 15, 2019
6f2510e
chore(NA): use regular instead of logWithMetadata
mistic Nov 15, 2019
2a27e85
chore(NA): move chokidar to a prod dep
mistic Nov 15, 2019
e0d67ec
chore(NA): add log explaining why we had fallback to use polling
mistic Nov 15, 2019
5e7a489
test(NA): fix unit tests
mistic Nov 15, 2019
f3db855
Merge remote-tracking branch 'upstream/master' into log-rotation
mistic Nov 15, 2019
e3df575
test(NA): fix unit tests
mistic Nov 16, 2019
8345e09
chore(NA): correctly place this.running condition
mistic Nov 18, 2019
44be9e5
Merge remote-tracking branch 'upstream/master' into log-rotation
mistic Nov 18, 2019
7f53219
chore(NA): remove redundant call
mistic Nov 18, 2019
3eda24b
fix(NA): log filename containing numbers would produce invalid sorting
mistic Nov 19, 2019
ad9fcb7
Merge remote-tracking branch 'upstream/master' into log-rotation
mistic Nov 19, 2019
eff8041
Merge branch 'master' into log-rotation
elasticmachine Nov 19, 2019
c1e3c4c
Merge branch 'master' into log-rotation
elasticmachine Nov 19, 2019
9249864
chore(NA): remove existsSync function call from readRotatedFilesMetad…
mistic Nov 20, 2019
c173c05
Merge branch 'log-rotation' of github.com:mistic/kibana into log-rota…
mistic Nov 20, 2019
b65d441
Merge branch 'master' into log-rotation
mistic Nov 20, 2019
08effb5
Merge branch 'master' into log-rotation
elasticmachine Nov 21, 2019
a5c3119
Merge branch 'master' into log-rotation
elasticmachine Nov 25, 2019
8acea0e
chore(NA): Update docs/setup/settings.asciidoc
mistic Nov 26, 2019
1cf9e06
chore(NA): Update docs/setup/settings.asciidoc
mistic Nov 26, 2019
83fb84d
chore(NA): Update docs/setup/settings.asciidoc
mistic Nov 26, 2019
5ac11f4
chore(NA): Update docs/setup/settings.asciidoc
mistic Nov 26, 2019
6398a39
chore(na): update src/legacy/server/logging/rotate/index.js
mistic Dec 2, 2019
9568e6f
Merge branch 'master' into log-rotation
elasticmachine Dec 2, 2019
7383722
chore(NA): remove unused config line from docker vars
mistic Dec 2, 2019
938052b
chore(NA): update documentation to include info about non exact limits
mistic Dec 2, 2019
7db2d59
chore(NA): remove redudant if clause
mistic Dec 2, 2019
35627b0
fix(NA): correctly work with new keepFiles limit after start
mistic Dec 2, 2019
5314b87
fix(NA): warning log for logging rotate
mistic Dec 2, 2019
32cb7e1
chore(NA): replace logwithmetadate with log
mistic Dec 3, 2019
844200d
docs(NA): correct log to right terms
mistic Dec 3, 2019
41605c3
docs(NA): add comment about usage of slice(-1)
mistic Dec 3, 2019
ecdf036
refact(NA): changing polling interval from seconds to milliseconds
mistic Dec 3, 2019
9220525
docs(NA): fix comments for shouldRotate method
mistic Dec 3, 2019
76f8a9d
chore(NA): update src/legacy/server/logging/rotate/log_rotator.js
mistic Dec 4, 2019
b7b0298
chore(NA): update src/legacy/server/logging/rotate/log_rotator.js
mistic Dec 4, 2019
a46d784
refact(NA): small change
mistic Dec 4, 2019
e99541b
refact(NA): bound stop
mistic Dec 4, 2019
8bb24c4
refact(NA): shouldUsePolling test function
mistic Dec 4, 2019
9b7fe0d
Merge branch 'master' into log-rotation
mistic Dec 4, 2019
900851c
refact(NA): move named truncate function to delete
mistic Dec 4, 2019
b21b9ea
Merge branch 'master' into log-rotation
elasticmachine Dec 4, 2019
11e2683
refact(NA): typescript conversion
mistic Dec 5, 2019
8ecfaac
Merge branch 'log-rotation' of github.com:mistic/kibana into log-rota…
mistic Dec 5, 2019
f07a968
Merge remote-tracking branch 'upstream/master' into log-rotation
mistic Dec 5, 2019
c01f8a4
chore(NA): type update for log rotation index file
mistic Dec 5, 2019
579d21e
docs(NA): add experimental tag on docs
mistic Dec 5, 2019
94c625f
chore(NA): add call protection of clearTimeout
mistic Dec 5, 2019
0c7c779
refact(NA): cleanup comments and wrong added logs plus inline config
mistic Dec 7, 2019
6111766
Merge remote-tracking branch 'upstream/master' into log-rotation
mistic Dec 7, 2019
c789425
chore(NA): replace ts-ignore by non null assertion operator
mistic Dec 7, 2019
1256e3d
docs(NA): extend documentation for _renameRotatedFilesByOne call
mistic Dec 7, 2019
67041cb
chore(NA): fix type problems for process.emit on nodejs
mistic Dec 7, 2019
b8a3d4f
Merge branch 'master' into log-rotation
elasticmachine Dec 9, 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
30 changes: 30 additions & 0 deletions docs/setup/settings.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,36 @@ will default to `true`.
`logging.quiet:`:: *Default: false* Set the value of this setting to `true` to
suppress all logging output other than error messages.

`logging.rotate:`:: [experimental] Specifies the options for the logging rotate feature.
When not defined, all the sub options defaults would be applied.
The following example shows a valid logging rotate configuration:
+
--
logging.rotate:
enabled: true
everyBytes: 10485760
keepFiles: 10
--

`logging.rotate.enabled:`:: [experimental] *Default: false* Set the value of this setting to `true` to
enable log rotation. If you do not have a `logging.dest` set that is different from `stdout`
that feature would not take any effect.

`logging.rotate.everyBytes:`:: [experimental] *Default: 10485760* The maximum size of a log file (that is `not an exact` limit). After the
limit is reached, a new log file is generated. The default size limit is 10485760 (10 MB) and
this option should be at least greater than 1024.

`logging.rotate.keepFiles:`:: [experimental] *Default: 7* The number of most recent rotated log files to keep
on disk. Older files are deleted during log rotation. The default value is 7. The `logging.rotate.keepFiles`
option has to be in the range of 2 to 1024 files.

`logging.rotate.pollingInterval:`:: [experimental] *Default: 10000* The number of milliseconds for the polling strategy in case
the `logging.rotate.usePolling` is enabled. That option has to be in the range of 5000 to 3600000 milliseconds.

`logging.rotate.usePolling:`:: [experimental] *Default: false* By default we try to understand the best way to monitoring
the log file. However, there is some systems where it could not be always accurate. In those cases, if needed,
the `polling` method could be used enabling that option.

`logging.silent:`:: *Default: false* Set the value of this setting to `true` to
suppress all logging output.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
"cache-loader": "^4.1.0",
"chalk": "^2.4.2",
"check-disk-space": "^2.1.0",
"chokidar": "3.2.1",
"color": "1.0.3",
"commander": "3.0.0",
"compare-versions": "3.5.1",
Expand Down Expand Up @@ -370,7 +371,6 @@
"chai": "3.5.0",
"chance": "1.0.18",
"cheerio": "0.22.0",
"chokidar": "3.2.1",
"chromedriver": "78.0.1",
"classnames": "2.2.6",
"dedent": "^0.7.0",
Expand Down
12 changes: 12 additions & 0 deletions src/cli/cluster/cluster_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ export default class ClusterManager {
});
});

// When receive that event from server worker
// forward a reloadLoggingConfig message to master
// and all workers. This is only used by LogRotator service
// when the cluster mode is enabled
this.server.on('reloadLoggingConfigFromServerWorker', () => {
process.emit('message', { reloadLoggingConfig: true });

this.workers.forEach(worker => {
worker.fork.send({ reloadLoggingConfig: true });
});
});

bindAll(this, 'onWatcherAdd', 'onWatcherError', 'onWatcherChange');

if (opts.open) {
Expand Down
3 changes: 3 additions & 0 deletions src/cli/cluster/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ export default class Worker extends EventEmitter {
this.listening = true;
this.emit('listening');
break;
case 'RELOAD_LOGGING_CONFIG_FROM_SERVER_WORKER':
this.emit('reloadLoggingConfigFromServerWorker');
break;
}
}

Expand Down
17 changes: 15 additions & 2 deletions src/core/server/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,20 @@ export async function bootstrap({

const root = new Root(rawConfigService.getConfig$(), env, onRootShutdown);

process.on('SIGHUP', () => {
process.on('SIGHUP', () => reloadLoggingConfig());

// This is only used by the LogRotator service
// in order to be able to reload the log configuration
// under the cluster mode
process.on('message', msg => {
if (!msg || msg.reloadLoggingConfig !== true) {
return;
}

reloadLoggingConfig();
});

function reloadLoggingConfig() {
const cliLogger = root.logger.get('cli');
cliLogger.info('Reloading logging configuration due to SIGHUP.', { tags: ['config'] });

Expand All @@ -82,7 +95,7 @@ export async function bootstrap({
}

cliLogger.info('Reloaded logging configuration due to SIGHUP.', { tags: ['config'] });
});
}

process.on('SIGINT', () => shutdown());
process.on('SIGTERM', () => shutdown());
Expand Down
4 changes: 4 additions & 0 deletions src/core/server/logging/logging_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ jest.mock('fs', () => ({
createWriteStream: jest.fn(() => ({ write: mockStreamWrite })),
}));

jest.mock('../../../legacy/server/logging/rotate', () => ({
setupLoggingRotate: jest.fn().mockImplementation(() => Promise.resolve({})),
}));

const timestamp = new Date(Date.UTC(2012, 1, 1));
let mockConsoleLog: jest.SpyInstance;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ kibana_vars=(
kibana.index
logging.dest
logging.quiet
logging.rotate.enabled
logging.rotate.everyBytes
logging.rotate.keepFiles
logging.rotate.pollingInterval
logging.rotate.usePolling
logging.silent
logging.useUTC
logging.verbose
Expand Down
9 changes: 8 additions & 1 deletion src/legacy/server/config/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,14 @@ export default () => Joi.object({
then: Joi.default(!process.stdout.isTTY),
otherwise: Joi.default(true)
}),
timezone: Joi.string()
timezone: Joi.string(),
rotate: Joi.object().keys({
enabled: Joi.boolean().default(false),
everyBytes: Joi.number().greater(1024).default(10485760),
keepFiles: Joi.number().greater(2).less(1024).default(7),
pollingInterval: Joi.number().greater(5000).less(3600000).default(10000),
usePolling: Joi.boolean().default(false)
}).default()
}).default(),

ops: Joi.object({
Expand Down
5 changes: 4 additions & 1 deletion src/legacy/server/logging/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import good from '@elastic/good';
import loggingConfiguration from './configuration';
import { logWithMetadata } from './log_with_metadata';
import { setupLoggingRotate } from './rotate';

export async function setupLogging(server, config) {
return await server.register({
Expand All @@ -30,5 +31,7 @@ export async function setupLogging(server, config) {

export async function loggingMixin(kbnServer, server, config) {
logWithMetadata.decorateServer(server);
return await setupLogging(server, config);

await setupLogging(server, config);
await setupLoggingRotate(server, config);
}
64 changes: 64 additions & 0 deletions src/legacy/server/logging/rotate/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { isMaster, isWorker } from 'cluster';
import { Server } from 'hapi';
import { LogRotator } from './log_rotator';
import { KibanaConfig } from '../../kbn_server';

let logRotator: LogRotator;

export async function setupLoggingRotate(server: Server, config: KibanaConfig) {
// If log rotate is not enabled we skip
if (!config.get('logging.rotate.enabled')) {
return;
}

// We just want to start the logging rotate service once
// and we choose to use the master (prod) or the worker server (dev)
if (!isMaster && isWorker && process.env.kbnWorkerType !== 'server') {
return;
}

// We don't want to run logging rotate server if
// we are not logging to a file
if (config.get('logging.dest') === 'stdout') {
server.log(
['warning', 'logging:rotate'],
'Log rotation is enabled but logging.dest is configured for stdout. Set logging.dest to a file for this setting to take effect.'
);
return;
}

server.log(
['warning', 'logging:rotate'],
'Log rotation is enabled but logging.dest is configured for stdout. Set logging.dest to a file for this setting to take effect.'
);

// Enable Logging Rotate Service
// We need the master process and it can
// try to setupLoggingRotate more than once,
// so we'll need to assure it only loads once.
if (!logRotator) {
logRotator = new LogRotator(config, server);
await logRotator.start();
}

return logRotator;
}
Loading