Skip to content

Commit d5b01fb

Browse files
committed
feat: added format override to log messages
Added the ability to override a log message format on a per-message basis. You have to provide an optional `LogFormatter` parameter to the message. For example logger.info('INFO MESSAGE', formatting.format`${formatting.date}:${formatting.msg}`);. Related #4
1 parent 01a8336 commit d5b01fb

File tree

4 files changed

+32
-17
lines changed

4 files changed

+32
-17
lines changed

Diff for: src/Handler.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ abstract class Handler {
1313
this.formatter = formatter;
1414
}
1515

16-
public handle(record: LogRecord): void {
17-
const output = this.format(record);
16+
public handle(record: LogRecord, format?: LogFormatter): void {
17+
const output = format == null ? this.format(record) : format(record);
1818
this.emit(output);
1919
}
2020

Diff for: src/Logger.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ToString, LogRecord } from './types';
1+
import type { ToString, LogRecord, LogFormatter } from './types';
22
import type Handler from './Handler';
33

44
import { LogLevel } from './types';
@@ -74,26 +74,26 @@ class Logger {
7474
}
7575
}
7676

77-
public debug(data: ToString): void {
78-
this.log(data.toString(), LogLevel.DEBUG);
77+
public debug(data: ToString, format?: LogFormatter): void {
78+
this.log(data.toString(), LogLevel.DEBUG, format);
7979
}
8080

81-
public info(data: ToString): void {
82-
this.log(data.toString(), LogLevel.INFO);
81+
public info(data: ToString, format?: LogFormatter): void {
82+
this.log(data.toString(), LogLevel.INFO, format);
8383
}
8484

85-
public warn(data: ToString): void {
86-
this.log(data.toString(), LogLevel.WARN);
85+
public warn(data: ToString, format?: LogFormatter): void {
86+
this.log(data.toString(), LogLevel.WARN, format);
8787
}
8888

89-
public error(data: ToString): void {
90-
this.log(data.toString(), LogLevel.ERROR);
89+
public error(data: ToString, format?: LogFormatter): void {
90+
this.log(data.toString(), LogLevel.ERROR, format);
9191
}
9292

93-
protected log(msg: string, level: LogLevel): void {
93+
protected log(msg: string, level: LogLevel, format?: LogFormatter): void {
9494
const record = this.makeRecord(msg, level);
9595
if (level >= this.getEffectiveLevel()) {
96-
this.callHandlers(record);
96+
this.callHandlers(record, format);
9797
}
9898
}
9999

@@ -107,12 +107,12 @@ class Logger {
107107
};
108108
}
109109

110-
protected callHandlers(record: LogRecord): void {
110+
protected callHandlers(record: LogRecord, format?: LogFormatter): void {
111111
for (const handler of this.handlers) {
112-
handler.handle(record);
112+
handler.handle(record, format);
113113
}
114114
if (this.parent) {
115-
this.parent.callHandlers(record);
115+
this.parent.callHandlers(record, format);
116116
}
117117
}
118118
}

Diff for: src/formatting.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ function format(
3535
result += levelToString(record.level);
3636
} else if (value === trace) {
3737
const errorStack = new Error().stack ?? '';
38-
const formattedStack = errorStack.split('\n').splice(7).join('\n');
38+
const splitErrorStack = errorStack.split('\n');
39+
const position =
40+
splitErrorStack.findIndex((value) => /Logger\.log/.test(value)) ?? 0;
41+
const formattedStack = splitErrorStack.splice(position + 1).join('\n');
3942
result += '\n' + formattedStack;
4043
} else {
4144
result += value.toString();

Diff for: tests/index.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,16 @@ describe('index', () => {
114114
);
115115
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('at'));
116116
});
117+
test('Testing overriding log format', () => {
118+
const consoleSpy = jest.spyOn(console, 'log');
119+
const logger = new Logger('root', LogLevel.NOTSET);
120+
logger.debug('DEBUG MESSAGE', formatting.format`OVERRIDDEN`);
121+
expect(consoleSpy).toHaveBeenCalledWith('OVERRIDDEN');
122+
logger.info('INFO MESSAGE', formatting.format`OVERRIDDEN`);
123+
expect(consoleSpy).toHaveBeenCalledWith('OVERRIDDEN');
124+
logger.warn('WARN MESSAGE', formatting.format`OVERRIDDEN`);
125+
expect(consoleSpy).toHaveBeenCalledWith('OVERRIDDEN');
126+
logger.error('ERROR MESSAGE', formatting.format`OVERRIDDEN`);
127+
expect(consoleSpy).toHaveBeenCalledWith('OVERRIDDEN');
128+
});
117129
});

0 commit comments

Comments
 (0)