From 8fc02252d2a0fd18f4bd1ee34b80c7c66cb72c4f Mon Sep 17 00:00:00 2001 From: Ruslan Lopatin Date: Wed, 2 Jun 2021 21:54:50 +0700 Subject: [PATCH] Add trace log level (#2) --- README.md | 1 + src/console-logger.ts | 5 +++++ src/console.logger.spec.ts | 27 +++++++++++++++++++++++++++ src/logger.ts | 25 +++++++++++++++++-------- src/silent-logger.spec.ts | 6 ++++++ src/silent-logger.ts | 4 ++++ 6 files changed, 60 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index a6b5d89..24720a4 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/console-logger.ts b/src/console-logger.ts index 6ff2cd5..cdb807c 100644 --- a/src/console-logger.ts +++ b/src/console-logger.ts @@ -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. @@ -35,4 +36,8 @@ export const consoleLogger: Logger = { return consoleLogger$debug; }, + get trace() { + return consoleLogger$trace; + }, + }; diff --git a/src/console.logger.spec.ts b/src/console.logger.spec.ts index 50e2562..e62d139 100644 --- a/src/console.logger.spec.ts +++ b/src/console.logger.spec.ts @@ -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(); + }); + }); + }); diff --git a/src/logger.ts b/src/logger.ts index 8bf9966..9f0d733 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -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; } diff --git a/src/silent-logger.spec.ts b/src/silent-logger.spec.ts index 2fedf62..c54aa71 100644 --- a/src/silent-logger.spec.ts +++ b/src/silent-logger.spec.ts @@ -26,4 +26,10 @@ describe('silentLogger', () => { expect(silentLogger.debug).toBe(silentLogger.error); }); }); + + describe('trace', () => { + it('logs nothing', () => { + expect(silentLogger.trace).toBe(silentLogger.error); + }); + }); }); diff --git a/src/silent-logger.ts b/src/silent-logger.ts index bc2577f..751428f 100644 --- a/src/silent-logger.ts +++ b/src/silent-logger.ts @@ -23,4 +23,8 @@ export const silentLogger: Logger = { return silentLogger$log; }, + get trace() { + return silentLogger$log; + }, + };