mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 01:38:56 -04:00
[pack installer] reworked Logger into a constructable object
This commit is contained in:
parent
fa1fd274b4
commit
6830296e2e
2 changed files with 44 additions and 43 deletions
|
@ -1,6 +1,6 @@
|
|||
import expect from 'expect.js';
|
||||
import sinon from 'sinon';
|
||||
import pluginLogger from '../plugin_logger';
|
||||
import Logger from '../logger';
|
||||
|
||||
describe('kibana cli', function () {
|
||||
|
||||
|
@ -20,7 +20,7 @@ describe('kibana cli', function () {
|
|||
});
|
||||
|
||||
it('should log messages to the console and append a new line', function () {
|
||||
logger = pluginLogger({ silent: false, quiet: false });
|
||||
logger = new Logger({ silent: false, quiet: false });
|
||||
const message = 'this is my message';
|
||||
|
||||
logger.log(message);
|
||||
|
@ -31,7 +31,7 @@ describe('kibana cli', function () {
|
|||
});
|
||||
|
||||
it('should log messages to the console and append not append a new line', function () {
|
||||
logger = pluginLogger({ silent: false, quiet: false });
|
||||
logger = new Logger({ silent: false, quiet: false });
|
||||
for (let i = 0; i < 10; i++) {
|
||||
logger.log('.', true);
|
||||
}
|
||||
|
@ -54,7 +54,7 @@ describe('kibana cli', function () {
|
|||
});
|
||||
|
||||
it('should not log any messages when quiet is set', function () {
|
||||
logger = pluginLogger({ silent: false, quiet: true });
|
||||
logger = new Logger({ silent: false, quiet: true });
|
||||
|
||||
const message = 'this is my message';
|
||||
logger.log(message);
|
||||
|
@ -68,7 +68,7 @@ describe('kibana cli', function () {
|
|||
});
|
||||
|
||||
it('should not log any messages when silent is set', function () {
|
||||
logger = pluginLogger({ silent: true, quiet: false });
|
||||
logger = new Logger({ silent: true, quiet: false });
|
||||
|
||||
const message = 'this is my message';
|
||||
logger.log(message);
|
||||
|
@ -94,7 +94,7 @@ describe('kibana cli', function () {
|
|||
});
|
||||
|
||||
it('should log error messages to the console and append a new line', function () {
|
||||
logger = pluginLogger({ silent: false, quiet: false });
|
||||
logger = new Logger({ silent: false, quiet: false });
|
||||
const message = 'this is my error';
|
||||
|
||||
logger.error(message);
|
||||
|
@ -102,7 +102,7 @@ describe('kibana cli', function () {
|
|||
});
|
||||
|
||||
it('should log error messages to the console when quiet is set', function () {
|
||||
logger = pluginLogger({ silent: false, quiet: true });
|
||||
logger = new Logger({ silent: false, quiet: true });
|
||||
const message = 'this is my error';
|
||||
|
||||
logger.error(message);
|
||||
|
@ -110,7 +110,7 @@ describe('kibana cli', function () {
|
|||
});
|
||||
|
||||
it('should not log any error messages when silent is set', function () {
|
||||
logger = pluginLogger({ silent: true, quiet: false });
|
||||
logger = new Logger({ silent: true, quiet: false });
|
||||
const message = 'this is my error';
|
||||
|
||||
logger.error(message);
|
||||
|
|
|
@ -1,44 +1,45 @@
|
|||
export default function createPluginLogger(settings) {
|
||||
let previousLineEnded = true;
|
||||
const silent = !!settings.silent;
|
||||
const quiet = !!settings.quiet;
|
||||
export default function Logger(settings) {
|
||||
const self = this;
|
||||
|
||||
function log(data, sameLine) {
|
||||
if (silent || quiet) return;
|
||||
self.previousLineEnded = true;
|
||||
self.silent = !!settings.silent;
|
||||
self.quiet = !!settings.quiet;
|
||||
}
|
||||
|
||||
if (!sameLine && !previousLineEnded) {
|
||||
process.stdout.write('\n');
|
||||
}
|
||||
Logger.prototype.log = function (data, sameLine) {
|
||||
const self = this;
|
||||
|
||||
//if data is a stream, pipe it.
|
||||
if (data.readable) {
|
||||
data.pipe(process.stdout);
|
||||
return;
|
||||
}
|
||||
if (self.silent || self.quiet) return;
|
||||
|
||||
process.stdout.write(data);
|
||||
if (!sameLine) process.stdout.write('\n');
|
||||
previousLineEnded = !sameLine;
|
||||
if (!sameLine && !self.previousLineEnded) {
|
||||
process.stdout.write('\n');
|
||||
}
|
||||
|
||||
function error(data) {
|
||||
if (silent) return;
|
||||
|
||||
if (!previousLineEnded) {
|
||||
process.stderr.write('\n');
|
||||
}
|
||||
|
||||
//if data is a stream, pipe it.
|
||||
if (data.readable) {
|
||||
data.pipe(process.stderr);
|
||||
return;
|
||||
}
|
||||
process.stderr.write(`${data}\n`);
|
||||
previousLineEnded = true;
|
||||
//if data is a stream, pipe it.
|
||||
if (data.readable) {
|
||||
data.pipe(process.stdout);
|
||||
return;
|
||||
}
|
||||
|
||||
return {
|
||||
log: log,
|
||||
error: error
|
||||
};
|
||||
process.stdout.write(data);
|
||||
if (!sameLine) process.stdout.write('\n');
|
||||
self.previousLineEnded = !sameLine;
|
||||
};
|
||||
|
||||
Logger.prototype.error = function (data) {
|
||||
const self = this;
|
||||
|
||||
if (self.silent) return;
|
||||
|
||||
if (!self.previousLineEnded) {
|
||||
process.stderr.write('\n');
|
||||
}
|
||||
|
||||
//if data is a stream, pipe it.
|
||||
if (data.readable) {
|
||||
data.pipe(process.stderr);
|
||||
return;
|
||||
}
|
||||
process.stderr.write(`${data}\n`);
|
||||
self.previousLineEnded = true;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue