Skip to content
Merged
Changes from 1 commit
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
29 changes: 19 additions & 10 deletions packages/kbn-legacy-logging/src/log_reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
* under the License.
*/

import { createWriteStream } from 'fs';
import { pipeline } from 'stream';

// @ts-expect-error missing type def
import { Squeeze } from '@hapi/good-squeeze';
import { createWriteStream as writeStr, WriteStream } from 'fs';

import { KbnLoggerJsonFormat } from './log_format_json';
import { KbnLoggerStringFormat } from './log_format_string';
Expand All @@ -31,21 +33,28 @@ export function getLogReporter({ events, config }: { events: any; config: LogFor
const format = config.json ? new KbnLoggerJsonFormat(config) : new KbnLoggerStringFormat(config);
const logInterceptor = new LogInterceptor();

let dest: WriteStream | NodeJS.WritableStream;
if (config.dest === 'stdout') {
dest = process.stdout;
pipeline(logInterceptor, squeeze, format, onFinished);
// The `pipeline` function is used to properly close all streams in the
// pipeline in case one of them ends or fails. Since stdout obviously
// shouldn't be closed in case of a failure in one of the other streams,
// we're not including that in the call to `pipeline`, but rely on the old
// `pipe` function instead.
format.pipe(process.stdout);
Comment thread
watson marked this conversation as resolved.
} else {
dest = writeStr(config.dest, {
const dest = createWriteStream(config.dest, {
flags: 'a',
encoding: 'utf8',
});

logInterceptor.on('end', () => {
dest.end();
});
pipeline(logInterceptor, squeeze, format, dest, onFinished);
}

logInterceptor.pipe(squeeze).pipe(format).pipe(dest);

return logInterceptor;
}

function onFinished(err: NodeJS.ErrnoException | null) {
if (err) {
// eslint-disable-next-line no-console
console.error('An unexpected error occurred in the logging pipeline:', err.stack);
}
}