Skip to content

Commit

Permalink
feat(logger): initOptions.debugOptionsFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
Wes Johnson (Arcadia) committed May 22, 2024
1 parent c8d9f7b commit ad43db7
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/i18next.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions test/runtime/i18next.debug.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
22 changes: 22 additions & 0 deletions test/runtime/i18next.debugFilter.test.js
Original file line number Diff line number Diff line change
@@ -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 });
});
});
});
6 changes: 6 additions & 0 deletions typescript/options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,12 @@ export interface InitOptions<T = object> extends PluginOptions<T> {
*/
debug?: boolean;

/**
* Filter function that allows changing the logged option value when `debug` is set to true.
* @default undefined
*/
debugOptionsFilter?: (options: InitOptions<T>) => unknown;

/**
* Resources to initialize with (if not using loading or not appending using addResourceBundle)
* @default undefined
Expand Down

0 comments on commit ad43db7

Please sign in to comment.