From db463fc85515cea9e83bb81e312f8f1687ffed94 Mon Sep 17 00:00:00 2001 From: Alexey Sudilovskiy Date: Thu, 24 Mar 2022 11:02:21 +0300 Subject: [PATCH 1/8] feat: extract `Rum` logger --- src/index.ts | 52 +++++++++++++---------------------------------- src/rum-logger.ts | 43 +++++++++++++++++++++++++++++++++++++++ src/types.ts | 4 ++++ 3 files changed, 61 insertions(+), 38 deletions(-) create mode 100644 src/rum-logger.ts 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..9dde58c --- /dev/null +++ b/src/rum-logger.ts @@ -0,0 +1,43 @@ +import {Logger} from "./types"; + +declare global { + interface Window { + Ya?: { + Rum?: { + logError: (arg?: any) => void; + ERROR_LEVEL: { + INFO: string; + }; + }; + }; + } +} + +const warnCache = new Set(); + +export const rumLogger: Logger = { + log(message, {level, logger, extra} = {}) { + 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..602641e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -55,3 +55,7 @@ export enum Plural { Many, None } + +export interface Logger { + log(message: string, extra?: {level?: string; logger?: string; extra?: Record}): void; +} From dd60f295706c178b76f55a742258e4a505e9c69e Mon Sep 17 00:00:00 2001 From: Alexey Sudilovskiy Date: Thu, 24 Mar 2022 11:06:37 +0300 Subject: [PATCH 2/8] chore: add deprecation warning --- src/rum-logger.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/rum-logger.ts b/src/rum-logger.ts index 9dde58c..c880055 100644 --- a/src/rum-logger.ts +++ b/src/rum-logger.ts @@ -17,6 +17,10 @@ const warnCache = new Set(); export const rumLogger: Logger = { log(message, {level, logger, extra} = {}) { + if (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; } From 59ec08b3e1e4d81bb682d4af02372dd7d3ba37ae Mon Sep 17 00:00:00 2001 From: Alexey Sudilovskiy Date: Thu, 24 Mar 2022 11:19:01 +0300 Subject: [PATCH 3/8] feat: add API docs --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index ede5710..309f63d 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 + * `extra` - 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` From 4ebca9ef8138cb10cf33209908732a8b916a240a Mon Sep 17 00:00:00 2001 From: Alexey Date: Sat, 26 Mar 2022 11:40:00 +0300 Subject: [PATCH 4/8] fix: `process` check --- src/rum-logger.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rum-logger.ts b/src/rum-logger.ts index c880055..e518172 100644 --- a/src/rum-logger.ts +++ b/src/rum-logger.ts @@ -17,7 +17,7 @@ const warnCache = new Set(); export const rumLogger: Logger = { log(message, {level, logger, extra} = {}) { - if (process?.env?.NODE_ENV === 'development') { + if (typeof process !== '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.'); } From df27a0b88e08f3e2b5333413b76e616e9e861573 Mon Sep 17 00:00:00 2001 From: Alexey Sudilovskiy Date: Mon, 28 Mar 2022 12:09:01 +0300 Subject: [PATCH 5/8] chore: add deprecation comment --- src/rum-logger.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/rum-logger.ts b/src/rum-logger.ts index e518172..1c818a6 100644 --- a/src/rum-logger.ts +++ b/src/rum-logger.ts @@ -15,6 +15,9 @@ declare global { const warnCache = new Set(); +/** + * @deprecated + */ export const rumLogger: Logger = { log(message, {level, logger, extra} = {}) { if (typeof process !== 'undefined' && process.env.NODE_ENV === 'development') { From 4ad20bc8084fe715f65caa4615876b4317d30834 Mon Sep 17 00:00:00 2001 From: Alexey Sudilovskiy Date: Mon, 28 Mar 2022 12:18:21 +0300 Subject: [PATCH 6/8] chore: change webpack detection --- src/rum-logger.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/rum-logger.ts b/src/rum-logger.ts index 1c818a6..d4f5729 100644 --- a/src/rum-logger.ts +++ b/src/rum-logger.ts @@ -1,5 +1,9 @@ +/* eslint camelcase: ['error', {allow: ['__webpack_require__']}] */ + import {Logger} from "./types"; +declare let __webpack_require__: unknown; + declare global { interface Window { Ya?: { @@ -20,7 +24,7 @@ const warnCache = new Set(); */ export const rumLogger: Logger = { log(message, {level, logger, extra} = {}) { - if (typeof process !== 'undefined' && process.env.NODE_ENV === 'development') { + 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.'); } From 2b702f8d0112b259847628a101752f9bef23ec0a Mon Sep 17 00:00:00 2001 From: Alexey Sudilovskiy Date: Fri, 1 Apr 2022 14:50:54 +0300 Subject: [PATCH 7/8] chore: copy --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 309f63d..3adfba7 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Accepts `options` object with optional `logger` that would be used for logging l Logger should have explicit `log` method with following signature: * `message` - string of message that would be logged - * `extra` - object of logging options: + * `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` From 4f81a11745197ab8e08b329a8e36191101f0ecd7 Mon Sep 17 00:00:00 2001 From: Alexey Date: Mon, 4 Apr 2022 16:15:55 +0300 Subject: [PATCH 8/8] chore: rename --- src/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types.ts b/src/types.ts index 602641e..c49e5c5 100644 --- a/src/types.ts +++ b/src/types.ts @@ -57,5 +57,5 @@ export enum Plural { } export interface Logger { - log(message: string, extra?: {level?: string; logger?: string; extra?: Record}): void; + log(message: string, options?: {level?: string; logger?: string; extra?: Record}): void; }