Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(logger): initOptions.debugOptionsFilter #2197

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-it('logs a subset of the options object', () => {
+it('logs 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but only for the log statement while init()... maybe the function name needs to respect this?

* @default undefined
*/
debugOptionsFilter?: (options: InitOptions<T>) => unknown;

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