Skip to content

Commit 18909f0

Browse files
committed
feat(core): developers can suppress ionic warnings and errors
1 parent 3216108 commit 18909f0

File tree

3 files changed

+133
-2
lines changed

3 files changed

+133
-2
lines changed

core/src/utils/config.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,16 @@ export interface IonicConfig {
220220
*/
221221
experimentalCloseWatcher?: boolean;
222222

223+
/**
224+
* Developers may configure the logging level for Ionic Framework.
225+
* This will log all logs at the specified level and above.
226+
*
227+
* - `OFF` will not log any logs.
228+
* - `ERROR` will log all errors.
229+
* - `WARN` will log all errors and warnings.
230+
*/
231+
logLevel?: 'OFF' | 'ERROR' | 'WARN';
232+
223233
// PRIVATE configs
224234
keyboardHeight?: number;
225235
inputShims?: boolean;

core/src/utils/logging/index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
import { config } from '@global/config';
2+
13
/**
24
* Logs a warning to the console with an Ionic prefix
35
* to indicate the library that is warning the developer.
46
*
57
* @param message - The string message to be logged to the console.
68
*/
79
export const printIonWarning = (message: string, ...params: any[]) => {
8-
return console.warn(`[Ionic Warning]: ${message}`, ...params);
10+
const logLevel = config.get('logLevel', 'WARN');
11+
if (logLevel === 'WARN' || logLevel === 'ERROR') {
12+
return console.warn(`[Ionic Warn]: ${message}`, ...params);
13+
}
914
};
1015

1116
/*
@@ -16,7 +21,10 @@ export const printIonWarning = (message: string, ...params: any[]) => {
1621
* @param params - Additional arguments to supply to the console.error.
1722
*/
1823
export const printIonError = (message: string, ...params: any) => {
19-
return console.error(`[Ionic Error]: ${message}`, ...params);
24+
const logLevel = config.get('logLevel', 'ERROR');
25+
if (logLevel === 'ERROR') {
26+
return console.error(`[Ionic Error]: ${message}`, ...params);
27+
}
2028
};
2129

2230
/**
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import { config } from '@global/config';
2+
3+
import { printIonError, printIonWarning } from '../index';
4+
5+
describe('Logging', () => {
6+
describe('#printIonWarning', () => {
7+
let consoleWarnSpy: jest.SpyInstance;
8+
9+
beforeEach(() => {
10+
consoleWarnSpy = jest.spyOn(console, 'warn');
11+
// Suppress console.warn output from polluting the test output
12+
consoleWarnSpy.mockImplementation(() => {});
13+
});
14+
15+
afterEach(() => {
16+
consoleWarnSpy.mockRestore();
17+
});
18+
19+
describe('when the logLevel configuration is not set', () => {
20+
it('logs a warning to the console', () => {
21+
config.set('logLevel', undefined);
22+
23+
printIonWarning('This is a warning message');
24+
25+
expect(consoleWarnSpy).toHaveBeenCalledWith('[Ionic Warn]: This is a warning message');
26+
});
27+
});
28+
29+
describe("when the logLevel configuration is set to 'ERROR'", () => {
30+
it('logs a warning to the console', () => {
31+
config.set('logLevel', 'ERROR');
32+
33+
printIonWarning('This is a warning message');
34+
35+
expect(consoleWarnSpy).toHaveBeenCalledWith('[Ionic Warn]: This is a warning message');
36+
});
37+
});
38+
39+
describe("when the logLevel configuration is set to 'WARN'", () => {
40+
it('logs a warning to the console', () => {
41+
config.set('logLevel', 'WARN');
42+
43+
printIonWarning('This is a warning message');
44+
45+
expect(consoleWarnSpy).toHaveBeenCalledWith('[Ionic Warn]: This is a warning message');
46+
});
47+
});
48+
49+
describe("when the logLevel configuration is set to 'OFF'", () => {
50+
it('does not log a warning to the console', () => {
51+
config.set('logLevel', 'OFF');
52+
53+
printIonWarning('This is a warning message');
54+
55+
expect(consoleWarnSpy).not.toHaveBeenCalled();
56+
});
57+
});
58+
});
59+
60+
describe('#printIonError', () => {
61+
let consoleErrorSpy: jest.SpyInstance;
62+
63+
beforeEach(() => {
64+
consoleErrorSpy = jest.spyOn(console, 'error');
65+
// Suppress console.error output from polluting the test output
66+
consoleErrorSpy.mockImplementation(() => {});
67+
});
68+
69+
afterEach(() => {
70+
consoleErrorSpy.mockRestore();
71+
});
72+
73+
describe('when the logLevel configuration is not set', () => {
74+
it('logs an error to the console', () => {
75+
config.set('logLevel', undefined);
76+
77+
printIonError('This is an error message');
78+
79+
expect(consoleErrorSpy).toHaveBeenCalledWith('[Ionic Error]: This is an error message');
80+
});
81+
});
82+
83+
describe("when the logLevel configuration is set to 'ERROR'", () => {
84+
it('logs an error to the console', () => {
85+
config.set('logLevel', 'ERROR');
86+
87+
printIonError('This is an error message');
88+
89+
expect(consoleErrorSpy).toHaveBeenCalledWith('[Ionic Error]: This is an error message');
90+
});
91+
});
92+
93+
describe("when the logLevel configuration is set to 'WARN'", () => {
94+
it('does not log an error to the console', () => {
95+
config.set('logLevel', 'WARN');
96+
97+
printIonError('This is an error message');
98+
99+
expect(consoleErrorSpy).not.toHaveBeenCalled();
100+
});
101+
});
102+
103+
describe("when the logLevel configuration is set to 'OFF'", () => {
104+
it('does not log an error to the console', () => {
105+
config.set('logLevel', 'OFF');
106+
107+
printIonError('This is an error message');
108+
109+
expect(consoleErrorSpy).not.toHaveBeenCalled();
110+
});
111+
});
112+
});
113+
});

0 commit comments

Comments
 (0)