Skip to content

Commit 3ccd86b

Browse files
authored
🔀 Merge pull request #706 from FrostCo/feature/logging-methods
New pattern for logging
2 parents f960ad5 + 453301a commit 3ccd86b

File tree

9 files changed

+122
-65
lines changed

9 files changed

+122
-65
lines changed

src/script/Background.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export default class Background {
4141
this.COLOR_BLUE_VIOLET,
4242
this.COLOR_FOREST_GREEN,
4343
] as chrome.action.ColorArray[];
44-
static readonly LOGGER = new Logger('Background');
44+
static readonly log = new Logger('Background', this.Constants.LOGGING_LEVELS.INFO);
4545
// #endregion
4646

4747
// #region Static Methods
@@ -159,7 +159,7 @@ export default class Background {
159159
tabOptions.disabledOnce = this.Constants.TAB_DISABLE_ONCE.WILL_DISABLE;
160160
await this.saveBackgroundStorage(storage);
161161
chrome.tabs.reload(tabId);
162-
this.LOGGER.info('disabling tab once.', tabId);
162+
this.log.info('disabling tab once.', tabId);
163163
}
164164

165165
static getTabOptions(storage: BackgroundStorage, tabId: number): TabStorageOptions {
@@ -229,7 +229,7 @@ export default class Background {
229229
static async handleUpdate(details: chrome.runtime.InstalledDetails) {
230230
this.contextMenuSetup();
231231
const thisVersion = chrome.runtime.getManifest().version;
232-
this.LOGGER.info(`Updated from ${details.previousVersion} to ${thisVersion}.`);
232+
this.log.info(`Updated from ${details.previousVersion} to ${thisVersion}.`);
233233

234234
// Run any data migrations on update
235235
this.runUpdateMigrations(details.previousVersion);
@@ -250,7 +250,7 @@ export default class Background {
250250
}
251251
}
252252
} catch (err) {
253-
this.LOGGER.warn('Error while displaying update notification', err);
253+
this.log.warn('Error while displaying update notification', err);
254254
}
255255
}
256256

@@ -314,7 +314,7 @@ export default class Background {
314314
if (request.updateContextMenus != null) {
315315
this.contextMenuSetup();
316316
} else {
317-
this.LOGGER.error('Received unhandled message.', JSON.stringify(request));
317+
this.log.error('Received unhandled message.', JSON.stringify(request));
318318
}
319319
break;
320320

@@ -329,12 +329,12 @@ export default class Background {
329329
this.toggleTabDisable(request.tabId, false, true, sendResponse);
330330
return true; // return true when waiting on an async call
331331
} else {
332-
this.LOGGER.error('Received unhandled message.', JSON.stringify(request));
332+
this.log.error('Received unhandled message.', JSON.stringify(request));
333333
}
334334
break;
335335

336336
default:
337-
this.LOGGER.error('Received message without a supported source:', JSON.stringify(request));
337+
this.log.error('Received message without a supported source:', JSON.stringify(request));
338338
}
339339

340340
sendResponse(); // Issue 393 - Chrome 99+ promisified sendMessage expects callback to be called
@@ -381,7 +381,7 @@ export default class Background {
381381
await cfg.save('words');
382382
chrome.tabs.reload();
383383
} catch (err) {
384-
this.LOGGER.errorTime(`Failed to process selection '${selection}'.`, err);
384+
this.log.errorTime(`Failed to process selection '${selection}'.`, err);
385385
}
386386
}
387387
}
@@ -448,7 +448,7 @@ export default class Background {
448448
await domain.save(cfg);
449449
chrome.tabs.reload();
450450
} catch (err) {
451-
this.LOGGER.error(`Failed to modify '${action}' for domain '${domain.cfgKey}'.`, err, domain);
451+
this.log.error(`Failed to modify '${action}' for domain '${domain.cfgKey}'.`, err, domain);
452452
}
453453
}
454454

src/script/OptionPage.ts

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ export default class OptionPage {
6868
}
6969
//#endregion
7070

71+
static get log() {
72+
return logger;
73+
}
74+
7175
static readonly activeClass = 'w3-flat-belize-hole';
7276
static readonly themeElementSelectors = ['body', 'div#page', 'div.w3-modal'];
7377

@@ -86,6 +90,10 @@ export default class OptionPage {
8690
this.filter = new this.Class.Filter();
8791
}
8892

93+
get log() {
94+
return this.Class.log;
95+
}
96+
8997
async addWordlist() {
9098
const wordlistText = document.getElementById('wordlistText') as HTMLInputElement;
9199
const name = wordlistText.value.trim();
@@ -705,7 +713,7 @@ export default class OptionPage {
705713
if (this.isStorageError(err) && this.cfg.syncLargeKeys) {
706714
this.confirm('bulkEditorSaveRetry');
707715
} else {
708-
logger.warn(this.t('options:bulkWordEditorModal.messages.saveFailed'), err);
716+
this.log.warn(this.t('options:bulkWordEditorModal.messages.saveFailed'), err);
709717
this.showErrorModal([this.t('options:.bulkWordEditor.messages.saveFailed'), `Error: ${err.message}`]);
710718
}
711719
}
@@ -1055,10 +1063,10 @@ export default class OptionPage {
10551063

10561064
handleError(message: string, error?: Error) {
10571065
if (error) {
1058-
logger.error(message, error);
1066+
this.log.error(message, error);
10591067
this.showErrorModal([message, `Error: ${error.message}`]);
10601068
} else {
1061-
logger.error(message);
1069+
this.log.error(message);
10621070
this.showErrorModal([message]);
10631071
}
10641072
}
@@ -1173,14 +1181,14 @@ export default class OptionPage {
11731181
await this.initializeCfg();
11741182
await this.translation.changeLanguage(this.cfg.language);
11751183
this.applyTranslation();
1176-
logger.setLevel(this.cfg.loggingLevel);
1184+
this.log.setLevel(this.cfg.loggingLevel);
11771185
this.applyTheme(refreshTheme);
11781186
this.setHelpVersion();
11791187
if (!this.auth) this.auth = new this.Class.OptionAuth(this, this.cfg.password);
11801188
this.filter.cfg = this.cfg;
11811189
this.filter.init();
11821190

1183-
// logger.debug(`Password: '${this.cfg.password}', Authenticated: ${this.auth.authenticated}`);
1191+
// this.log.debug(`Password: '${this.cfg.password}', Authenticated: ${this.auth.authenticated}`);
11841192
if (this.cfg.password && !this.auth.authenticated) {
11851193
this.openModal('passwordModal');
11861194
document.getElementById('passwordInput').focus();
@@ -1434,7 +1442,7 @@ export default class OptionPage {
14341442

14351443
this.populateStatsSummary(stats, totalFiltered);
14361444
} catch (err) {
1437-
logger.warn(this.t('options:statsPage.messages.loadFailed'), err);
1445+
this.log.warn(this.t('options:statsPage.messages.loadFailed'), err);
14381446
this.showErrorModal([this.t('options:statsPage.messages.loadFailed'), `Error: ${err.message}`]);
14391447
}
14401448
}
@@ -1726,7 +1734,7 @@ export default class OptionPage {
17261734
}
17271735
}
17281736
} catch (err) {
1729-
logger.warn(this.t('options:statsPage.lessUsedWords.messages.prepareLessUsedWordsError'), err);
1737+
this.log.warn(this.t('options:statsPage.lessUsedWords.messages.prepareLessUsedWordsError'), err);
17301738
return {};
17311739
}
17321740
}
@@ -1749,7 +1757,7 @@ export default class OptionPage {
17491757
await this.cfg.save('domains');
17501758
this.populateDomainPage();
17511759
} catch (err) {
1752-
logger.warn(this.t('options:domainsPage.removeFailed', { domain: domainsSelect.value }), err);
1760+
this.log.warn(this.t('options:domainsPage.removeFailed', { domain: domainsSelect.value }), err);
17531761
this.showErrorModal([
17541762
this.t('options:domainsPage.removeFailed', { domain: domainsSelect.value }),
17551763
`Error: ${err.message}`,
@@ -1780,7 +1788,7 @@ export default class OptionPage {
17801788
this.filter.init();
17811789
this.populateOptions();
17821790
} catch (err) {
1783-
logger.warn(this.t('options:listPage.messages.removeWordFromAllowlistFailed', { word: originalWord }), err);
1791+
this.log.warn(this.t('options:listPage.messages.removeWordFromAllowlistFailed', { word: originalWord }), err);
17841792
this.showErrorModal([
17851793
this.t('options:listPage.messages.removeWordFromAllowlistFailed', { word: originalWord }),
17861794
`Error: ${err.message}`,
@@ -1804,7 +1812,7 @@ export default class OptionPage {
18041812
this.filter.rebuildWordlists();
18051813
this.populateOptions();
18061814
} catch (err) {
1807-
logger.warn(this.t('options:wordsPage.messages.removeFailed', { word: word }), err);
1815+
this.log.warn(this.t('options:wordsPage.messages.removeFailed', { word: word }), err);
18081816
this.showErrorModal([
18091817
this.t('options:wordsPage.messages.removeFailed', { word: word }),
18101818
`Error: ${err.message}`,
@@ -1838,7 +1846,7 @@ export default class OptionPage {
18381846
this.populateWordlists();
18391847
this.populateWordPage();
18401848
} catch (err) {
1841-
logger.warn(this.t('options:listsPage.messages.removeWordlistFailed', { wordlist: wordlist }), err);
1849+
this.log.warn(this.t('options:listsPage.messages.removeWordlistFailed', { wordlist: wordlist }), err);
18421850
this.showErrorModal([
18431851
this.t('options:listsPage.messages.removeWordlistFailed', { wordlist: wordlist }),
18441852
`Error: ${err.message}`,
@@ -1883,7 +1891,7 @@ export default class OptionPage {
18831891
await this.init(true);
18841892
return true;
18851893
} catch (err) {
1886-
logger.warn(this.t('options:configsPage.messages.restoreDefaultsFailed'), err);
1894+
this.log.warn(this.t('options:configsPage.messages.restoreDefaultsFailed'), err);
18871895
this.showErrorModal([this.t('options:configsPage.messages.restoreDefaultsFailed'), `Error: ${err.message}`]);
18881896
return false;
18891897
}
@@ -1939,7 +1947,7 @@ export default class OptionPage {
19391947
await this.init();
19401948
return true;
19411949
} catch (err) {
1942-
logger.warn(this.t('options:statusModal.messages.saveOptionsFailed'), err);
1950+
this.log.warn(this.t('options:statusModal.messages.saveOptionsFailed'), err);
19431951
this.showErrorModal([this.t('options:statusModal.messages.saveOptionsFailed'), `Error: ${err.message}`]);
19441952
return false;
19451953
}
@@ -1995,7 +2003,7 @@ export default class OptionPage {
19952003
this.filter.init();
19962004
this.populateOptions();
19972005
} catch (err) {
1998-
logger.warn(this.t('options:listsPage.messages.saveAllowlistFailed'), err);
2006+
this.log.warn(this.t('options:listsPage.messages.saveAllowlistFailed'), err);
19992007
this.showErrorModal([this.t('options:listsPage.messages.saveAllowlistFailed'), `Error: ${err.message}`]);
20002008
return false;
20012009
}
@@ -2091,18 +2099,18 @@ export default class OptionPage {
20912099

20922100
if (wordList.value === '') {
20932101
// New record
2094-
logger.info(`Adding new word: '${word}'.`, wordOptions);
2102+
this.log.info(`Adding new word: '${word}'.`, wordOptions);
20952103
added = this.cfg.addWord(word, wordOptions);
20962104
} else {
20972105
// Updating existing record
20982106
const originalWord = wordList.value;
20992107
if (originalWord == word) {
21002108
// Word options changed
2101-
logger.info(`Modifying existing word options for '${word}'.`, wordOptions);
2109+
this.log.info(`Modifying existing word options for '${word}'.`, wordOptions);
21022110
this.cfg.words[word] = wordOptions;
21032111
} else {
21042112
// Existing word modified
2105-
logger.info(`Rename existing word '${originalWord}' to '${word}'.`, wordOptions);
2113+
this.log.info(`Rename existing word '${originalWord}' to '${word}'.`, wordOptions);
21062114
added = this.cfg.addWord(word, wordOptions);
21072115
if (added) {
21082116
delete this.cfg.words[originalWord];
@@ -2119,7 +2127,7 @@ export default class OptionPage {
21192127
this.filter.rebuildWordlists();
21202128
this.populateOptions();
21212129
} catch (err) {
2122-
logger.warn(this.t('options:wordsPage.messages.updateFailed', { word: word }), err);
2130+
this.log.warn(this.t('options:wordsPage.messages.updateFailed', { word: word }), err);
21232131
this.showErrorModal([
21242132
this.t('options:wordsPage.messages.updateFailed', { word: word }),
21252133
`Error: ${err.message}`,
@@ -2526,7 +2534,7 @@ export default class OptionPage {
25262534
await this.Class.Config.removeLocalStorage('stats');
25272535
this.populateStats();
25282536
} catch (err) {
2529-
logger.warn(this.t('options:statsPage.messages.resetFailed'), err);
2537+
this.log.warn(this.t('options:statsPage.messages.resetFailed'), err);
25302538
this.showErrorModal([this.t('options:statsPage.messages.resetFailed'), `Error: ${err.message}`]);
25312539
}
25322540
}

src/script/Popup.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ export default class Popup {
4444
}
4545
//#endregion
4646

47+
static get log() {
48+
return logger;
49+
}
50+
4751
static readonly _requiredConfig = [
4852
'darkMode',
4953
'domains',
@@ -74,7 +78,7 @@ export default class Popup {
7478

7579
static async load(instance: Popup) {
7680
instance.cfg = await this.Config.load(this._requiredConfig);
77-
logger.setLevel(instance.cfg.loggingLevel);
81+
this.log.setLevel(instance.cfg.loggingLevel);
7882
instance.tab = (await this.Domain.getCurrentTab()) as chrome.tabs.Tab;
7983
if (instance.tab.url) {
8084
instance.url = new URL(instance.tab.url);
@@ -97,7 +101,7 @@ export default class Popup {
97101
// Handle service worker availability for Firefox for Android
98102
chrome.runtime.sendMessage(statusMessage, () => {
99103
if (chrome.runtime.lastError) {
100-
logger.debug('Service worker not ready for status request');
104+
this.log.debug('Service worker not ready for status request');
101105
}
102106
});
103107

@@ -121,6 +125,10 @@ export default class Popup {
121125
this.themeElements = [document.body, document.getElementById('footer')];
122126
}
123127

128+
get log() {
129+
return this.Class.log;
130+
}
131+
124132
applyDarkTheme() {
125133
document.documentElement.style.setProperty('color-scheme', 'dark');
126134
const summaryTable = document.querySelector('#summary > table') as HTMLTableElement;
@@ -215,7 +223,7 @@ export default class Popup {
215223
chrome.tabs.reload();
216224
this.populateOptions();
217225
} catch (err) {
218-
logger.error('Failed to update selected filter method.', err);
226+
this.log.error('Failed to update selected filter method.', err);
219227
}
220228
}
221229

@@ -293,7 +301,7 @@ export default class Popup {
293301
} else if (request.status) {
294302
this.updateStatus(request.status);
295303
} else {
296-
logger.error('Received unhandled message.', JSON.stringify(request));
304+
this.log.error('Received unhandled message.', JSON.stringify(request));
297305
}
298306

299307
sendResponse(); // Issue 393 - Chrome 99+ promisified sendMessage expects callback to be called
@@ -487,7 +495,7 @@ export default class Popup {
487495
chrome.tabs.reload();
488496
this.populateOptions();
489497
} catch (err) {
490-
logger.error(`Failed to toggle domain '${this.domain.hostname}'.`, err);
498+
this.log.error(`Failed to toggle domain '${this.domain.hostname}'.`, err);
491499
}
492500
}
493501
}
@@ -501,7 +509,7 @@ export default class Popup {
501509
chrome.tabs.reload();
502510
this.populateOptions();
503511
} catch (err) {
504-
logger.error(`Failed to update mode for domain '${this.domain.hostname}'.`, err);
512+
this.log.error(`Failed to update mode for domain '${this.domain.hostname}'.`, err);
505513
}
506514
}
507515
}
@@ -527,7 +535,7 @@ export default class Popup {
527535
chrome.tabs.reload();
528536
this.populateOptions();
529537
} catch (err) {
530-
logger.error(`Failed to select wordlist for domain ${this.domain.hostname}.`, err);
538+
this.log.error(`Failed to select wordlist for domain ${this.domain.hostname}.`, err);
531539
}
532540
}
533541

0 commit comments

Comments
 (0)