Skip to content

Commit

Permalink
feat: add logger option to constructor (#20)
Browse files Browse the repository at this point in the history
* extract `Rum` logger
* deprecate `Rum` logger
  • Loading branch information
ogonkov authored Apr 4, 2022
1 parent 0b5833b commit b135807
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 38 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
52 changes: 14 additions & 38 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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<string, string | string[]>;
type KeysetData = Record<string, KeysData>;

declare global {
interface Window {
Ya?: {
Rum?: {
logError: (arg?: any) => void;
ERROR_LEVEL: {
INFO: string;
};
};
};
}
}

export * from './types';

export class I18N {
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
});
}
}
54 changes: 54 additions & 0 deletions src/rum-logger.ts
Original file line number Diff line number Diff line change
@@ -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);
}
};
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,7 @@ export enum Plural {
Many,
None
}

export interface Logger {
log(message: string, options?: {level?: string; logger?: string; extra?: Record<string, unknown>}): void;
}

0 comments on commit b135807

Please sign in to comment.