Skip to content

Commit

Permalink
refactor(logger): rename args to splat (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
noahziheng authored Jun 28, 2022
1 parent 22fed48 commit e61aa00
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
16 changes: 8 additions & 8 deletions src/logger/impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,32 +54,32 @@ export default class ArtusLogger extends BaseLogger {
public log({
level,
message,
args = []
splat = []
}: LogOptions) {
if (message instanceof Error) {
if (level === LoggerLevel.ERROR) {
return this.error(message, ...args);
return this.error(message, ...splat);
}
message = message.stack ?? message.message;
}
switch (level) {
case LoggerLevel.TRACE:
this.trace(message, ...args);
this.trace(message, ...splat);
break;
case LoggerLevel.DEBUG:
this.debug(message, ...args);
this.debug(message, ...splat);
break;
case LoggerLevel.INFO:
this.info(message, ...args);
this.info(message, ...splat);
break;
case LoggerLevel.WARN:
this.warn(message, ...args);
this.warn(message, ...splat);
break;
case LoggerLevel.ERROR:
this.error(message, ...args);
this.error(message, ...splat);
break;
case LoggerLevel.FATAL:
this.fatal(message, ...args);
this.fatal(message, ...splat);
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/logger/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface LoggerOptions {
export interface LogOptions {
level: LoggerLevel;
message: string|Error;
args?: any[];
splat?: any[];
}

export interface Logger {
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/logger/src/test_clazz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ export default class TestLoggerClazz {
@Inject()
private logger!: ArtusLogger;

public testLog(level: LoggerLevel, message: string|Error, ...args: any[]) {
public testLog(level: LoggerLevel, message: string|Error, ...splat: any[]) {
this.logger.log({
level,
message,
args,
splat,
});
}

Expand Down
24 changes: 12 additions & 12 deletions test/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,42 +61,42 @@ describe('test/logger.test.ts', () => {
app.logger.log({
level: LoggerLevel.TRACE,
message: 'trace',
args: [ 0, {} ]
splat: [ 0, {} ]
});
expect(console.trace).toBeCalledTimes(0);

app.logger.log({
level: LoggerLevel.DEBUG,
message: 'debug',
args: [ 1, {} ]
splat: [ 1, {} ]
});
expect(console.debug).toBeCalledTimes(0);

app.logger.log({
level: LoggerLevel.INFO,
message: 'info',
args: [ 2, {} ]
splat: [ 2, {} ]
});
expect(console.info).toBeCalledTimes(1);
expect(console.info).toBeCalledWith('info', 2, {});

app.logger.log({
level: LoggerLevel.WARN,
message: 'warn',
args: [ 3, {} ]
splat: [ 3, {} ]
});
expect(console.warn).toBeCalledTimes(1);
expect(console.warn).toBeCalledWith('warn', 3, {});

app.logger.log({
level: LoggerLevel.ERROR,
message: 'error',
args: [ 4, {} ]
splat: [ 4, {} ]
});
app.logger.log({
level: LoggerLevel.ERROR,
message: err,
args: [ 5, {} ]
splat: [ 5, {} ]
});
expect(console.error).toBeCalledTimes(2);
expect(console.error).toBeCalledWith('error', 4, {});
Expand Down Expand Up @@ -198,44 +198,44 @@ describe('test/logger.test.ts', () => {
app.logger.log({
level: LoggerLevel.TRACE,
message: 'trace',
args: [ 0, {} ]
splat: [ 0, {} ]
});
expect(console.trace).toBeCalledTimes(1);
expect(console.trace).toBeCalledWith('trace', 0, {});

app.logger.log({
level: LoggerLevel.DEBUG,
message: 'debug',
args: [ 1, {} ]
splat: [ 1, {} ]
});
expect(console.debug).toBeCalledTimes(1);
expect(console.debug).toBeCalledWith('debug', 1, {});

app.logger.log({
level: LoggerLevel.INFO,
message: 'info',
args: [ 2, {} ]
splat: [ 2, {} ]
});
expect(console.info).toBeCalledTimes(1);
expect(console.info).toBeCalledWith('info', 2, {});

app.logger.log({
level: LoggerLevel.WARN,
message: 'warn',
args: [ 3, {} ]
splat: [ 3, {} ]
});
expect(console.warn).toBeCalledTimes(1);
expect(console.warn).toBeCalledWith('warn', 3, {});

app.logger.log({
level: LoggerLevel.ERROR,
message: 'error',
args: [ 4, {} ]
splat: [ 4, {} ]
});
app.logger.log({
level: LoggerLevel.ERROR,
message: err,
args: [ 5, {} ]
splat: [ 5, {} ]
});
expect(console.error).toBeCalledTimes(2);
expect(console.error).toBeCalledWith('error', 4, {});
Expand Down

0 comments on commit e61aa00

Please sign in to comment.