diff --git a/README.md b/README.md index ede5710..3adfba7 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,22 @@ Utilities in the I18N package are designed for internationalization of Yandex Cl `npm install --save @yandex-cloud/i18n` +### API + +#### constructor(options) + +Accepts `options` object with optional `logger` that would be used for logging library warnings. + +##### logger + +Logger should have explicit `log` method with following signature: + + * `message` - string of message that would be logged + * `options` - object of logging options: + * `level` - level for logging message, always `'info'` + * `logger` - where to log library messages + * `extra` - additional options object, with a single `type` string, that is always `i18n` + ### Use examples #### `keysets/en.json` diff --git a/src/index.ts b/src/index.ts index b31ea77..95bad7b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,25 +1,11 @@ import {pluralize} from "./pluralize"; import {replaceParams} from "./replace-params"; -import {Params, Plural} from "./types"; - -const warnCache = new Set(); +import {rumLogger} from "./rum-logger"; +import {Logger, Params, Plural} from "./types"; type KeysData = Record; type KeysetData = Record; -declare global { - interface Window { - Ya?: { - Rum?: { - logError: (arg?: any) => void; - ERROR_LEVEL: { - INFO: string; - }; - }; - }; - } -} - export * from './types'; export class I18N { @@ -46,6 +32,12 @@ export class I18N { lang: string | undefined = undefined; + logger: Logger | null = null; + + constructor(options: {logger?: Logger} = {logger: rumLogger}) { + this.logger = options?.logger || null; + } + setLang(lang: string) { if (I18N.LANGS[lang]) { this.lang = lang; @@ -189,28 +181,12 @@ export class I18N { cacheKey = 'languageData'; } - - if (!warnCache.has(cacheKey)) { - console.warn(`[i18n][${cacheKey}] ${msg}`); - - if (typeof window !== 'undefined' - && window.Ya - && window.Ya.Rum - && typeof window.Ya.Rum.logError === 'function') - { - try { - window.Ya.Rum.logError({ - message: `I18n: ${msg}`, - type: 'i18n', - level: window.Ya.Rum.ERROR_LEVEL.INFO, - block: cacheKey, - }); - } catch (err) { - console.error(err); - } + this.logger?.log(`I18n: ${msg}`, { + level: 'info', + logger: cacheKey, + extra: { + type: 'i18n' } - - warnCache.add(cacheKey); - } + }); } } diff --git a/src/rum-logger.ts b/src/rum-logger.ts new file mode 100644 index 0000000..d4f5729 --- /dev/null +++ b/src/rum-logger.ts @@ -0,0 +1,54 @@ +/* eslint camelcase: ['error', {allow: ['__webpack_require__']}] */ + +import {Logger} from "./types"; + +declare let __webpack_require__: unknown; + +declare global { + interface Window { + Ya?: { + Rum?: { + logError: (arg?: any) => void; + ERROR_LEVEL: { + INFO: string; + }; + }; + }; + } +} + +const warnCache = new Set(); + +/** + * @deprecated + */ +export const rumLogger: Logger = { + log(message, {level, logger, extra} = {}) { + if (typeof __webpack_require__ !== 'undefined' && process.env.NODE_ENV === 'development') { + console.log('@yandex-cloud/i18n: default logger is deprecated, and would be removed in future. Consult docs for alternative.'); + } + + if (typeof window === 'undefined' || typeof window.Ya?.Rum?.logError !== 'function') { + return; + } + + if (warnCache.has(logger)) { + return; + } + + console.warn(`[${extra?.type}][${logger || ''}] ${message}`); + + try { + window.Ya.Rum.logError({ + message, + type: extra?.type, + level: level === 'info' ? window.Ya.Rum.ERROR_LEVEL.INFO : undefined, + block: logger + }); + } catch (err) { + console.error(err); + } + + warnCache.add(logger); + } +}; diff --git a/src/types.ts b/src/types.ts index 13021a3..c49e5c5 100644 --- a/src/types.ts +++ b/src/types.ts @@ -55,3 +55,7 @@ export enum Plural { Many, None } + +export interface Logger { + log(message: string, options?: {level?: string; logger?: string; extra?: Record}): void; +}