@splitsoftware/splitio-commons 2.6.0 → 2.6.1-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/CHANGES.txt +3 -0
  2. package/cjs/listeners/browser.js +1 -0
  3. package/cjs/logger/constants.js +4 -3
  4. package/cjs/logger/index.js +37 -18
  5. package/cjs/logger/messages/info.js +1 -0
  6. package/cjs/logger/sdkLogger.js +7 -0
  7. package/cjs/storages/inMemory/EventsCacheInMemory.js +1 -0
  8. package/cjs/storages/inMemory/ImpressionCountsCacheInMemory.js +1 -0
  9. package/cjs/storages/inMemory/ImpressionsCacheInMemory.js +1 -0
  10. package/cjs/storages/inMemory/TelemetryCacheInMemory.js +1 -0
  11. package/cjs/storages/inMemory/UniqueKeysCacheInMemory.js +1 -0
  12. package/cjs/storages/inMemory/UniqueKeysCacheInMemoryCS.js +1 -0
  13. package/cjs/sync/submitters/eventsSubmitter.js +2 -3
  14. package/cjs/sync/submitters/impressionCountsSubmitter.js +1 -1
  15. package/cjs/sync/submitters/impressionsSubmitter.js +2 -4
  16. package/cjs/sync/submitters/submitter.js +2 -1
  17. package/cjs/sync/submitters/telemetrySubmitter.js +3 -2
  18. package/cjs/sync/submitters/uniqueKeysSubmitter.js +2 -3
  19. package/cjs/utils/settingsValidation/logger/builtinLogger.js +4 -3
  20. package/cjs/utils/settingsValidation/logger/commons.js +12 -15
  21. package/cjs/utils/settingsValidation/logger/pluggableLogger.js +9 -6
  22. package/esm/listeners/browser.js +2 -1
  23. package/esm/logger/constants.js +1 -0
  24. package/esm/logger/index.js +37 -18
  25. package/esm/logger/messages/info.js +1 -0
  26. package/esm/logger/sdkLogger.js +7 -0
  27. package/esm/storages/inMemory/EventsCacheInMemory.js +1 -0
  28. package/esm/storages/inMemory/ImpressionCountsCacheInMemory.js +1 -0
  29. package/esm/storages/inMemory/ImpressionsCacheInMemory.js +1 -0
  30. package/esm/storages/inMemory/TelemetryCacheInMemory.js +1 -0
  31. package/esm/storages/inMemory/UniqueKeysCacheInMemory.js +1 -0
  32. package/esm/storages/inMemory/UniqueKeysCacheInMemoryCS.js +1 -0
  33. package/esm/sync/submitters/eventsSubmitter.js +2 -3
  34. package/esm/sync/submitters/impressionCountsSubmitter.js +1 -1
  35. package/esm/sync/submitters/impressionsSubmitter.js +2 -4
  36. package/esm/sync/submitters/submitter.js +2 -1
  37. package/esm/sync/submitters/telemetrySubmitter.js +3 -2
  38. package/esm/sync/submitters/uniqueKeysSubmitter.js +2 -3
  39. package/esm/utils/settingsValidation/logger/builtinLogger.js +4 -3
  40. package/esm/utils/settingsValidation/logger/commons.js +10 -14
  41. package/esm/utils/settingsValidation/logger/pluggableLogger.js +10 -7
  42. package/package.json +1 -1
  43. package/src/listeners/browser.ts +2 -1
  44. package/src/logger/constants.ts +1 -0
  45. package/src/logger/index.ts +36 -22
  46. package/src/logger/messages/info.ts +1 -0
  47. package/src/logger/sdkLogger.ts +7 -0
  48. package/src/logger/types.ts +4 -3
  49. package/src/storages/inMemory/EventsCacheInMemory.ts +1 -0
  50. package/src/storages/inMemory/ImpressionCountsCacheInMemory.ts +2 -0
  51. package/src/storages/inMemory/ImpressionsCacheInMemory.ts +1 -0
  52. package/src/storages/inMemory/TelemetryCacheInMemory.ts +2 -0
  53. package/src/storages/inMemory/UniqueKeysCacheInMemory.ts +3 -3
  54. package/src/storages/inMemory/UniqueKeysCacheInMemoryCS.ts +3 -3
  55. package/src/storages/types.ts +1 -0
  56. package/src/sync/submitters/eventsSubmitter.ts +2 -4
  57. package/src/sync/submitters/impressionCountsSubmitter.ts +1 -1
  58. package/src/sync/submitters/impressionsSubmitter.ts +2 -5
  59. package/src/sync/submitters/submitter.ts +1 -1
  60. package/src/sync/submitters/telemetrySubmitter.ts +3 -2
  61. package/src/sync/submitters/uniqueKeysSubmitter.ts +2 -3
  62. package/src/utils/settingsValidation/logger/builtinLogger.ts +5 -4
  63. package/src/utils/settingsValidation/logger/commons.ts +11 -11
  64. package/src/utils/settingsValidation/logger/pluggableLogger.ts +12 -8
  65. package/types/splitio.d.ts +26 -0
@@ -40,15 +40,16 @@ if (/^(enabled?|on)/i.test(initialState)) {
40
40
  * @param settings - user config object, with an optional `debug` property of type boolean or string log level.
41
41
  * @returns a logger instance with the log level at `settings.debug`. If `settings.debug` is invalid or not provided, `initialLogLevel` is used.
42
42
  */
43
- export function validateLogger(settings: { debug: unknown }): ILogger {
44
- const { debug } = settings;
43
+ export function validateLogger(settings: { debug: unknown, logger?: SplitIO.Logger }): ILogger {
44
+ const { debug, logger } = settings;
45
45
 
46
46
  const logLevel: SplitIO.LogLevel | undefined = debug !== undefined ? getLogLevel(debug) : initialLogLevel;
47
47
 
48
48
  const log = new Logger({ logLevel: logLevel || initialLogLevel }, allCodes);
49
+ log.setLogger(logger);
49
50
 
50
- // @ts-ignore // if logLevel is undefined at this point, it means that settings `debug` value is invalid
51
- if (!logLevel) log._log(LogLevels.ERROR, 'Invalid Log Level - No changes to the logs will be applied.');
51
+ // if logLevel is undefined at this point, it means that settings `debug` value is invalid
52
+ if (!logLevel) log._log('error', 'Invalid Log Level - No changes to the logs will be applied.');
52
53
 
53
54
  return log;
54
55
  }
@@ -10,15 +10,15 @@ import SplitIO from '../../../../types/splitio';
10
10
  * @returns LogLevel of the given debugValue or undefined if the provided value is invalid
11
11
  */
12
12
  export function getLogLevel(debugValue: unknown): SplitIO.LogLevel | undefined {
13
- if (typeof debugValue === 'boolean') {
14
- if (debugValue) {
15
- return LogLevels.DEBUG;
16
- } else {
17
- return LogLevels.NONE;
18
- }
19
- } else if (typeof debugValue === 'string' && isLogLevelString(debugValue)) {
20
- return debugValue;
21
- } else {
22
- return undefined;
23
- }
13
+ return typeof debugValue === 'boolean' ?
14
+ debugValue ?
15
+ LogLevels.DEBUG :
16
+ LogLevels.NONE :
17
+ typeof debugValue === 'string' && isLogLevelString(debugValue) ?
18
+ debugValue :
19
+ undefined;
20
+ }
21
+
22
+ export function isLogger(log: any): log is SplitIO.Logger {
23
+ return log !== null && typeof log === 'object' && typeof log.debug === 'function' && typeof log.info === 'function' && typeof log.warn === 'function' && typeof log.error === 'function';
24
24
  }
@@ -1,10 +1,10 @@
1
1
  import { Logger, LogLevels } from '../../../logger';
2
2
  import { ILogger } from '../../../logger/types';
3
3
  import SplitIO from '../../../../types/splitio';
4
- import { getLogLevel } from './commons';
4
+ import { getLogLevel, isLogger } from './commons';
5
5
 
6
- function isLogger(log: any): log is ILogger {
7
- return log !== null && typeof log === 'object' && typeof log.debug === 'function' && typeof log.info === 'function' && typeof log.warn === 'function' && typeof log.error === 'function' && typeof log.setLogLevel === 'function';
6
+ function isILogger(log: any): log is ILogger {
7
+ return isLogger(log) && typeof (log as any).setLogLevel === 'function';
8
8
  }
9
9
 
10
10
  // By default it starts disabled.
@@ -17,19 +17,23 @@ let initialLogLevel = LogLevels.NONE;
17
17
  * @returns a logger instance, that might be: the provided logger at `settings.debug`, or one with the given `debug` log level,
18
18
  * or one with NONE log level if `debug` is not defined or invalid.
19
19
  */
20
- export function validateLogger(settings: { debug: unknown }): ILogger {
21
- const { debug } = settings;
20
+ export function validateLogger(settings: { debug: unknown, logger?: SplitIO.Logger }): ILogger {
21
+ const { debug, logger } = settings;
22
22
  let logLevel: SplitIO.LogLevel | undefined = initialLogLevel;
23
23
 
24
24
  if (debug !== undefined) {
25
- if (isLogger(debug)) return debug;
25
+ if (isILogger(debug)) {
26
+ debug.setLogger(logger);
27
+ return debug;
28
+ }
26
29
  logLevel = getLogLevel(settings.debug);
27
30
  }
28
31
 
29
32
  const log = new Logger({ logLevel: logLevel || initialLogLevel });
33
+ log.setLogger(logger);
30
34
 
31
- // @ts-ignore // `debug` value is invalid if logLevel is undefined at this point
32
- if (!logLevel) log._log(LogLevels.ERROR, 'Invalid `debug` value at config. Logs will be disabled.');
35
+ // `debug` value is invalid if logLevel is undefined at this point
36
+ if (!logLevel) log._log('error', 'Invalid `debug` value at config. Logs will be disabled.');
33
37
 
34
38
  return log;
35
39
  }
@@ -91,6 +91,10 @@ interface ISharedSettings {
91
91
  * Do not change these settings unless you're working an advanced use case, like connecting to the Split proxy.
92
92
  */
93
93
  urls?: SplitIO.UrlSettings;
94
+ /**
95
+ * Custom logger object. If not provided, the SDK will use the default `console.log` method for all log levels.
96
+ */
97
+ logger?: SplitIO.Logger;
94
98
  }
95
99
  /**
96
100
  * Common settings properties for SDKs with synchronous API (standalone and localhost modes).
@@ -141,6 +145,8 @@ interface IPluggableSharedSettings {
141
145
  * config.debug = ErrorLogger()
142
146
  * ```
143
147
  *
148
+ * When combined with the `logger` option, any log level other than `NONE` (false) will be set to `DEBUG` (true), delegating log level control to the custom logger.
149
+ *
144
150
  * @defaultValue `false`
145
151
  */
146
152
  debug?: boolean | SplitIO.LogLevel | SplitIO.ILogger;
@@ -164,6 +170,8 @@ interface INonPluggableSharedSettings {
164
170
  * config.debug = 'WARN'
165
171
  * ```
166
172
  *
173
+ * When combined with the `logger` option, any log level other than `NONE` (false) will be set to `DEBUG` (true), delegating log level control to the custom logger.
174
+ *
167
175
  * @defaultValue `false`
168
176
  */
169
177
  debug?: boolean | SplitIO.LogLevel;
@@ -587,6 +595,7 @@ declare namespace SplitIO {
587
595
  telemetry: string;
588
596
  };
589
597
  readonly integrations?: IntegrationFactory[];
598
+ readonly logger?: Logger;
590
599
  readonly debug: boolean | LogLevel | ILogger;
591
600
  readonly version: string;
592
601
  /**
@@ -617,6 +626,15 @@ declare namespace SplitIO {
617
626
  * Log levels.
618
627
  */
619
628
  type LogLevel = 'DEBUG' | 'INFO' | 'WARN' | 'ERROR' | 'NONE';
629
+ /**
630
+ * Custom logger interface.
631
+ */
632
+ interface Logger {
633
+ debug(message: string): any;
634
+ info(message: string): any;
635
+ warn(message: string): any;
636
+ error(message: string): any;
637
+ }
620
638
  /**
621
639
  * Logger API
622
640
  */
@@ -631,8 +649,16 @@ declare namespace SplitIO {
631
649
  disable(): void;
632
650
  /**
633
651
  * Sets a log level for the SDK logs.
652
+ *
653
+ * @param logLevel - The log level to set.
634
654
  */
635
655
  setLogLevel(logLevel: LogLevel): void;
656
+ /**
657
+ * Sets a custom logger for the SDK logs.
658
+ *
659
+ * @param logger - The custom logger to set, or `undefined` to remove the custom logger and fall back to the default `console.log` method.
660
+ */
661
+ setLogger(logger?: Logger): void;
636
662
  /**
637
663
  * Log level constants. Use this to pass them to setLogLevel function.
638
664
  */