@sprucelabs/spruce-skill-utils 33.0.27 → 33.1.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,4 +1,41 @@
1
1
  import chalk from 'chalk';
2
+ export declare class Logger implements Log {
3
+ readonly prefix: string | undefined;
4
+ private readonly baseLog?;
5
+ private readonly useColorsOption?;
6
+ private readonly transports;
7
+ private readonly colors;
8
+ private readonly pre;
9
+ private readonly shouldUseColors;
10
+ constructor(prefix?: string | undefined, options?: LogOptions);
11
+ info(...args: LoggableType[]): string;
12
+ warn(...args: LoggableType[]): string;
13
+ error(...args: LoggableType[]): string;
14
+ buildLog(prefix?: string | undefined, options?: LogOptions): Log;
15
+ private write;
16
+ private shouldLog;
17
+ private resolveChalk;
18
+ private combinePrefixes;
19
+ private formatArgs;
20
+ private buildPrefixes;
21
+ private reducePrefix;
22
+ private dispatchToTransports;
23
+ private buildMessage;
24
+ private buildPlainMessage;
25
+ private decorateWithTimeDelta;
26
+ private decorateWithTimestamp;
27
+ private emit;
28
+ private getTransports;
29
+ private resolveConsoleMethod;
30
+ private resolveTransport;
31
+ private get env();
32
+ private get logLevel();
33
+ private get shouldWrite();
34
+ private get shouldLogTimeDeltas();
35
+ private get shouldLogTime();
36
+ private formatArg;
37
+ protected isMainModule(): boolean;
38
+ }
2
39
  export default function buildLog(prefix?: string | undefined, options?: LogOptions): Log;
3
40
  export declare const testLog: Log;
4
41
  export declare const stubLog: Log;
@@ -1,5 +1,5 @@
1
1
  import chalk from 'chalk';
2
- class Logger {
2
+ export class Logger {
3
3
  constructor(prefix = undefined, options) {
4
4
  var _a, _b, _c;
5
5
  const { colors = {}, log, useColors, transportsByLevel } = options !== null && options !== void 0 ? options : {};
@@ -34,16 +34,25 @@ class Logger {
34
34
  if (!this.shouldWrite(level)) {
35
35
  return '';
36
36
  }
37
+ const shouldLog = this.shouldLog();
37
38
  const formattedArgs = this.formatArgs(rawArgs);
38
- const { prefix, chalkArgs } = this.buildPrefixes(formattedArgs);
39
- if (this.dispatchToTransports(level, prefix, formattedArgs)) {
40
- return prefix;
39
+ const { prefix, logArgs: logArgs } = this.buildPrefixes(formattedArgs);
40
+ if (!shouldLog ||
41
+ this.dispatchToTransports(level, prefix, formattedArgs)) {
42
+ const joined = rawArgs.join(' ');
43
+ return prefix ? prefix + ' ' + joined : joined;
41
44
  }
42
45
  const transport = this.resolveTransport(level, this.resolveConsoleMethod(level));
43
- const message = this.buildMessage(chalkMethod, chalkArgs, level, prefix);
46
+ const message = this.buildMessage(chalkMethod, logArgs, level, prefix);
44
47
  this.emit(transport, message, formattedArgs, rawArgs);
45
48
  return message;
46
49
  }
50
+ shouldLog() {
51
+ var _a;
52
+ return (this.isMainModule() ||
53
+ (this.prefix &&
54
+ ((_a = this.env.SPRUCE_LOGS) === null || _a === void 0 ? void 0 : _a.split(',').map((s) => s.trim()).includes(this.prefix))));
55
+ }
47
56
  resolveChalk(base, modifier) {
48
57
  var _a;
49
58
  const baseMethod = chalk === null || chalk === void 0 ? void 0 : chalk[base];
@@ -69,15 +78,15 @@ class Logger {
69
78
  if (!this.pre) {
70
79
  return {
71
80
  prefix: '',
72
- chalkArgs: [...args],
81
+ logArgs: [...args],
73
82
  };
74
83
  }
75
84
  const reducedPrefix = this.reducePrefix(this.pre);
76
85
  const prefixValue = reducedPrefix.length > 0 ? ` ${reducedPrefix}` : '';
77
- const chalkArgs = reducedPrefix.length > 0 ? [reducedPrefix, ...args] : [...args];
86
+ const logArgs = reducedPrefix.length > 0 ? [reducedPrefix, ...args] : [...args];
78
87
  return {
79
- prefix: prefixValue,
80
- chalkArgs,
88
+ prefix: prefixValue.trim(),
89
+ logArgs,
81
90
  };
82
91
  }
83
92
  reducePrefix(prefix) {
@@ -96,15 +105,15 @@ class Logger {
96
105
  if (transports.length === 0) {
97
106
  return false;
98
107
  }
99
- const payload = [prefix.trim(), ...args].filter((part) => part.length > 0);
108
+ const payload = [prefix, ...args].filter((part) => part.length > 0);
100
109
  for (const transport of transports) {
101
110
  transport(...payload);
102
111
  }
103
112
  return true;
104
113
  }
105
- buildMessage(chalkMethod, chalkArgs, level, prefix) {
114
+ buildMessage(chalkMethod, logArgs, level, prefix) {
106
115
  const baseMessage = this.shouldUseColors && chalkMethod
107
- ? chalkMethod(...chalkArgs)
116
+ ? chalkMethod(...logArgs)
108
117
  : this.buildPlainMessage(level, prefix);
109
118
  const withDelta = this.shouldLogTimeDeltas
110
119
  ? this.decorateWithTimeDelta(baseMessage)
@@ -114,7 +123,7 @@ class Logger {
114
123
  : withDelta;
115
124
  }
116
125
  buildPlainMessage(level, prefix) {
117
- return `(${level})${prefix}`;
126
+ return `(${level})${prefix ? ' ' + prefix : ''}`;
118
127
  }
119
128
  decorateWithTimeDelta(message) {
120
129
  const now = Date.now();
@@ -207,6 +216,9 @@ class Logger {
207
216
  }
208
217
  return 'undefined';
209
218
  }
219
+ isMainModule() {
220
+ return true;
221
+ }
210
222
  }
211
223
  export default function buildLog(prefix = undefined, options) {
212
224
  return new Logger(prefix, options);
@@ -1,4 +1,41 @@
1
1
  import chalk from 'chalk';
2
+ export declare class Logger implements Log {
3
+ readonly prefix: string | undefined;
4
+ private readonly baseLog?;
5
+ private readonly useColorsOption?;
6
+ private readonly transports;
7
+ private readonly colors;
8
+ private readonly pre;
9
+ private readonly shouldUseColors;
10
+ constructor(prefix?: string | undefined, options?: LogOptions);
11
+ info(...args: LoggableType[]): string;
12
+ warn(...args: LoggableType[]): string;
13
+ error(...args: LoggableType[]): string;
14
+ buildLog(prefix?: string | undefined, options?: LogOptions): Log;
15
+ private write;
16
+ private shouldLog;
17
+ private resolveChalk;
18
+ private combinePrefixes;
19
+ private formatArgs;
20
+ private buildPrefixes;
21
+ private reducePrefix;
22
+ private dispatchToTransports;
23
+ private buildMessage;
24
+ private buildPlainMessage;
25
+ private decorateWithTimeDelta;
26
+ private decorateWithTimestamp;
27
+ private emit;
28
+ private getTransports;
29
+ private resolveConsoleMethod;
30
+ private resolveTransport;
31
+ private get env();
32
+ private get logLevel();
33
+ private get shouldWrite();
34
+ private get shouldLogTimeDeltas();
35
+ private get shouldLogTime();
36
+ private formatArg;
37
+ protected isMainModule(): boolean;
38
+ }
2
39
  export default function buildLog(prefix?: string | undefined, options?: LogOptions): Log;
3
40
  export declare const testLog: Log;
4
41
  export declare const stubLog: Log;
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.stubLog = exports.testLog = void 0;
6
+ exports.stubLog = exports.testLog = exports.Logger = void 0;
7
7
  exports.default = buildLog;
8
8
  const chalk_1 = __importDefault(require("chalk"));
9
9
  class Logger {
@@ -45,16 +45,26 @@ class Logger {
45
45
  if (!this.shouldWrite(level)) {
46
46
  return '';
47
47
  }
48
+ const shouldLog = this.shouldLog();
48
49
  const formattedArgs = this.formatArgs(rawArgs);
49
- const { prefix, chalkArgs } = this.buildPrefixes(formattedArgs);
50
- if (this.dispatchToTransports(level, prefix, formattedArgs)) {
51
- return prefix;
50
+ const { prefix, logArgs: logArgs } = this.buildPrefixes(formattedArgs);
51
+ if (!shouldLog ||
52
+ this.dispatchToTransports(level, prefix, formattedArgs)) {
53
+ const joined = rawArgs.join(' ');
54
+ return prefix ? prefix + ' ' + joined : joined;
52
55
  }
53
56
  const transport = this.resolveTransport(level, this.resolveConsoleMethod(level));
54
- const message = this.buildMessage(chalkMethod, chalkArgs, level, prefix);
57
+ const message = this.buildMessage(chalkMethod, logArgs, level, prefix);
55
58
  this.emit(transport, message, formattedArgs, rawArgs);
56
59
  return message;
57
60
  }
61
+ shouldLog() {
62
+ return (this.isMainModule() ||
63
+ (this.prefix &&
64
+ this.env.SPRUCE_LOGS?.split(',')
65
+ .map((s) => s.trim())
66
+ .includes(this.prefix)));
67
+ }
58
68
  resolveChalk(base, modifier) {
59
69
  const baseMethod = chalk_1.default?.[base];
60
70
  if (!baseMethod) {
@@ -79,15 +89,15 @@ class Logger {
79
89
  if (!this.pre) {
80
90
  return {
81
91
  prefix: '',
82
- chalkArgs: [...args],
92
+ logArgs: [...args],
83
93
  };
84
94
  }
85
95
  const reducedPrefix = this.reducePrefix(this.pre);
86
96
  const prefixValue = reducedPrefix.length > 0 ? ` ${reducedPrefix}` : '';
87
- const chalkArgs = reducedPrefix.length > 0 ? [reducedPrefix, ...args] : [...args];
97
+ const logArgs = reducedPrefix.length > 0 ? [reducedPrefix, ...args] : [...args];
88
98
  return {
89
- prefix: prefixValue,
90
- chalkArgs,
99
+ prefix: prefixValue.trim(),
100
+ logArgs,
91
101
  };
92
102
  }
93
103
  reducePrefix(prefix) {
@@ -106,15 +116,15 @@ class Logger {
106
116
  if (transports.length === 0) {
107
117
  return false;
108
118
  }
109
- const payload = [prefix.trim(), ...args].filter((part) => part.length > 0);
119
+ const payload = [prefix, ...args].filter((part) => part.length > 0);
110
120
  for (const transport of transports) {
111
121
  transport(...payload);
112
122
  }
113
123
  return true;
114
124
  }
115
- buildMessage(chalkMethod, chalkArgs, level, prefix) {
125
+ buildMessage(chalkMethod, logArgs, level, prefix) {
116
126
  const baseMessage = this.shouldUseColors && chalkMethod
117
- ? chalkMethod(...chalkArgs)
127
+ ? chalkMethod(...logArgs)
118
128
  : this.buildPlainMessage(level, prefix);
119
129
  const withDelta = this.shouldLogTimeDeltas
120
130
  ? this.decorateWithTimeDelta(baseMessage)
@@ -124,7 +134,7 @@ class Logger {
124
134
  : withDelta;
125
135
  }
126
136
  buildPlainMessage(level, prefix) {
127
- return `(${level})${prefix}`;
137
+ return `(${level})${prefix ? ' ' + prefix : ''}`;
128
138
  }
129
139
  decorateWithTimeDelta(message) {
130
140
  const now = Date.now();
@@ -213,7 +223,11 @@ class Logger {
213
223
  }
214
224
  return 'undefined';
215
225
  }
226
+ isMainModule() {
227
+ return true;
228
+ }
216
229
  }
230
+ exports.Logger = Logger;
217
231
  function buildLog(prefix = undefined, options) {
218
232
  return new Logger(prefix, options);
219
233
  }
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "33.0.27",
6
+ "version": "33.1.0",
7
7
  "skill": {
8
8
  "namespace": "skill-utils",
9
9
  "upgradeIgnoreList": [
@@ -58,7 +58,7 @@
58
58
  },
59
59
  "dependencies": {
60
60
  "@sprucelabs/globby": "^2.0.515",
61
- "@sprucelabs/schema": "^33.0.23",
61
+ "@sprucelabs/schema": "^33.0.24",
62
62
  "chalk": "^4.1.2",
63
63
  "dotenv": "^17.2.3",
64
64
  "fs-extra": "^11.3.3",