@naturalcycles/js-lib 14.62.0 → 14.66.0

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.
@@ -1,3 +1,5 @@
1
+ import { _noop } from '../index'
2
+
1
3
  /**
2
4
  * These levels follow console.* naming,
3
5
  * so you can use console[level] safely.
@@ -10,6 +12,12 @@
10
12
  */
11
13
  export type CommonLogLevel = 'log' | 'warn' | 'error'
12
14
 
15
+ export const commonLogLevelNumber: Record<CommonLogLevel, number> = {
16
+ log: 10,
17
+ warn: 20,
18
+ error: 30,
19
+ }
20
+
13
21
  /**
14
22
  * Function that takes any number of arguments and logs them all.
15
23
  * It is expected that logged arguments are separated by "space", like console.log does.
@@ -31,35 +39,41 @@ export interface CommonLogger {
31
39
  }
32
40
 
33
41
  /**
34
- * Same as CommonLogger, but also is a "convenience function" itself.
35
- * So you can do `logger('hey')` which is the same as `logger.log('hey')`
36
- *
37
- * @experimental
38
- */
39
- export interface SimpleLogger extends CommonLogFunction, CommonLogger {}
40
-
41
- /**
42
- * Creates a SimpleLogger from CommonLogger.
42
+ * SimpleLogger that does nothing (noop).
43
43
  *
44
44
  * @experimental
45
45
  */
46
- export function createSimpleLogger(logger: CommonLogger): SimpleLogger {
47
- return Object.assign(((...args: any[]) => logger.log(...args)) as any, {
48
- log: (...args: any[]) => logger.log(...args),
49
- warn: (...args: any[]) => logger.warn(...args),
50
- error: (...args: any[]) => logger.error(...args),
51
- })
46
+ export const noopLogger: CommonLogger = {
47
+ log: _noop,
48
+ warn: _noop,
49
+ error: _noop,
52
50
  }
53
51
 
54
- const noop = () => {}
55
-
56
52
  /**
57
- * SimpleLogger that does nothing (noop).
58
- *
59
- * @experimental
53
+ * Creates a "child" logger that is "limited" to the specified CommonLogLevel.
60
54
  */
61
- export const noopLogger: SimpleLogger = createSimpleLogger({
62
- log: noop,
63
- warn: noop,
64
- error: noop,
65
- })
55
+ export function commonLoggerMinLevel(
56
+ logger: CommonLogger,
57
+ minLevel: CommonLogLevel,
58
+ mutate = false,
59
+ ): CommonLogger {
60
+ const level = commonLogLevelNumber[minLevel]
61
+ if (mutate) {
62
+ if (level > commonLogLevelNumber['log']) {
63
+ logger.log = _noop
64
+ if (level > commonLogLevelNumber['warn']) {
65
+ logger.warn = _noop
66
+ if (level > commonLogLevelNumber['error']) {
67
+ logger.error = _noop
68
+ }
69
+ }
70
+ }
71
+ return logger
72
+ }
73
+
74
+ return {
75
+ log: level <= commonLogLevelNumber['log'] ? (...args) => logger.log(...args) : _noop,
76
+ warn: level <= commonLogLevelNumber['warn'] ? (...args) => logger.warn(...args) : _noop,
77
+ error: level <= commonLogLevelNumber['error'] ? (...args) => logger.error(...args) : _noop,
78
+ }
79
+ }
@@ -1,4 +1,4 @@
1
- import { _since, _stringifyAny } from '..'
1
+ import { _since, _stringifyAny, CommonLogger } from '..'
2
2
 
3
3
  export interface PRetryOptions {
4
4
  /**
@@ -63,6 +63,11 @@ export interface PRetryOptions {
63
63
  * @default false
64
64
  */
65
65
  logNone?: boolean
66
+
67
+ /**
68
+ * Default to `console`
69
+ */
70
+ logger?: CommonLogger
66
71
  }
67
72
 
68
73
  /**
@@ -71,7 +76,13 @@ export interface PRetryOptions {
71
76
  */
72
77
  // eslint-disable-next-line @typescript-eslint/ban-types
73
78
  export function pRetry<T extends Function>(fn: T, opt: PRetryOptions = {}): T {
74
- const { maxAttempts = 4, delay: initialDelay = 1000, delayMultiplier = 2, predicate } = opt
79
+ const {
80
+ maxAttempts = 4,
81
+ delay: initialDelay = 1000,
82
+ delayMultiplier = 2,
83
+ predicate,
84
+ logger = console,
85
+ } = opt
75
86
 
76
87
  let { logFirstAttempt = false, logRetries = true, logFailures = false, logSuccess = false } = opt
77
88
 
@@ -95,18 +106,18 @@ export function pRetry<T extends Function>(fn: T, opt: PRetryOptions = {}): T {
95
106
  try {
96
107
  attempt++
97
108
  if ((attempt === 1 && logFirstAttempt) || (attempt > 1 && logRetries)) {
98
- console.log(`${fname} attempt #${attempt}...`)
109
+ logger.log(`${fname} attempt #${attempt}...`)
99
110
  }
100
111
 
101
112
  const r = await fn.apply(this, args)
102
113
 
103
114
  if (logSuccess) {
104
- console.log(`${fname} attempt #${attempt} succeeded in ${_since(started)}`)
115
+ logger.log(`${fname} attempt #${attempt} succeeded in ${_since(started)}`)
105
116
  }
106
117
  resolve(r)
107
118
  } catch (err) {
108
119
  if (logFailures) {
109
- console.warn(
120
+ logger.warn(
110
121
  `${fname} attempt #${attempt} error in ${_since(started)}:\n${_stringifyAny(err, {
111
122
  includeErrorData: true,
112
123
  })}`,