From 18909f030912883d742cf9e3de0c6018b6ba3fa9 Mon Sep 17 00:00:00 2001 From: Sean Perkins Date: Wed, 13 Nov 2024 13:55:33 -0500 Subject: [PATCH 1/8] feat(core): developers can suppress ionic warnings and errors --- core/src/utils/config.ts | 10 ++ core/src/utils/logging/index.ts | 12 ++- core/src/utils/logging/test/logging.spec.ts | 113 ++++++++++++++++++++ 3 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 core/src/utils/logging/test/logging.spec.ts diff --git a/core/src/utils/config.ts b/core/src/utils/config.ts index e38d43beb4e..d95cd6a7241 100644 --- a/core/src/utils/config.ts +++ b/core/src/utils/config.ts @@ -220,6 +220,16 @@ export interface IonicConfig { */ experimentalCloseWatcher?: boolean; + /** + * Developers may configure the logging level for Ionic Framework. + * This will log all logs at the specified level and above. + * + * - `OFF` will not log any logs. + * - `ERROR` will log all errors. + * - `WARN` will log all errors and warnings. + */ + logLevel?: 'OFF' | 'ERROR' | 'WARN'; + // PRIVATE configs keyboardHeight?: number; inputShims?: boolean; diff --git a/core/src/utils/logging/index.ts b/core/src/utils/logging/index.ts index ee4234cb6a5..016ed46fa74 100644 --- a/core/src/utils/logging/index.ts +++ b/core/src/utils/logging/index.ts @@ -1,3 +1,5 @@ +import { config } from '@global/config'; + /** * Logs a warning to the console with an Ionic prefix * to indicate the library that is warning the developer. @@ -5,7 +7,10 @@ * @param message - The string message to be logged to the console. */ export const printIonWarning = (message: string, ...params: any[]) => { - return console.warn(`[Ionic Warning]: ${message}`, ...params); + const logLevel = config.get('logLevel', 'WARN'); + if (logLevel === 'WARN' || logLevel === 'ERROR') { + return console.warn(`[Ionic Warn]: ${message}`, ...params); + } }; /* @@ -16,7 +21,10 @@ export const printIonWarning = (message: string, ...params: any[]) => { * @param params - Additional arguments to supply to the console.error. */ export const printIonError = (message: string, ...params: any) => { - return console.error(`[Ionic Error]: ${message}`, ...params); + const logLevel = config.get('logLevel', 'ERROR'); + if (logLevel === 'ERROR') { + return console.error(`[Ionic Error]: ${message}`, ...params); + } }; /** diff --git a/core/src/utils/logging/test/logging.spec.ts b/core/src/utils/logging/test/logging.spec.ts new file mode 100644 index 00000000000..e1825a77357 --- /dev/null +++ b/core/src/utils/logging/test/logging.spec.ts @@ -0,0 +1,113 @@ +import { config } from '@global/config'; + +import { printIonError, printIonWarning } from '../index'; + +describe('Logging', () => { + describe('#printIonWarning', () => { + let consoleWarnSpy: jest.SpyInstance; + + beforeEach(() => { + consoleWarnSpy = jest.spyOn(console, 'warn'); + // Suppress console.warn output from polluting the test output + consoleWarnSpy.mockImplementation(() => {}); + }); + + afterEach(() => { + consoleWarnSpy.mockRestore(); + }); + + describe('when the logLevel configuration is not set', () => { + it('logs a warning to the console', () => { + config.set('logLevel', undefined); + + printIonWarning('This is a warning message'); + + expect(consoleWarnSpy).toHaveBeenCalledWith('[Ionic Warn]: This is a warning message'); + }); + }); + + describe("when the logLevel configuration is set to 'ERROR'", () => { + it('logs a warning to the console', () => { + config.set('logLevel', 'ERROR'); + + printIonWarning('This is a warning message'); + + expect(consoleWarnSpy).toHaveBeenCalledWith('[Ionic Warn]: This is a warning message'); + }); + }); + + describe("when the logLevel configuration is set to 'WARN'", () => { + it('logs a warning to the console', () => { + config.set('logLevel', 'WARN'); + + printIonWarning('This is a warning message'); + + expect(consoleWarnSpy).toHaveBeenCalledWith('[Ionic Warn]: This is a warning message'); + }); + }); + + describe("when the logLevel configuration is set to 'OFF'", () => { + it('does not log a warning to the console', () => { + config.set('logLevel', 'OFF'); + + printIonWarning('This is a warning message'); + + expect(consoleWarnSpy).not.toHaveBeenCalled(); + }); + }); + }); + + describe('#printIonError', () => { + let consoleErrorSpy: jest.SpyInstance; + + beforeEach(() => { + consoleErrorSpy = jest.spyOn(console, 'error'); + // Suppress console.error output from polluting the test output + consoleErrorSpy.mockImplementation(() => {}); + }); + + afterEach(() => { + consoleErrorSpy.mockRestore(); + }); + + describe('when the logLevel configuration is not set', () => { + it('logs an error to the console', () => { + config.set('logLevel', undefined); + + printIonError('This is an error message'); + + expect(consoleErrorSpy).toHaveBeenCalledWith('[Ionic Error]: This is an error message'); + }); + }); + + describe("when the logLevel configuration is set to 'ERROR'", () => { + it('logs an error to the console', () => { + config.set('logLevel', 'ERROR'); + + printIonError('This is an error message'); + + expect(consoleErrorSpy).toHaveBeenCalledWith('[Ionic Error]: This is an error message'); + }); + }); + + describe("when the logLevel configuration is set to 'WARN'", () => { + it('does not log an error to the console', () => { + config.set('logLevel', 'WARN'); + + printIonError('This is an error message'); + + expect(consoleErrorSpy).not.toHaveBeenCalled(); + }); + }); + + describe("when the logLevel configuration is set to 'OFF'", () => { + it('does not log an error to the console', () => { + config.set('logLevel', 'OFF'); + + printIonError('This is an error message'); + + expect(consoleErrorSpy).not.toHaveBeenCalled(); + }); + }); + }); +}); From f8f274ba1febc97b1f70a6737e4687b81e6af343 Mon Sep 17 00:00:00 2001 From: Sean Perkins Date: Wed, 13 Nov 2024 14:02:26 -0500 Subject: [PATCH 2/8] refactor: type and code style --- core/src/utils/logging/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/src/utils/logging/index.ts b/core/src/utils/logging/index.ts index 016ed46fa74..694cf7191f4 100644 --- a/core/src/utils/logging/index.ts +++ b/core/src/utils/logging/index.ts @@ -8,7 +8,7 @@ import { config } from '@global/config'; */ export const printIonWarning = (message: string, ...params: any[]) => { const logLevel = config.get('logLevel', 'WARN'); - if (logLevel === 'WARN' || logLevel === 'ERROR') { + if (['WARN', 'ERROR'].includes(logLevel)) { return console.warn(`[Ionic Warn]: ${message}`, ...params); } }; @@ -20,9 +20,9 @@ export const printIonWarning = (message: string, ...params: any[]) => { * @param message - The string message to be logged to the console. * @param params - Additional arguments to supply to the console.error. */ -export const printIonError = (message: string, ...params: any) => { +export const printIonError = (message: string, ...params: any[]) => { const logLevel = config.get('logLevel', 'ERROR'); - if (logLevel === 'ERROR') { + if (['ERROR'].includes(logLevel)) { return console.error(`[Ionic Error]: ${message}`, ...params); } }; From f05ecc3df69d37d76f5ca87b0763a7cca1c63e7d Mon Sep 17 00:00:00 2001 From: Sean Perkins Date: Wed, 13 Nov 2024 14:06:23 -0500 Subject: [PATCH 3/8] fix: adjust implementation --- core/src/utils/config.ts | 5 ++--- core/src/utils/logging/index.ts | 4 ++-- core/src/utils/logging/test/logging.spec.ts | 16 ++++++++-------- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/core/src/utils/config.ts b/core/src/utils/config.ts index d95cd6a7241..4be29061dae 100644 --- a/core/src/utils/config.ts +++ b/core/src/utils/config.ts @@ -222,11 +222,10 @@ export interface IonicConfig { /** * Developers may configure the logging level for Ionic Framework. - * This will log all logs at the specified level and above. * - * - `OFF` will not log any logs. - * - `ERROR` will log all errors. + * - `OFF` will not log any errors or warnings. * - `WARN` will log all errors and warnings. + * - `ERROR` will log only errors. */ logLevel?: 'OFF' | 'ERROR' | 'WARN'; diff --git a/core/src/utils/logging/index.ts b/core/src/utils/logging/index.ts index 694cf7191f4..0ee2b59e14b 100644 --- a/core/src/utils/logging/index.ts +++ b/core/src/utils/logging/index.ts @@ -8,7 +8,7 @@ import { config } from '@global/config'; */ export const printIonWarning = (message: string, ...params: any[]) => { const logLevel = config.get('logLevel', 'WARN'); - if (['WARN', 'ERROR'].includes(logLevel)) { + if (['WARN'].includes(logLevel)) { return console.warn(`[Ionic Warn]: ${message}`, ...params); } }; @@ -22,7 +22,7 @@ export const printIonWarning = (message: string, ...params: any[]) => { */ export const printIonError = (message: string, ...params: any[]) => { const logLevel = config.get('logLevel', 'ERROR'); - if (['ERROR'].includes(logLevel)) { + if (['ERROR', 'WARN'].includes(logLevel)) { return console.error(`[Ionic Error]: ${message}`, ...params); } }; diff --git a/core/src/utils/logging/test/logging.spec.ts b/core/src/utils/logging/test/logging.spec.ts index e1825a77357..7f2f5b50292 100644 --- a/core/src/utils/logging/test/logging.spec.ts +++ b/core/src/utils/logging/test/logging.spec.ts @@ -26,9 +26,9 @@ describe('Logging', () => { }); }); - describe("when the logLevel configuration is set to 'ERROR'", () => { + describe("when the logLevel configuration is set to 'WARN'", () => { it('logs a warning to the console', () => { - config.set('logLevel', 'ERROR'); + config.set('logLevel', 'WARN'); printIonWarning('This is a warning message'); @@ -36,13 +36,13 @@ describe('Logging', () => { }); }); - describe("when the logLevel configuration is set to 'WARN'", () => { - it('logs a warning to the console', () => { - config.set('logLevel', 'WARN'); + describe("when the logLevel configuration is set to 'ERROR'", () => { + it('does not log a warning to the console', () => { + config.set('logLevel', 'ERROR'); printIonWarning('This is a warning message'); - expect(consoleWarnSpy).toHaveBeenCalledWith('[Ionic Warn]: This is a warning message'); + expect(consoleWarnSpy).not.toHaveBeenCalled(); }); }); @@ -91,12 +91,12 @@ describe('Logging', () => { }); describe("when the logLevel configuration is set to 'WARN'", () => { - it('does not log an error to the console', () => { + it('logs an error to the console', () => { config.set('logLevel', 'WARN'); printIonError('This is an error message'); - expect(consoleErrorSpy).not.toHaveBeenCalled(); + expect(consoleErrorSpy).toHaveBeenCalledWith('[Ionic Error]: This is an error message'); }); }); From d101f2eb80a9412b5d587b751ea1244e23cf05ea Mon Sep 17 00:00:00 2001 From: Sean Perkins Date: Wed, 13 Nov 2024 14:15:49 -0500 Subject: [PATCH 4/8] fix: warning message format --- core/src/utils/logging/index.ts | 2 +- core/src/utils/logging/test/logging.spec.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/core/src/utils/logging/index.ts b/core/src/utils/logging/index.ts index 0ee2b59e14b..f5ff24c6949 100644 --- a/core/src/utils/logging/index.ts +++ b/core/src/utils/logging/index.ts @@ -9,7 +9,7 @@ import { config } from '@global/config'; export const printIonWarning = (message: string, ...params: any[]) => { const logLevel = config.get('logLevel', 'WARN'); if (['WARN'].includes(logLevel)) { - return console.warn(`[Ionic Warn]: ${message}`, ...params); + return console.warn(`[Ionic Warning]: ${message}`, ...params); } }; diff --git a/core/src/utils/logging/test/logging.spec.ts b/core/src/utils/logging/test/logging.spec.ts index 7f2f5b50292..815a6c6720f 100644 --- a/core/src/utils/logging/test/logging.spec.ts +++ b/core/src/utils/logging/test/logging.spec.ts @@ -22,7 +22,7 @@ describe('Logging', () => { printIonWarning('This is a warning message'); - expect(consoleWarnSpy).toHaveBeenCalledWith('[Ionic Warn]: This is a warning message'); + expect(consoleWarnSpy).toHaveBeenCalledWith('[Ionic Warning]: This is a warning message'); }); }); @@ -32,7 +32,7 @@ describe('Logging', () => { printIonWarning('This is a warning message'); - expect(consoleWarnSpy).toHaveBeenCalledWith('[Ionic Warn]: This is a warning message'); + expect(consoleWarnSpy).toHaveBeenCalledWith('[Ionic Warning]: This is a warning message'); }); }); From e3c8acdc188140804f778589268097724043300b Mon Sep 17 00:00:00 2001 From: Brandy Carney <6577830+brandyscarney@users.noreply.github.com> Date: Mon, 20 Jan 2025 15:38:44 -0500 Subject: [PATCH 5/8] refactor(utils): update LogLevel to enum --- core/src/utils/config.ts | 11 ++++++----- core/src/utils/logging/index.ts | 16 +++++++++++----- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/core/src/utils/config.ts b/core/src/utils/config.ts index 4be29061dae..8cf497ed673 100644 --- a/core/src/utils/config.ts +++ b/core/src/utils/config.ts @@ -2,6 +2,7 @@ import type { SpinnerTypes } from '../components/spinner/spinner-configs'; import type { TabButtonLayout } from '../components/tab-bar/tab-bar-interface'; import type { AnimationBuilder, Mode } from '../interface'; +import type { LogLevel } from './logging'; import type { PlatformConfig } from './platform'; export interface IonicConfig { @@ -221,13 +222,13 @@ export interface IonicConfig { experimentalCloseWatcher?: boolean; /** - * Developers may configure the logging level for Ionic Framework. + * Configures the logging level for Ionic Framework: * - * - `OFF` will not log any errors or warnings. - * - `WARN` will log all errors and warnings. - * - `ERROR` will log only errors. + * - `OFF`: No errors or warnings are logged. + * - `WARN`: Logs errors and warnings. + * - `ERROR`: Logs only errors. */ - logLevel?: 'OFF' | 'ERROR' | 'WARN'; + logLevel?: LogLevel; // PRIVATE configs keyboardHeight?: number; diff --git a/core/src/utils/logging/index.ts b/core/src/utils/logging/index.ts index f5ff24c6949..acbd680b4e1 100644 --- a/core/src/utils/logging/index.ts +++ b/core/src/utils/logging/index.ts @@ -1,5 +1,11 @@ import { config } from '@global/config'; +export const enum LogLevel { + INFO = 'INFO', + ERROR = 'ERROR', + WARN = 'WARN', +} + /** * Logs a warning to the console with an Ionic prefix * to indicate the library that is warning the developer. @@ -7,13 +13,13 @@ import { config } from '@global/config'; * @param message - The string message to be logged to the console. */ export const printIonWarning = (message: string, ...params: any[]) => { - const logLevel = config.get('logLevel', 'WARN'); - if (['WARN'].includes(logLevel)) { + const logLevel = config.get('logLevel', LogLevel.WARN); + if ([LogLevel.WARN].includes(logLevel)) { return console.warn(`[Ionic Warning]: ${message}`, ...params); } }; -/* +/** * Logs an error to the console with an Ionic prefix * to indicate the library that is warning the developer. * @@ -21,8 +27,8 @@ export const printIonWarning = (message: string, ...params: any[]) => { * @param params - Additional arguments to supply to the console.error. */ export const printIonError = (message: string, ...params: any[]) => { - const logLevel = config.get('logLevel', 'ERROR'); - if (['ERROR', 'WARN'].includes(logLevel)) { + const logLevel = config.get('logLevel', LogLevel.ERROR); + if ([LogLevel.ERROR, LogLevel.WARN].includes(logLevel)) { return console.error(`[Ionic Error]: ${message}`, ...params); } }; From 13b1441ce7312c76d3369e05ee502dece245b9c4 Mon Sep 17 00:00:00 2001 From: Brandy Carney <6577830+brandyscarney@users.noreply.github.com> Date: Mon, 20 Jan 2025 15:42:53 -0500 Subject: [PATCH 6/8] refactor(utils): OFF not INFO --- core/src/utils/logging/index.ts | 2 +- core/src/utils/logging/test/logging.spec.ts | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/core/src/utils/logging/index.ts b/core/src/utils/logging/index.ts index acbd680b4e1..4bf58ce6297 100644 --- a/core/src/utils/logging/index.ts +++ b/core/src/utils/logging/index.ts @@ -1,7 +1,7 @@ import { config } from '@global/config'; export const enum LogLevel { - INFO = 'INFO', + OFF = 'OFF', ERROR = 'ERROR', WARN = 'WARN', } diff --git a/core/src/utils/logging/test/logging.spec.ts b/core/src/utils/logging/test/logging.spec.ts index 815a6c6720f..e0f1bdcb923 100644 --- a/core/src/utils/logging/test/logging.spec.ts +++ b/core/src/utils/logging/test/logging.spec.ts @@ -1,4 +1,5 @@ import { config } from '@global/config'; +import { LogLevel } from '../index'; import { printIonError, printIonWarning } from '../index'; @@ -28,7 +29,7 @@ describe('Logging', () => { describe("when the logLevel configuration is set to 'WARN'", () => { it('logs a warning to the console', () => { - config.set('logLevel', 'WARN'); + config.set('logLevel', LogLevel.WARN); printIonWarning('This is a warning message'); @@ -38,7 +39,7 @@ describe('Logging', () => { describe("when the logLevel configuration is set to 'ERROR'", () => { it('does not log a warning to the console', () => { - config.set('logLevel', 'ERROR'); + config.set('logLevel', LogLevel.ERROR); printIonWarning('This is a warning message'); @@ -48,7 +49,7 @@ describe('Logging', () => { describe("when the logLevel configuration is set to 'OFF'", () => { it('does not log a warning to the console', () => { - config.set('logLevel', 'OFF'); + config.set('logLevel', LogLevel.OFF); printIonWarning('This is a warning message'); @@ -82,7 +83,7 @@ describe('Logging', () => { describe("when the logLevel configuration is set to 'ERROR'", () => { it('logs an error to the console', () => { - config.set('logLevel', 'ERROR'); + config.set('logLevel', LogLevel.ERROR); printIonError('This is an error message'); @@ -92,7 +93,7 @@ describe('Logging', () => { describe("when the logLevel configuration is set to 'WARN'", () => { it('logs an error to the console', () => { - config.set('logLevel', 'WARN'); + config.set('logLevel', LogLevel.WARN); printIonError('This is an error message'); @@ -102,7 +103,7 @@ describe('Logging', () => { describe("when the logLevel configuration is set to 'OFF'", () => { it('does not log an error to the console', () => { - config.set('logLevel', 'OFF'); + config.set('logLevel', LogLevel.OFF); printIonError('This is an error message'); From b4cff160d8a43b21be8ad2672b388ca39eb6c180 Mon Sep 17 00:00:00 2001 From: Brandy Carney <6577830+brandyscarney@users.noreply.github.com> Date: Mon, 20 Jan 2025 15:52:55 -0500 Subject: [PATCH 7/8] docs(config): string types --- core/src/utils/config.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/src/utils/config.ts b/core/src/utils/config.ts index 8cf497ed673..8b56bd417a1 100644 --- a/core/src/utils/config.ts +++ b/core/src/utils/config.ts @@ -224,9 +224,9 @@ export interface IonicConfig { /** * Configures the logging level for Ionic Framework: * - * - `OFF`: No errors or warnings are logged. - * - `WARN`: Logs errors and warnings. - * - `ERROR`: Logs only errors. + * - `'OFF'`: No errors or warnings are logged. + * - `'WARN'`: Logs errors and warnings. + * - `'ERROR'`: Logs only errors. */ logLevel?: LogLevel; From 68c079a7a68f2eee71ad9b798405017cf75a15bc Mon Sep 17 00:00:00 2001 From: Brandy Carney <6577830+brandyscarney@users.noreply.github.com> Date: Mon, 20 Jan 2025 16:00:26 -0500 Subject: [PATCH 8/8] docs(config): order --- core/src/utils/config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/utils/config.ts b/core/src/utils/config.ts index 8b56bd417a1..6b66417f7fc 100644 --- a/core/src/utils/config.ts +++ b/core/src/utils/config.ts @@ -225,8 +225,8 @@ export interface IonicConfig { * Configures the logging level for Ionic Framework: * * - `'OFF'`: No errors or warnings are logged. - * - `'WARN'`: Logs errors and warnings. * - `'ERROR'`: Logs only errors. + * - `'WARN'`: Logs errors and warnings. */ logLevel?: LogLevel;