Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/logger/browser/debugLogger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Logger } from '../index';
import { codesError } from '../messages/error';
import { codesWarn } from '../messages/warn';
import { codesInfo } from '../messages/info';
import { codesDebug } from '../messages/debug';
import { codesDebugBrowser } from '../messages/debugBrowser';
import { _Map } from '../../utils/lang/maps';

export const debugLogger = new Logger(
'splitio', { logLevel: 'DEBUG' },
new _Map(codesError.concat(codesWarn, codesInfo, codesDebug, codesDebugBrowser))
);
8 changes: 8 additions & 0 deletions src/logger/browser/errorLogger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Logger } from '../index';
import { codesError } from '../messages/error';
import { _Map } from '../../utils/lang/maps';

export const errorLogger = new Logger(
'splitio', { logLevel: 'ERROR' },
new _Map(codesError)
);
10 changes: 10 additions & 0 deletions src/logger/browser/infoLogger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Logger } from '../index';
import { codesError } from '../messages/error';
import { codesWarn } from '../messages/warn';
import { codesInfo } from '../messages/info';
import { _Map } from '../../utils/lang/maps';

export const infoLogger = new Logger(
'splitio', { logLevel: 'INFO' },
new _Map(codesError.concat(codesWarn, codesInfo))
);
9 changes: 9 additions & 0 deletions src/logger/browser/warnLogger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Logger } from '../index';
import { codesError } from '../messages/error';
import { codesWarn } from '../messages/warn';
import { _Map } from '../../utils/lang/maps';

export const warnLogger = new Logger(
'splitio', { logLevel: 'WARN' },
new _Map(codesError.concat(codesWarn))
);
272 changes: 136 additions & 136 deletions src/logger/constants.ts

Large diffs are not rendered by default.

23 changes: 15 additions & 8 deletions src/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import objectAssign from 'object-assign';
import { ILoggerOptions, ILogger } from './types';
import { find } from '../utils/lang';
import { LogLevel } from '../types';
import { IMap, _Map } from '../utils/lang/maps';

export const LogLevels: { [level: string]: LogLevel } = {
DEBUG: 'DEBUG',
Expand Down Expand Up @@ -31,36 +32,42 @@ export class Logger implements ILogger {

private category: string;
private options: Required<ILoggerOptions>;
private codes: IMap<number, string>;

constructor(category: string, options?: ILoggerOptions) {
constructor(category: string, options?: ILoggerOptions, codes?: IMap<number, string>) {
this.category = category;
this.options = objectAssign({}, defaultOptions, options);
this.codes = codes || new _Map();
}

setLogLevel(logLevel: LogLevel) {
this.options.logLevel = logLevel;
}

debug(msg: string, args?: any[]) {
debug(msg: string | number, args?: any[]) {
this._log(LogLevels.DEBUG, msg, args);
}

info(msg: string, args?: any[]) {
info(msg: string | number, args?: any[]) {
this._log(LogLevels.INFO, msg, args);
}

warn(msg: string, args?: any[]) {
warn(msg: string | number, args?: any[]) {
this._log(LogLevels.WARN, msg, args);
}

error(msg: string, args?: any[]) {
error(msg: string | number, args?: any[]) {
this._log(LogLevels.ERROR, msg, args);
}

private _log(level: LogLevel, text: string, args?: any[]) {
private _log(level: LogLevel, msg: string | number, args?: any[]) {
if (this._shouldLog(level)) {
if (args) text = sprintf(text, args);
const formattedText = this._generateLogMessage(level, text);
if (typeof msg === 'number') {
const format = this.codes.get(msg);
if (format) msg = sprintf(format, args);
else msg = `Message code ${msg}${args ? ', with args: ' + args.toString() : ''}`;
}
const formattedText = this._generateLogMessage(level, msg);

console.log(formattedText);
}
Expand Down
44 changes: 44 additions & 0 deletions src/logger/messages/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { DEBUG_0, DEBUG_1, DEBUG_2, DEBUG_3, DEBUG_4, DEBUG_5, DEBUG_6, DEBUG_7, DEBUG_8, DEBUG_9, DEBUG_10, DEBUG_11, DEBUG_12, DEBUG_13, DEBUG_14, DEBUG_15, DEBUG_16, DEBUG_17, DEBUG_18, DEBUG_19, DEBUG_20, DEBUG_21, DEBUG_22, DEBUG_23, DEBUG_24, DEBUG_25, DEBUG_31, DEBUG_32, DEBUG_33, DEBUG_36, DEBUG_42, DEBUG_43, DEBUG_44, DEBUG_45, DEBUG_46, DEBUG_47, DEBUG_48, DEBUG_49, DEBUG_50, DEBUG_51 } from '../constants';

export const codesDebug: [number, string][] = [
[DEBUG_0, 'splitio-engine:combiner => [andCombiner] evaluates to %s'],
[DEBUG_1, 'splitio-engine:combiner => Treatment found: %s'],
[DEBUG_2, 'splitio-engine:combiner => All predicates evaluated, no treatment found.'],
[DEBUG_3, 'splitio-engine => [engine] using algo "murmur" bucket %s for key %s using seed %s - treatment %s'],
[DEBUG_4, 'splitio-engine:matcher => [allMatcher] is always true'],
[DEBUG_5, 'splitio-engine:matcher => [betweenMatcher] is %s between %s and %s? %s'],
[DEBUG_6, 'splitio-engine:matcher => [booleanMatcher] %s === %s'],
[DEBUG_7, 'splitio-engine:matcher => [containsAllMatcher] %s contains all elements of %s? %s'],
[DEBUG_8, 'splitio-engine:matcher => [containsAnyMatcher] %s contains at least an element of %s? %s'],
[DEBUG_9, 'splitio-engine:matcher => [containsStringMatcher] %s contains %s? %s'],
[DEBUG_10, 'splitio-engine:matcher => [dependencyMatcher] Parent split "%s" evaluated to "%s" with label "%s". %s evaluated treatment is part of [%s] ? %s.'],
[DEBUG_11, 'splitio-engine:matcher => [dependencyMatcher] will evaluate parent split: "%s" with key: %s %s'],
[DEBUG_12, 'splitio-engine:matcher => [equalToMatcher] is %s equal to %s? %s'],
[DEBUG_13, 'splitio-engine:matcher => [equalToSetMatcher] is %s equal to set %s? %s'],
[DEBUG_14, 'splitio-engine:matcher => [endsWithMatcher] %s ends with %s? %s'],
[DEBUG_15, 'splitio-engine:matcher => [greaterThanEqualMatcher] is %s greater than %s? %s'],
[DEBUG_16, 'splitio-engine:matcher => [lessThanEqualMatcher] is %s less than %s? %s'],
[DEBUG_17, 'splitio-engine:matcher => [partOfMatcher] %s is part of %s? %s'],
[DEBUG_18, 'splitio-engine:matcher => [asyncSegmentMatcher] evaluated %s / %s => %s'],
[DEBUG_19, 'splitio-engine:matcher => [segmentMatcher] evaluated %s / %s => %s'],
[DEBUG_20, 'splitio-engine:matcher => [stringMatcher] does %s matches with %s? %s'],
[DEBUG_21, 'splitio-engine:matcher => [stringMatcher] %s is an invalid regex'],
[DEBUG_22, 'splitio-engine:matcher => [startsWithMatcher] %s starts with %s? %s'],
[DEBUG_23, 'splitio-engine:matcher => [whitelistMatcher] evaluated %s in [%s] => %s'],
[DEBUG_24, 'splitio-engine:value => Extracted attribute [%s], [%s] will be used for matching.'],
[DEBUG_25, 'splitio-engine:sanitize => Attempted to sanitize [%s] which should be of type [%s]. \n Sanitized and processed value => [%s]'],
[DEBUG_31, 'splitio => Retrieving SDK client.'],
[DEBUG_32, 'splitio => Retrieving default SDK client.'], // @TODO remove and use 'splitio => Retrieving SDK client.'
[DEBUG_33, 'splitio => Retrieving existing SDK client.'],
[DEBUG_36, 'splitio-producer:offline => Splits data: '],
[DEBUG_42, 'splitio-sync:split-changes => Spin up split update using since = %s'],
[DEBUG_43, 'splitio-sync:split-changes => New splits %s'],
[DEBUG_44, 'splitio-sync:split-changes => Removed splits %s'],
[DEBUG_45, 'splitio-sync:split-changes => Segment names collected %s'],
[DEBUG_46, 'splitio-sync:sse-handler => New SSE message received, with data: %s.'],
[DEBUG_47, 'splitio-sync:task => Starting %s. Running each %s millis'],
[DEBUG_48, 'splitio-sync:task => Running %s'],
[DEBUG_49, 'splitio-sync:task => Stopping %s'],
[DEBUG_50, 'splitio-client:impressions-tracker => Successfully stored %s impression%s.'],
[DEBUG_51, 'Factory instantiation: splits filtering criteria is "%s".']
];
6 changes: 6 additions & 0 deletions src/logger/messages/debugBrowser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { DEBUG_26, DEBUG_27 } from '../constants';

export const codesDebugBrowser: [number, string][] = [
[DEBUG_26, 'splitio-client:cleanup => Registering flush handler when unload page event is triggered.'],
[DEBUG_27, 'splitio-client:cleanup => Deregistering flush handler when unload page event is triggered.'],
];
14 changes: 14 additions & 0 deletions src/logger/messages/debugNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { DEBUG_28, DEBUG_29, DEBUG_30, DEBUG_39, DEBUG_40, DEBUG_41, DEBUG_34, DEBUG_35, DEBUG_37, DEBUG_38 } from '../constants';

export const codesDebugNode: [number, string][] = [
[DEBUG_28, 'splitio-client:cleanup => Registering cleanup handlers.'],
[DEBUG_29, 'splitio-client:cleanup => Deregistering cleanup handlers.'],
[DEBUG_30, 'splitio-client:cleanup => Split SDK graceful shutdown after SIGTERM.'],
[DEBUG_39, 'splitio-sync:segment-changes => Processed %s with till = %s. Added: %s. Removed: %s'],
[DEBUG_40, 'splitio-sync:segment-changes => Processing segment %s'],
[DEBUG_41, 'splitio-sync:segment-changes => Started segments update'],
[DEBUG_34, 'splitio-offline:splits-fetcher => Ignoring empty line or comment at #%s'],
[DEBUG_35, 'splitio-offline:splits-fetcher => Ignoring line since it does not have exactly two columns #%s'],
[DEBUG_37, 'splitio-sync:polling-manager => Splits will be refreshed each %s millis'], // @TODO remove since we already log it in syncTask debug log?
[DEBUG_38, 'splitio-sync:polling-manager => Segments will be refreshed each %s millis'], // @TODO remove since we already log it in syncTask debug log?
];
37 changes: 37 additions & 0 deletions src/logger/messages/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { ERROR_0, ERROR_2, ERROR_3, ERROR_4, ERROR_5, ERROR_7, ERROR_9, ERROR_10, ERROR_11, ERROR_12, ERROR_13, ERROR_14, ERROR_15, ERROR_16, ERROR_17, ERROR_18, ERROR_19, ERROR_20, ERROR_21, ERROR_22, ERROR_23, ERROR_24, ERROR_25, ERROR_26, ERROR_32, ERROR_33, ERROR_34, ERROR_35, ERROR_36, ERROR_37, ERROR_38, ERROR_39, ERROR_API_KEY } from '../constants';

export const codesError: [number, string][] = [
[ERROR_0, 'splitio-engine:combiner => Invalid Split provided, no valid conditions found'],
[ERROR_2, 'splitio-utils:logger => Invalid Log Level - No changes to the logs will be applied.'],
[ERROR_3, 'A listener was added for %s on the SDK, which has already fired and won\'t be emitted again. The callback won\'t be executed.'],
[ERROR_4, 'splitio => Manager instance is not available. Provide the manager module on settings.'],
[ERROR_5, 'splitio-services:service => %s The SDK will not get ready.'],
[ERROR_7, 'splitio-producer:offline => There was an issue loading the mock Splits data, no changes will be applied to the current cache. %s'],
[ERROR_9, 'splitio-sync:sse-handler => Fail to connect to streaming, with error message: %s'],
[ERROR_10, 'splitio-sync:push-manager => Failed to authenticate for streaming. Error: "%s".'],
[ERROR_11, 'splitio-client:impressions-tracker => Could not store impressions bulk with %s impression%s. Error: %s'],
[ERROR_12, 'splitio-client:impressions-tracker => Impression listener logImpression method threw: %s.'],
[ERROR_13, '%s: attributes must be a plain object.'],
[ERROR_14, '%s: you passed "%s", event_type must adhere to the regular expression /^[a-zA-Z0-9][-_.:a-zA-Z0-9]{0,79}$/g. This means an event_type must be alphanumeric, cannot be more than 80 characters long, and can only include a dash, underscore, period, or colon as separators of alphanumeric characters.'],
[ERROR_15, '%s: you passed a null or undefined event_type, event_type must be a non-empty string.'],
[ERROR_16, '%s: you passed an invalid event_type, event_type must be a non-empty string.'],
[ERROR_17, '%s: you passed an empty event_type, event_type must be a non-empty string.'],
[ERROR_18, '%s: properties must be a plain object.'],
[ERROR_19, '%s: The maximum size allowed for the properties is 32768 bytes, which was exceeded. Event not queued.'],
[ERROR_20, '%s: value must be a finite number.'],
[ERROR_21, 'Client has already been destroyed - no calls possible.'],
[ERROR_22, '%s: you passed a null or undefined %s, %s must be a non-empty string.'],
[ERROR_23, '%s: %s too long, %s must be 250 characters or less.'],
[ERROR_24, '%s: you passed an invalid %s type, %s must be a non-empty string.'],
[ERROR_25, '%s: you passed an empty string, %s must be a non-empty string.'],
[ERROR_26, '%s: Key must be an object with bucketingKey and matchingKey with valid string properties.'],
[ERROR_32, '%s: you passed an invalid %s, %s must be a non-empty string.'],
[ERROR_33, '%s: you passed an empty %s, %s must be a non-empty string.'],
[ERROR_34, '%s: %s must be a non-empty array.'],
[ERROR_35, '%s: you passed a null or undefined traffic_type_name, traffic_type_name must be a non-empty string.'],
[ERROR_36, '%s: you passed an invalid traffic_type_name, traffic_type_name must be a non-empty string.'],
[ERROR_37, '%s: you passed an empty traffic_type_name, traffic_type_name must be a non-empty string.'],
[ERROR_38, 'splitio-settings => You passed an invalid impressionsMode, impressionsMode should be one of the following values: "%s" or "%s". Defaulting to "%s" mode.'],
[ERROR_39, 'Response status is not OK. Status: %s. URL: %s. Message: %s'],
[ERROR_API_KEY, 'Factory instantiation: %s, api_key must be a non-empty string']
];
7 changes: 7 additions & 0 deletions src/logger/messages/errorNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ERROR_1, ERROR_8, ERROR_6 } from '../constants';

export const codesErrorNode: [number, string][] = [
[ERROR_1, 'splitio-client:cleanup => Error with Split graceful shutdown: %s'],
[ERROR_8, 'splitio-sync:segment-changes => Factory instantiation: you passed a Browser type authorizationKey, please grab an Api Key from the Split web console that is of type SDK.'],
[ERROR_6, 'splitio-offline:splits-fetcher => Ignoring entry on YAML since the format is incorrect.'],
];
26 changes: 26 additions & 0 deletions src/logger/messages/info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { INFO_0, INFO_1, INFO_2, INFO_3, INFO_4, INFO_5, INFO_6, INFO_7, INFO_8, INFO_9, INFO_10, INFO_11, INFO_12, INFO_13, INFO_14, INFO_15, INFO_16, INFO_17, INFO_18, INFO_19, INFO_20, INFO_21 } from '../constants';

export const codesInfo: [number, string][] = [
[INFO_0, 'Split SDK is ready from cache.'],
[INFO_1, 'Split SDK is ready.'],
[INFO_2, 'splitio-client => Split: %s. Key: %s. Evaluation: %s. Label: %s'],
[INFO_3, 'splitio-client => Queueing corresponding impression.'],
[INFO_4, 'splitio => New shared client instance created.'],
[INFO_5, 'splitio => New Split SDK instance created.'],
[INFO_6, 'splitio => Manager instance retrieved.'],
[INFO_7, 'splitio-sync:polling-manager => Turning segments data polling %s.'],
[INFO_8, 'splitio-sync:polling-manager => Starting polling'],
[INFO_9, 'splitio-sync:polling-manager => Stopping polling'],
[INFO_10, 'splitio-sync:split-changes => Retrying download of splits #%s. Reason: %s'],
[INFO_11, 'splitio-sync:push-manager => Refreshing streaming token in %s seconds.'],
[INFO_12, 'splitio-sync:push-manager => Attempting to reconnect in %s seconds.'],
[INFO_13, 'splitio-sync:push-manager => Connecting to push streaming.'],
[INFO_14, 'splitio-sync:push-manager => Streaming is not available. Switching to polling mode.'],
[INFO_15, 'splitio-sync:push-manager => Disconnecting from push streaming.'],
[INFO_16, 'splitio-sync:submitters => Flushing full events queue and reseting timer.'],
[INFO_17, 'splitio-sync:submitters => Pushing %s %s.'],
[INFO_18, 'splitio-sync:sync-manager => Streaming not available. Starting periodic fetch of data.'],
[INFO_19, 'splitio-sync:sync-manager => Streaming couldn\'t connect. Continue periodic fetch of data.'],
[INFO_20, 'splitio-sync:sync-manager => PUSH (re)connected. Syncing and stopping periodic fetch of data.'],
[INFO_21, 'splitio-client:event-tracker => Successfully qeued %s']
];
29 changes: 29 additions & 0 deletions src/logger/messages/warn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { WARN_0, WARN_1, WARN_2, WARN_4, WARN_5, WARN_6, WARN_7, WARN_8, WARN_9, WARN_10, WARN_11, WARN_12, WARN_13, WARN_14, WARN_15, WARN_17, WARN_18, WARN_19, WARN_20, WARN_21, WARN_22, WARN_23, WARN_24, WARN_25, WARN_API_KEY } from '../constants';

export const codesWarn: [number, string][] = [
[WARN_0, 'splitio-engine:value => Value %s %sdoesn\'t match with expected type.'],
[WARN_1, 'splitio-engine:value => Defined attribute [%s], no attributes received.'],
[WARN_2, 'No listeners for SDK Readiness detected. Incorrect control treatments could have been logged if you called getTreatment/s while the SDK was not yet ready.'],
[WARN_4, 'splitio-sync:my-segments => Retrying download of segments #%s. Reason: %s'],
[WARN_5, 'splitio-sync:split-changes => Error while doing fetch of Splits. %s'],
[WARN_6, 'splitio-sync:sse-handler => Error parsing SSE error notification: %s'],
[WARN_7, 'splitio-sync:sse-handler => Error parsing new SSE message notification: %s'],
[WARN_8, 'splitio-sync:push-manager => %sFalling back to polling mode.'],
[WARN_9, 'splitio-sync:submitters => Droping %s %s after retry. Reason %s.'],
[WARN_10, 'splitio-sync:submitters => Failed to push %s %s, keeping data to retry on next iteration. Reason %s.'],
[WARN_11, 'splitio-client:event-tracker => Failed to queue %s'],
[WARN_12, '%s: Property %s is of invalid type. Setting value to null.'],
[WARN_13, '%s: Event has more than 300 properties. Some of them will be trimmed when processed.'],
[WARN_14, '%s: the SDK is not ready, results may be incorrect. Make sure to wait for SDK readiness before using this method.'],
[WARN_15, '%s: %s "%s" is not of type string, converting.'],
[WARN_17, '%s: %s "%s" has extra whitespace, trimming.'],
[WARN_18, '%s: you passed "%s" that does not exist in this environment, please double check what Splits exist in the web console.'],
[WARN_19, '%s: traffic_type_name should be all lowercase - converting string to lowercase.'],
[WARN_20, '%s: Traffic Type %s does not have any corresponding Splits in this environment, make sure you\'re tracking your events to a valid traffic type defined in the Split console.'],
[WARN_21, 'splitio-settings => %s integration %s at settings %s invalid. %s'],
[WARN_22, 'Factory instantiation: split filters have been configured but will have no effect if mode is not "%s", since synchronization is being deferred to an external tool.'],
[WARN_23, 'Factory instantiation: split filter at position %s is invalid. It must be an object with a valid filter type ("byName" or "byPrefix") and a list of "values".'],
[WARN_24, 'Factory instantiation: splitFilters configuration must be a non-empty array of filter objects.'],
[WARN_25, 'splitio-settings => The provided storage is invalid. Fallbacking into default MEMORY storage'],
[WARN_API_KEY, 'Factory instantiation: %s. We recommend keeping only one instance of the factory at all times (Singleton pattern) and reusing it throughout your application']
];
5 changes: 5 additions & 0 deletions src/logger/messages/warnNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { WARN_3 } from '../constants';

export const codesWarnNode: [number, string][] = [
[WARN_3, 'splitio-offline:splits-fetcher => .split mocks will be deprecated soon in favor of YAML files, which provide more targeting power. Take a look in our documentation.'],
];
8 changes: 4 additions & 4 deletions src/logger/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ export interface ILoggerOptions {
export interface ILogger {
setLogLevel(logLevel: LogLevel): void

debug(msg: string, args?: any[]): void
debug(msg: string | number, args?: any[]): void

info(msg: string, args?: any[]): void
info(msg: string | number, args?: any[]): void

warn(msg: string, args?: any[]): void
warn(msg: string | number, args?: any[]): void

error(msg: string, args?: any[]): void
error(msg: string | number, args?: any[]): void
}
3 changes: 2 additions & 1 deletion src/utils/inputValidation/__tests__/attributes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ERROR_13 } from '../../../logger/constants';
import { loggerMock } from '../../../logger/__tests__/sdkLogger.mock';

import { validateAttributes } from '../attributes';
Expand Down Expand Up @@ -40,7 +41,7 @@ describe('INPUT VALIDATION for Attributes', () => {
const invalidAttribute = invalidAttributes[i];

expect(validateAttributes(loggerMock, invalidAttribute, 'test_method')).toBe(false); // Invalid attribute objects should return false.
expect(loggerMock.error.mock.calls[0][0]).toEqual('test_method: attributes must be a plain object.'); // The error should be logged for the invalid attributes map.
expect(loggerMock.error).lastCalledWith(ERROR_13, ['test_method']); // The error should be logged for the invalid attributes map.

loggerMock.error.mockClear();
}
Expand Down
2 changes: 0 additions & 2 deletions src/utils/settingsValidation/logger/builtinLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,3 @@ export function validateLogger(settings: { debug: unknown} ): ILogger {

return log;
}


Loading