@lwrjs/diagnostics 0.11.1 → 0.11.2

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.
@@ -27,12 +27,14 @@ __export(exports, {
27
27
  DEBUG: () => DEBUG,
28
28
  ERROR: () => ERROR,
29
29
  INFO: () => INFO,
30
+ SILENT: () => SILENT,
30
31
  VERBOSE: () => VERBOSE,
31
32
  WARN: () => WARN,
32
33
  logger: () => logger,
33
34
  stringifyError: () => stringifyError
34
35
  });
35
36
  var import_errors = __toModule(require("./errors.cjs"));
37
+ var SILENT = "silent";
36
38
  var VERBOSE = "verbose";
37
39
  var DEBUG = "debug";
38
40
  var INFO = "info";
@@ -40,63 +42,61 @@ var WARN = "warn";
40
42
  var ERROR = "error";
41
43
  var options = {};
42
44
  var DUPES = new Set();
43
- var currentLevel = process.env.LOG_LEVEL || INFO;
44
- function log(level, message, additionalInfo) {
45
- const LOG_LEVEL = process.env.LOG_LEVEL || INFO;
46
- if (currentLevel !== LOG_LEVEL) {
47
- currentLevel = LOG_LEVEL;
48
- console.log(`LOG_LEVEL: ${LOG_LEVEL}`);
49
- }
50
- let shouldLog = false;
45
+ function isLevelEnabled(level) {
46
+ const LOG_LEVEL = getLogLevel();
51
47
  switch (level) {
52
48
  case VERBOSE:
53
- shouldLog = LOG_LEVEL == VERBOSE;
54
- break;
49
+ return LOG_LEVEL == VERBOSE;
55
50
  case DEBUG:
56
- shouldLog = LOG_LEVEL == VERBOSE || LOG_LEVEL == DEBUG;
57
- break;
51
+ return LOG_LEVEL == VERBOSE || LOG_LEVEL == DEBUG;
58
52
  case INFO:
59
- shouldLog = LOG_LEVEL == VERBOSE || LOG_LEVEL == DEBUG || LOG_LEVEL == INFO;
60
- break;
53
+ return LOG_LEVEL == VERBOSE || LOG_LEVEL == DEBUG || LOG_LEVEL == INFO;
61
54
  case WARN:
62
- shouldLog = LOG_LEVEL == VERBOSE || LOG_LEVEL == DEBUG || LOG_LEVEL == INFO || LOG_LEVEL == WARN;
63
- break;
55
+ return LOG_LEVEL == VERBOSE || LOG_LEVEL == DEBUG || LOG_LEVEL == INFO || LOG_LEVEL == WARN;
64
56
  case ERROR:
65
- shouldLog = true;
66
- break;
57
+ return true;
58
+ case SILENT:
59
+ default:
60
+ return false;
61
+ }
62
+ }
63
+ function getLogLevel() {
64
+ return process.env.LWR_LOG_LEVEL || INFO;
65
+ }
66
+ function log(level, message, additionalInfo) {
67
+ if (!isLevelEnabled(level)) {
68
+ return;
67
69
  }
68
- if (shouldLog && options.dedupe && options.dedupe.has(level)) {
70
+ if (options.dedupe && options.dedupe.has(level)) {
69
71
  const key = `[${level}] : ${message}`;
70
72
  if (DUPES.has(key)) {
71
- shouldLog = false;
73
+ return;
72
74
  } else {
73
75
  DUPES.add(key);
74
76
  }
75
77
  }
76
- if (shouldLog) {
77
- const logMessage = `[${level}]${gap(message)}${message}`;
78
- const additionalMessage = additionalInfo ? `Additional Info: ${JSON.stringify(additionalInfo)}` : void 0;
79
- if (level == ERROR) {
80
- console.error("%s", logMessage);
81
- if (additionalInfo) {
82
- console.error("\n%s", additionalMessage);
83
- }
84
- } else if (level == WARN) {
85
- console.warn("%s", logMessage);
86
- if (additionalInfo) {
87
- console.warn("\n%s", additionalMessage);
88
- }
89
- } else if (level == DEBUG || level == VERBOSE) {
90
- console.log("%s", logMessage);
91
- if (additionalInfo) {
92
- console.log("\n%s", additionalMessage);
93
- }
94
- } else {
95
- console.log(logMessage);
96
- if (additionalInfo) {
97
- console.log(`
78
+ const logMessage = `[${level}]${gap(message)}${message}`;
79
+ const additionalMessage = additionalInfo ? `Additional Info: ${JSON.stringify(additionalInfo)}` : void 0;
80
+ if (level == ERROR) {
81
+ console.error("%s", logMessage);
82
+ if (additionalInfo) {
83
+ console.error("\n%s", additionalMessage);
84
+ }
85
+ } else if (level == WARN) {
86
+ console.warn("%s", logMessage);
87
+ if (additionalInfo) {
88
+ console.warn("\n%s", additionalMessage);
89
+ }
90
+ } else if (level == DEBUG || level == VERBOSE) {
91
+ console.log("%s", logMessage);
92
+ if (additionalInfo) {
93
+ console.log("\n%s", additionalMessage);
94
+ }
95
+ } else {
96
+ console.log(logMessage);
97
+ if (additionalInfo) {
98
+ console.log(`
98
99
  ${additionalMessage}`);
99
- }
100
100
  }
101
101
  }
102
102
  }
@@ -142,5 +142,10 @@ var logger = {
142
142
  setOptions: (opts) => {
143
143
  options = opts;
144
144
  },
145
- currentLevel
145
+ getLogLevel,
146
+ isDebugEnabled: () => isLevelEnabled(DEBUG),
147
+ isVerboseEnabled: () => isLevelEnabled(VERBOSE),
148
+ isWarnEnabled: () => isLevelEnabled(WARN),
149
+ isInfoEnabled: () => isLevelEnabled(INFO),
150
+ isErrorEnabled: () => isLevelEnabled(ERROR)
146
151
  };
@@ -1,4 +1,5 @@
1
- type LEVEL = 'verbose' | 'debug' | 'info' | 'warn' | 'error';
1
+ type LEVEL = 'verbose' | 'debug' | 'info' | 'warn' | 'error' | 'silent';
2
+ export declare const SILENT: LEVEL;
2
3
  export declare const VERBOSE: LEVEL;
3
4
  export declare const DEBUG: LEVEL;
4
5
  export declare const INFO: LEVEL;
@@ -7,7 +8,8 @@ export declare const ERROR: LEVEL;
7
8
  type LoggerOptions = {
8
9
  dedupe?: Set<string>;
9
10
  };
10
- declare function log(level: string, message: string, additionalInfo?: any): void;
11
+ declare function getLogLevel(): string;
12
+ declare function log(level: LEVEL, message: string, additionalInfo?: any): void;
11
13
  export declare const stringifyError: (error: any) => string;
12
14
  export declare const logger: {
13
15
  verbose: (message: string, additionalInfo?: any) => void;
@@ -17,7 +19,12 @@ export declare const logger: {
17
19
  error: (error: any, additionalInfo?: any) => void;
18
20
  log: typeof log;
19
21
  setOptions: (opts: LoggerOptions) => void;
20
- currentLevel: string;
22
+ getLogLevel: typeof getLogLevel;
23
+ isDebugEnabled: () => boolean;
24
+ isVerboseEnabled: () => boolean;
25
+ isWarnEnabled: () => boolean;
26
+ isInfoEnabled: () => boolean;
27
+ isErrorEnabled: () => boolean;
21
28
  };
22
29
  export {};
23
30
  //# sourceMappingURL=logger.d.ts.map
@@ -1,4 +1,5 @@
1
1
  import { DiagnosticsError } from './errors.js';
2
+ export const SILENT = 'silent';
2
3
  export const VERBOSE = 'verbose';
3
4
  export const DEBUG = 'debug';
4
5
  export const INFO = 'info';
@@ -6,70 +7,68 @@ export const WARN = 'warn';
6
7
  export const ERROR = 'error';
7
8
  let options = {};
8
9
  const DUPES = new Set();
9
- let currentLevel = process.env.LOG_LEVEL || INFO;
10
- function log(level, message, additionalInfo) {
11
- const LOG_LEVEL = process.env.LOG_LEVEL || INFO;
12
- if (currentLevel !== LOG_LEVEL) {
13
- currentLevel = LOG_LEVEL;
14
- console.log(`LOG_LEVEL: ${LOG_LEVEL}`);
15
- }
16
- let shouldLog = false;
10
+ function isLevelEnabled(level) {
11
+ const LOG_LEVEL = getLogLevel();
17
12
  switch (level) {
18
13
  case VERBOSE:
19
- shouldLog = LOG_LEVEL == VERBOSE;
20
- break;
14
+ return LOG_LEVEL == VERBOSE;
21
15
  case DEBUG:
22
- shouldLog = LOG_LEVEL == VERBOSE || LOG_LEVEL == DEBUG;
23
- break;
16
+ return LOG_LEVEL == VERBOSE || LOG_LEVEL == DEBUG;
24
17
  case INFO:
25
- shouldLog = LOG_LEVEL == VERBOSE || LOG_LEVEL == DEBUG || LOG_LEVEL == INFO;
26
- break;
18
+ return LOG_LEVEL == VERBOSE || LOG_LEVEL == DEBUG || LOG_LEVEL == INFO;
27
19
  case WARN:
28
- shouldLog = LOG_LEVEL == VERBOSE || LOG_LEVEL == DEBUG || LOG_LEVEL == INFO || LOG_LEVEL == WARN;
29
- break;
20
+ return LOG_LEVEL == VERBOSE || LOG_LEVEL == DEBUG || LOG_LEVEL == INFO || LOG_LEVEL == WARN;
30
21
  case ERROR:
31
- shouldLog = true;
32
- break;
22
+ return true;
23
+ case SILENT:
24
+ default:
25
+ return false;
26
+ }
27
+ }
28
+ function getLogLevel() {
29
+ return process.env.LWR_LOG_LEVEL || INFO; // TODO control for invalid log levels being set
30
+ }
31
+ function log(level, message, additionalInfo) {
32
+ // if log level not enabled, don't log
33
+ if (!isLevelEnabled(level)) {
34
+ return;
33
35
  }
34
36
  // Check if we should suppress dupes and we have already logged this message
35
- if (shouldLog && options.dedupe && options.dedupe.has(level)) {
37
+ if (options.dedupe && options.dedupe.has(level)) {
36
38
  const key = `[${level}] : ${message}`;
37
39
  if (DUPES.has(key)) {
38
- shouldLog = false;
40
+ return; // Don't log duplicates
39
41
  }
40
42
  else {
41
- // add key to de-duplicate cache
42
- DUPES.add(key);
43
+ DUPES.add(key); // add key to de-duplicate cache
43
44
  }
44
45
  }
45
- if (shouldLog) {
46
- const logMessage = `[${level}]${gap(message)}${message}`;
47
- const additionalMessage = additionalInfo
48
- ? `Additional Info: ${JSON.stringify(additionalInfo)}`
49
- : undefined;
50
- if (level == ERROR) {
51
- console.error('\x1b[31m%s\x1b[0m', logMessage); // red
52
- if (additionalInfo) {
53
- console.error('\n\x1b[31m%s\x1b[0m', additionalMessage); // red
54
- }
46
+ const logMessage = `[${level}]${gap(message)}${message}`;
47
+ const additionalMessage = additionalInfo
48
+ ? `Additional Info: ${JSON.stringify(additionalInfo)}`
49
+ : undefined;
50
+ if (level == ERROR) {
51
+ console.error('\x1b[31m%s\x1b[0m', logMessage); // red
52
+ if (additionalInfo) {
53
+ console.error('\n\x1b[31m%s\x1b[0m', additionalMessage); // red
55
54
  }
56
- else if (level == WARN) {
57
- console.warn('\x1b[33m%s\x1b[0m', logMessage); // yellow
58
- if (additionalInfo) {
59
- console.warn('\n\x1b[33m%s\x1b[0m', additionalMessage); // yellow
60
- }
55
+ }
56
+ else if (level == WARN) {
57
+ console.warn('\x1b[33m%s\x1b[0m', logMessage); // yellow
58
+ if (additionalInfo) {
59
+ console.warn('\n\x1b[33m%s\x1b[0m', additionalMessage); // yellow
61
60
  }
62
- else if (level == DEBUG || level == VERBOSE) {
63
- console.log('\x1b[2m%s\x1b[0m', logMessage); // dim
64
- if (additionalInfo) {
65
- console.log('\n\x1b[2m%s\x1b[0m', additionalMessage); // dim
66
- }
61
+ }
62
+ else if (level == DEBUG || level == VERBOSE) {
63
+ console.log('\x1b[2m%s\x1b[0m', logMessage); // dim
64
+ if (additionalInfo) {
65
+ console.log('\n\x1b[2m%s\x1b[0m', additionalMessage); // dim
67
66
  }
68
- else {
69
- console.log(logMessage);
70
- if (additionalInfo) {
71
- console.log(`\n${additionalMessage}`);
72
- }
67
+ }
68
+ else {
69
+ console.log(logMessage);
70
+ if (additionalInfo) {
71
+ console.log(`\n${additionalMessage}`);
73
72
  }
74
73
  }
75
74
  }
@@ -119,6 +118,11 @@ export const logger = {
119
118
  setOptions: (opts) => {
120
119
  options = opts;
121
120
  },
122
- currentLevel,
121
+ getLogLevel,
122
+ isDebugEnabled: () => isLevelEnabled(DEBUG),
123
+ isVerboseEnabled: () => isLevelEnabled(VERBOSE),
124
+ isWarnEnabled: () => isLevelEnabled(WARN),
125
+ isInfoEnabled: () => isLevelEnabled(INFO),
126
+ isErrorEnabled: () => isLevelEnabled(ERROR),
123
127
  };
124
128
  //# sourceMappingURL=logger.js.map
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.11.1",
7
+ "version": "0.11.2",
8
8
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
9
9
  "repository": {
10
10
  "type": "git",
@@ -35,7 +35,7 @@
35
35
  "test": "jest"
36
36
  },
37
37
  "devDependencies": {
38
- "@lwrjs/types": "0.11.1",
38
+ "@lwrjs/types": "0.11.2",
39
39
  "jest": "^26.6.3",
40
40
  "ts-jest": "^26.5.6",
41
41
  "typescript": "^4.9.5"
@@ -43,5 +43,5 @@
43
43
  "engines": {
44
44
  "node": ">=16.0.0"
45
45
  },
46
- "gitHead": "b7c40fdcd86635dd4e368c0a2e91c5d3374c0fcf"
46
+ "gitHead": "290a924d5e6610b54566aec8750895772f06d3aa"
47
47
  }