@xh/hoist 76.0.0-SNAPSHOT.1756402657806 → 76.0.0-SNAPSHOT.1756431654493

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.
@@ -6,7 +6,7 @@
6
6
  */
7
7
  import {PlainObject, Thunkable} from '@xh/hoist/core';
8
8
  import {Exception} from '@xh/hoist/core/exception/Exception';
9
- import {LogSource, logWarn} from '@xh/hoist/utils/js/LogUtils';
9
+ import {LogSource, logWarn, logError} from '@xh/hoist/utils/js/LogUtils';
10
10
  import {
11
11
  flatMap,
12
12
  forOwn,
@@ -131,7 +131,7 @@ export function throwIf(condition: any, message: unknown) {
131
131
  */
132
132
  export function warnIf(condition: any, message: any) {
133
133
  if (condition) {
134
- console.warn(message);
134
+ logWarn(message);
135
135
  }
136
136
  }
137
137
 
@@ -140,7 +140,7 @@ export function warnIf(condition: any, message: any) {
140
140
  */
141
141
  export function errorIf(condition: any, message: any) {
142
142
  if (condition) {
143
- console.error(message);
143
+ logError(message);
144
144
  }
145
145
  }
146
146
 
@@ -6,10 +6,62 @@
6
6
  */
7
7
  import {Some} from '@xh/hoist/core';
8
8
  import {castArray, isString} from 'lodash';
9
+ import store from 'store2';
9
10
  import {intersperse} from './LangUtils';
10
11
 
12
+ /**
13
+ * Utility functions providing managed, structured logging to Hoist apps.
14
+ *
15
+ * Essentially a wrapper around the browser console supporting logging levels, timing, and
16
+ * miscellaneous Hoist display conventions.
17
+ *
18
+ * Objects extending `HoistBase` need not import these functions directly, as they are available
19
+ * via delegates on `HoistBase`.
20
+ *
21
+ * Hoist sets its minimum severity level to 'info' by default. This prevents performance or
22
+ * memory impacts that might result from verbose debug logging. This can be adjusted by calling
23
+ * XH.logLevel from the console.
24
+ */
25
+
26
+ /** Severity Level for log statement */
27
+ export type LogLevel = 'error' | 'warn' | 'info' | 'debug';
28
+
29
+ /** Object identifying the source of log statement. Typically, a javascript class */
11
30
  export type LogSource = string | {displayName: string} | {constructor: {name: string}};
12
31
 
32
+ /**
33
+ * Current minimum severity for Hoist log utils (default 'info').
34
+ * Messages logged via managed Hoist log utils with lower severity will be ignored.
35
+ *
36
+ * @internal - use public `XH.logLevel`.
37
+ */
38
+ export function getLogLevel() {
39
+ return _logLevel;
40
+ }
41
+
42
+ /**
43
+ * Set the minimum severity for Hoist log utils until the page is refreshed. Optionally persist
44
+ * this adjustment to sessionStorage to maintain for the lifetime of the browser tab.
45
+ *
46
+ * @internal - use public `XH.setLogLevel()`.
47
+ */
48
+ export function setLogLevel(level: LogLevel, persistInSessionStorage: boolean = false) {
49
+ level = level.toLowerCase() as LogLevel;
50
+
51
+ const validLevels = ['error', 'warn', 'info', 'debug'];
52
+ if (!validLevels.includes(level)) {
53
+ console.error(`Ignored invalid log level '${level}' - must be one of ${validLevels}`);
54
+ return;
55
+ }
56
+ _logLevel = level;
57
+ if (persistInSessionStorage) {
58
+ store.session.set('xhLogLevel', level);
59
+ }
60
+ if (level != 'info') {
61
+ console.warn(`Client logging set to level '${level}'.`);
62
+ }
63
+ }
64
+
13
65
  /**
14
66
  * Time and log execution of a function to `console.info()`.
15
67
  *
@@ -72,19 +124,14 @@ export function logWarn(msgs: Some<unknown>, source?: LogSource) {
72
124
  return loggedDo(msgs, null, source, 'warn');
73
125
  }
74
126
 
75
- /** Parse a LogSource in to a canonical string label. */
76
- export function parseSource(source: LogSource): string {
77
- if (!source) return null;
78
- if (isString(source)) return source;
79
- if (source['displayName']) return source['displayName'];
80
- if (source.constructor) return source.constructor.name;
81
- return null;
82
- }
83
-
84
127
  //----------------------------------
85
128
  // Implementation
86
129
  //----------------------------------
87
- function loggedDo<T>(messages: Some<unknown>, fn: () => T, source: LogSource, level: LogLevel) {
130
+ function loggedDo<T>(messages: Some<unknown>, fn: () => T, source: LogSource, level: LogLevel): T {
131
+ if (_severity[level] < _severity[_logLevel]) {
132
+ return fn?.();
133
+ }
134
+
88
135
  let src = parseSource(source);
89
136
  let msgs = castArray(messages);
90
137
 
@@ -147,4 +194,20 @@ function writeLog(msgs: unknown[], src: string, level: LogLevel) {
147
194
  }
148
195
  }
149
196
 
150
- type LogLevel = 'error' | 'warn' | 'info' | 'debug';
197
+ /** Parse a LogSource in to a canonical string label. */
198
+ function parseSource(source: LogSource): string {
199
+ if (!source) return null;
200
+ if (isString(source)) return source;
201
+ if (source['displayName']) return source['displayName'];
202
+ if (source.constructor) return source.constructor.name;
203
+ return null;
204
+ }
205
+
206
+ //----------------------------------------------------------------
207
+ // Initialization + Level/Severity support.
208
+ // Initialize during parsing to make available immediately.
209
+ //----------------------------------------------------------------
210
+ let _logLevel: LogLevel = 'info';
211
+ const _severity: Record<LogLevel, number> = {error: 3, warn: 2, info: 1, debug: 0};
212
+
213
+ setLogLevel(store.session.get('xhLogLevel', 'info'));