Skip to content

Commit

Permalink
improve browser logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Dosant committed Jan 20, 2025
1 parent bf15ee7 commit 98dabc9
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,72 @@ import { LogRecord, LogLevel } from '@kbn/logging';
import { unsafeConsole } from '@kbn/security-hardening';
import { ConsoleAppender } from './console_appender';

test('`append()` correctly formats records and pushes them to console.', () => {
jest.spyOn(unsafeConsole, 'log').mockImplementation(() => {
// noop
});
const levels = [
LogLevel.Trace,
LogLevel.Debug,
LogLevel.Info,
LogLevel.Warn,
LogLevel.Error,
LogLevel.Fatal,
LogLevel.All,
];

const levelsToMethods = new Map<LogLevel, keyof typeof unsafeConsole>([
[LogLevel.Trace, 'trace'],
[LogLevel.Debug, 'debug'],
[LogLevel.Info, 'info'],
[LogLevel.Warn, 'warn'],
[LogLevel.Error, 'error'],
[LogLevel.Fatal, 'error'],
[LogLevel.All, 'log'],
]);

const records: LogRecord[] = [
{
context: 'context-1',
level: LogLevel.All,
message: 'message-1',
timestamp: new Date(),
pid: 5355,
},
{
context: 'context-2',
level: LogLevel.Trace,
message: 'message-2',
timestamp: new Date(),
pid: 5355,
},
{
context: 'context-3',
error: new Error('Error'),
level: LogLevel.Fatal,
message: 'message-3',
timestamp: new Date(),
pid: 5355,
},
];

const appender = new ConsoleAppender({
format(record) {
return `mock-${JSON.stringify(record)}`;
},
describe.each(levels)('`append()` correctly logs %p level.', (level) => {
beforeEach(() => {
jest.clearAllMocks();
});

for (const record of records) {
appender.append(record);
expect(unsafeConsole.log).toHaveBeenCalledWith(`mock-${JSON.stringify(record)}`);
}
expect(unsafeConsole.log).toHaveBeenCalledTimes(records.length);
test('`append()` correctly formats records and pushes them to console.', () => {
const method = levelsToMethods.get(level)!;
jest.spyOn(unsafeConsole, method).mockImplementation(() => {
// noop
});

const records: LogRecord[] = [
{
context: 'context-1',
level,
message: 'message-1',
timestamp: new Date(),
pid: 5355,
},
{
context: 'context-2',
level,
message: 'message-2',
timestamp: new Date(),
pid: 5355,
},
{
context: 'context-3',
error: new Error('Error'),
level,
message: 'message-3',
timestamp: new Date(),
pid: 5355,
},
];

const appender = new ConsoleAppender({
format(record) {
return `mock-${JSON.stringify(record)}`;
},
});

for (const record of records) {
appender.append(record);
expect(unsafeConsole[method]).toHaveBeenCalledWith(`mock-${JSON.stringify(record)}`);
}
expect(unsafeConsole[method]).toHaveBeenCalledTimes(records.length);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import type { Layout, LogRecord, DisposableAppender } from '@kbn/logging';
import { type Layout, type LogRecord, type DisposableAppender, LogLevel } from '@kbn/logging';
import { unsafeConsole } from '@kbn/security-hardening';

/**
Expand All @@ -27,8 +27,33 @@ export class ConsoleAppender implements DisposableAppender {
* @param record `LogRecord` instance to be logged.
*/
public append(record: LogRecord) {
// eslint-disable-next-line @kbn/eslint/no_unsafe_console
unsafeConsole.log(this.layout.format(record));
const message = this.layout.format(record);
switch (record.level) {
case LogLevel.Error:
case LogLevel.Fatal:
// eslint-disable-next-line @kbn/eslint/no_unsafe_console
unsafeConsole.error(message);
break;
case LogLevel.Warn:
// eslint-disable-next-line @kbn/eslint/no_unsafe_console
unsafeConsole.warn(message);
break;
case LogLevel.Trace:
// eslint-disable-next-line @kbn/eslint/no_unsafe_console
unsafeConsole.trace(message);
break;
case LogLevel.Info:
// eslint-disable-next-line @kbn/eslint/no_unsafe_console
unsafeConsole.info(message);
break;
case LogLevel.Debug:
// eslint-disable-next-line @kbn/eslint/no_unsafe_console
unsafeConsole.debug(message);
break;
default:
// eslint-disable-next-line @kbn/eslint/no_unsafe_console
unsafeConsole.log(message);
}
}

/**
Expand Down

0 comments on commit 98dabc9

Please sign in to comment.