Skip to content

Commit

Permalink
Create missing directories in file logging appenders (#117666)
Browse files Browse the repository at this point in the history
  • Loading branch information
watson authored Nov 16, 2021
1 parent 6742a2d commit 72d3728
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
Empty file removed logs/.empty
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ jest.mock('../../layouts/layouts', () => {
});

export const mockCreateWriteStream = jest.fn();
jest.mock('fs', () => ({ createWriteStream: mockCreateWriteStream }));
export const mockMkdirSync = jest.fn();
jest.mock('fs', () => ({ createWriteStream: mockCreateWriteStream, mkdirSync: mockMkdirSync }));
11 changes: 10 additions & 1 deletion src/core/server/logging/appenders/file/file_appender.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import { mockCreateWriteStream } from './file_appender.test.mocks';
import { mockCreateWriteStream, mockMkdirSync } from './file_appender.test.mocks';

import { LogRecord, LogLevel } from '@kbn/logging';
import { FileAppender } from './file_appender';
Expand All @@ -15,6 +15,7 @@ const tickMs = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))

beforeEach(() => {
mockCreateWriteStream.mockReset();
mockMkdirSync.mockReset();
});

test('`createConfigSchema()` creates correct schema.', () => {
Expand Down Expand Up @@ -49,8 +50,10 @@ test('file stream is created only once and only after first `append()` is called
});

const mockPath = 'mock://path/file.log';
const mockDir = 'mock://path';
const appender = new FileAppender({ format: () => '' }, mockPath);

expect(mockMkdirSync).not.toHaveBeenCalled();
expect(mockCreateWriteStream).not.toHaveBeenCalled();

appender.append({
Expand All @@ -61,12 +64,17 @@ test('file stream is created only once and only after first `append()` is called
pid: 5355,
});

expect(mockMkdirSync).toHaveBeenCalledTimes(1);
expect(mockMkdirSync).toHaveBeenCalledWith(mockDir, {
recursive: true,
});
expect(mockCreateWriteStream).toHaveBeenCalledTimes(1);
expect(mockCreateWriteStream).toHaveBeenCalledWith(mockPath, {
encoding: 'utf8',
flags: 'a',
});

mockMkdirSync.mockClear();
mockCreateWriteStream.mockClear();
appender.append({
context: 'context-2',
Expand All @@ -76,6 +84,7 @@ test('file stream is created only once and only after first `append()` is called
pid: 5355,
});

expect(mockMkdirSync).not.toHaveBeenCalled();
expect(mockCreateWriteStream).not.toHaveBeenCalled();
});

Expand Down
8 changes: 7 additions & 1 deletion src/core/server/logging/appenders/file/file_appender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

import { schema } from '@kbn/config-schema';
import { LogRecord, Layout, DisposableAppender } from '@kbn/logging';
import { createWriteStream, WriteStream } from 'fs';
import { createWriteStream, WriteStream, mkdirSync } from 'fs';
import { dirname } from 'path';

import { Layouts, LayoutConfigType } from '../../layouts/layouts';

Expand Down Expand Up @@ -47,6 +48,7 @@ export class FileAppender implements DisposableAppender {
*/
public append(record: LogRecord) {
if (this.outputStream === undefined) {
this.ensureDirectory(this.path);
this.outputStream = createWriteStream(this.path, {
encoding: 'utf8',
flags: 'a',
Expand All @@ -73,4 +75,8 @@ export class FileAppender implements DisposableAppender {
});
});
}

private ensureDirectory(path: string) {
mkdirSync(dirname(path), { recursive: true });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* Side Public License, v 1.
*/

import { createWriteStream, WriteStream } from 'fs';
import { createWriteStream, WriteStream, mkdirSync } from 'fs';
import { dirname } from 'path';
import { RollingFileContext } from './rolling_file_context';

/**
Expand Down Expand Up @@ -40,6 +41,7 @@ export class RollingFileManager {

private ensureStreamOpen() {
if (this.outputStream === undefined) {
this.ensureDirectory(this.filePath);
this.outputStream = createWriteStream(this.filePath, {
encoding: 'utf8',
flags: 'a',
Expand All @@ -49,4 +51,8 @@ export class RollingFileManager {
}
return this.outputStream!;
}

private ensureDirectory(path: string) {
mkdirSync(dirname(path), { recursive: true });
}
}

0 comments on commit 72d3728

Please sign in to comment.