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: deprecate Rum logger #20

Merged
merged 8 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
* `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`
ogonkov marked this conversation as resolved.
Show resolved Hide resolved

### 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, extra?: {level?: string; logger?: string; extra?: Record<string, unknown>}): void;
}