From ad43db7554769aef805a7d026ff25aad34abc540 Mon Sep 17 00:00:00 2001 From: "Wes Johnson (Arcadia)" Date: Tue, 21 May 2024 21:43:03 -0400 Subject: [PATCH] feat(logger): initOptions.debugOptionsFilter --- src/i18next.js | 7 ++++++- test/runtime/i18next.debug.test.js | 21 +++++++++++++++++++++ test/runtime/i18next.debugFilter.test.js | 22 ++++++++++++++++++++++ typescript/options.d.ts | 6 ++++++ 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 test/runtime/i18next.debug.test.js create mode 100644 test/runtime/i18next.debugFilter.test.js diff --git a/src/i18next.js b/src/i18next.js index 2873b6ce..a0a2a664 100644 --- a/src/i18next.js +++ b/src/i18next.js @@ -193,7 +193,12 @@ class I18n extends EventEmitter { this.isInitializing = false; if (this.isInitialized && !this.initializedStoreOnce) this.logger.warn('init: i18next is already initialized. You should call init just once!'); this.isInitialized = true; - if (!this.options.isClone) this.logger.log('initialized', this.options); + if (!this.options.isClone) { + this.logger.log( + 'initialized', + typeof this.options.debugOptionsFilter === 'function' ? this.options.debugOptionsFilter(this.options) : this.options + ); + } this.emit('initialized', this.options); deferred.resolve(t); // not rejecting on err (as err is only a loading translation failed warning) diff --git a/test/runtime/i18next.debug.test.js b/test/runtime/i18next.debug.test.js new file mode 100644 index 00000000..cd9d52cd --- /dev/null +++ b/test/runtime/i18next.debug.test.js @@ -0,0 +1,21 @@ +import { describe, it, expect, beforeAll, vi } from 'vitest'; +import i18next from '../../src/i18next.js'; +import logger from '../../src/logger.js'; + +describe('i18next debug', () => { + let logSpy; + beforeAll(async () => { + logSpy = vi.spyOn(logger, 'log'); + await i18next.init({ + foo: 'bar', + debug: true, + }); + i18next.changeLanguage('en'); + }); + + describe('init log options filtering', () => { + it('logs a subset of the options object', () => { + expect(logSpy).toHaveBeenCalledWith('initialized', i18next.options); + }); + }); +}); diff --git a/test/runtime/i18next.debugFilter.test.js b/test/runtime/i18next.debugFilter.test.js new file mode 100644 index 00000000..4e6609f5 --- /dev/null +++ b/test/runtime/i18next.debugFilter.test.js @@ -0,0 +1,22 @@ +import { describe, it, expect, beforeAll, vi } from 'vitest'; +import i18next from '../../src/i18next.js'; +import logger from '../../src/logger.js'; + +describe('i18next debug', () => { + let logSpy; + beforeAll(async () => { + logSpy = vi.spyOn(logger, 'log'); + await i18next.init({ + foo: 'bar', + debug: true, + debugOptionsFilter: (opt) => ({ namespaces: opt.ns.length }), + }); + i18next.changeLanguage('en'); + }); + + describe('init log options filtering', () => { + it('logs a subset of the options object', () => { + expect(logSpy).toHaveBeenCalledWith('initialized', { namespaces: 1 }); + }); + }); +}); diff --git a/typescript/options.d.ts b/typescript/options.d.ts index f1646edd..5aab9be6 100644 --- a/typescript/options.d.ts +++ b/typescript/options.d.ts @@ -350,6 +350,12 @@ export interface InitOptions extends PluginOptions { */ debug?: boolean; + /** + * Filter function that allows changing the logged option value when `debug` is set to true. + * @default undefined + */ + debugOptionsFilter?: (options: InitOptions) => unknown; + /** * Resources to initialize with (if not using loading or not appending using addResourceBundle) * @default undefined