Skip to content

Commit

Permalink
Add trace log level (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
surol authored Jun 2, 2021
1 parent 0ba0d13 commit 8fc0225
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Logger
- `warn(...args)` - Logs warning.
- `info(...args)` - Logs informational message.
- `debug(...args)` - Logs debug message.
- `trace(...args)` - Logs tracing message. This may lead to outputting of stack trace.

Each method accepts arbitrary number of arguments.

Expand Down
5 changes: 5 additions & 0 deletions src/console-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const consoleLogger$error = (/*#__PURE__*/ consoleLogger$log((...args) => consol
const consoleLogger$warn = (/*#__PURE__*/ consoleLogger$log((...args) => console.warn(...args)));
const consoleLogger$info = (/*#__PURE__*/ consoleLogger$log((...args) => console.info(...args)));
const consoleLogger$debug = (/*#__PURE__*/ consoleLogger$log((...args) => console.debug(...args)));
const consoleLogger$trace = (/*#__PURE__*/ consoleLogger$log((...args) => console.trace(...args)));

/**
* Logger instance that logs to console.
Expand All @@ -35,4 +36,8 @@ export const consoleLogger: Logger = {
return consoleLogger$debug;
},

get trace() {
return consoleLogger$trace;
},

};
27 changes: 27 additions & 0 deletions src/console.logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,31 @@ describe('consoleLogger', () => {
});
});

describe('trace', () => {

beforeEach(() => {
logSpy = jest.spyOn(console, 'trace').mockImplementation(() => { /* do not log */ });
});

it('logs message to console', () => {
consoleLogger.trace('message', 1, 2, 3);
expect(logSpy).toHaveBeenCalledWith('%s', 'message', 1, 2, 3);
});
it('logs object to console', () => {

const object = { name: 'test' };

consoleLogger.trace(object, 1, 2, 3);
expect(logSpy).toHaveBeenCalledWith(object, 1, 2, 3);
});
it('logs `undefined` to console', () => {
consoleLogger.trace(undefined);
expect(logSpy).toHaveBeenCalledWith(undefined);
});
it('logs empty message to console', () => {
consoleLogger.trace();
expect(logSpy).toHaveBeenCalledWith();
});
});

});
25 changes: 17 additions & 8 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,38 @@ export interface Logger {
/**
* Logs error.
*
* @param args - Log message and arguments.
* @param args - Arbitrary arguments to log.
*/
error(...args: any[]): void;
error(...args: unknown[]): void;

/**
* Logs warning.
*
* @param args - Log message and arguments.
* @param args - Arbitrary arguments to log.
*/
warn(...args: any[]): void;
warn(...args: unknown[]): void;

/**
* Logs informational message.
*
* @param args - Log message and arguments.
* @param args - Arbitrary arguments to log.
*/
info(...args: any[]): void;
info(...args: unknown[]): void;

/**
* Logs debug message.
*
* @param args - Log message and arguments.
* @param args - Arbitrary arguments to log.
*/
debug(...args: any[]): void;
debug(...args: unknown[]): void;

/**
* Logs tracing message.
*
* This may lead to outputting of stack trace.
*
* @param args - Arbitrary arguments to log.
*/
trace(...args: unknown[]): void;

}
6 changes: 6 additions & 0 deletions src/silent-logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@ describe('silentLogger', () => {
expect(silentLogger.debug).toBe(silentLogger.error);
});
});

describe('trace', () => {
it('logs nothing', () => {
expect(silentLogger.trace).toBe(silentLogger.error);
});
});
});
4 changes: 4 additions & 0 deletions src/silent-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ export const silentLogger: Logger = {
return silentLogger$log;
},

get trace() {
return silentLogger$log;
},

};

0 comments on commit 8fc0225

Please sign in to comment.