rslog 1.3.2 → 2.0.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.
- package/README.md +5 -9
- package/dist/color.d.ts +17 -8
- package/dist/index.d.ts +1 -0
- package/dist/index.js +39 -33
- package/package.json +12 -11
- package/dist/index.cjs +0 -300
package/README.md
CHANGED
|
@@ -17,7 +17,7 @@ A tiny, intuitive, type-friendly logger for Node.js.
|
|
|
17
17
|
|
|
18
18
|
## Preview
|
|
19
19
|
|
|
20
|
-

|
|
21
21
|
|
|
22
22
|
## Install
|
|
23
23
|
|
|
@@ -35,15 +35,11 @@ pnpm add rslog
|
|
|
35
35
|
bun add rslog
|
|
36
36
|
```
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
Rslog requires Node.js `^20.19.0 || >=22.12.0`. If you are on Node 18, upgrade Node before using the package.
|
|
39
39
|
|
|
40
|
-
|
|
40
|
+
## Usage
|
|
41
41
|
|
|
42
42
|
```js
|
|
43
|
-
// with require
|
|
44
|
-
const { logger } = require('rslog');
|
|
45
|
-
|
|
46
|
-
// with import
|
|
47
43
|
import { logger } from 'rslog';
|
|
48
44
|
```
|
|
49
45
|
|
|
@@ -138,7 +134,7 @@ logger.override({
|
|
|
138
134
|
|
|
139
135
|
## Environment
|
|
140
136
|
|
|
141
|
-
Rslog
|
|
137
|
+
Rslog supports Node.js `^20.19.0 || >=22.12.0`.
|
|
142
138
|
|
|
143
139
|
## Credits
|
|
144
140
|
|
|
@@ -148,4 +144,4 @@ The color implementation of Rslog are modified from [alexeyraspopov/picocolors](
|
|
|
148
144
|
|
|
149
145
|
## License
|
|
150
146
|
|
|
151
|
-
Rslog is [MIT licensed](https://github.com/
|
|
147
|
+
Rslog is [MIT licensed](https://github.com/rstackjs/rslog/blob/main/LICENSE).
|
package/dist/color.d.ts
CHANGED
|
@@ -1,8 +1,17 @@
|
|
|
1
|
-
export type ColorFn = (
|
|
2
|
-
export declare const
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
export type ColorFn = (text: string | number) => string;
|
|
2
|
+
export declare const color: {
|
|
3
|
+
dim: ColorFn;
|
|
4
|
+
red: ColorFn;
|
|
5
|
+
bold: ColorFn;
|
|
6
|
+
blue: ColorFn;
|
|
7
|
+
cyan: ColorFn;
|
|
8
|
+
gray: ColorFn;
|
|
9
|
+
black: ColorFn;
|
|
10
|
+
green: ColorFn;
|
|
11
|
+
white: ColorFn;
|
|
12
|
+
reset: ColorFn;
|
|
13
|
+
yellow: ColorFn;
|
|
14
|
+
magenta: ColorFn;
|
|
15
|
+
underline: ColorFn;
|
|
16
|
+
strikethrough: ColorFn;
|
|
17
|
+
};
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,13 +1,37 @@
|
|
|
1
|
+
import node_util from "node:util";
|
|
1
2
|
import node_process from "node:process";
|
|
2
3
|
import node_os from "node:os";
|
|
3
4
|
import node_tty from "node:tty";
|
|
5
|
+
function checkNodeVersion() {
|
|
6
|
+
const { versions } = process;
|
|
7
|
+
if ("styleText" in node_util || !versions.node || versions.bun || versions.deno) return;
|
|
8
|
+
throw new Error(`Unsupported Node.js version: "${process.versions.node || 'unknown'}". Expected Node.js >= 20.`);
|
|
9
|
+
}
|
|
10
|
+
checkNodeVersion();
|
|
11
|
+
const createStyler = (style)=>(text)=>node_util.styleText(style, String(text));
|
|
12
|
+
const color = {
|
|
13
|
+
dim: createStyler('dim'),
|
|
14
|
+
red: createStyler('red'),
|
|
15
|
+
bold: createStyler('bold'),
|
|
16
|
+
blue: createStyler('blue'),
|
|
17
|
+
cyan: createStyler('cyan'),
|
|
18
|
+
gray: createStyler('gray'),
|
|
19
|
+
black: createStyler('black'),
|
|
20
|
+
green: createStyler('green'),
|
|
21
|
+
white: createStyler('white'),
|
|
22
|
+
reset: createStyler('reset'),
|
|
23
|
+
yellow: createStyler('yellow'),
|
|
24
|
+
magenta: createStyler('magenta'),
|
|
25
|
+
underline: createStyler('underline'),
|
|
26
|
+
strikethrough: createStyler('strikethrough')
|
|
27
|
+
};
|
|
4
28
|
function hasFlag(flag, argv = globalThis.Deno ? globalThis.Deno.args : node_process.argv) {
|
|
5
29
|
const prefix = flag.startsWith('-') ? '' : 1 === flag.length ? '-' : '--';
|
|
6
30
|
const position = argv.indexOf(prefix + flag);
|
|
7
31
|
const terminatorPosition = argv.indexOf('--');
|
|
8
32
|
return -1 !== position && (-1 === terminatorPosition || position < terminatorPosition);
|
|
9
33
|
}
|
|
10
|
-
const { env } = node_process;
|
|
34
|
+
const { env: env } = node_process;
|
|
11
35
|
let flagForceColor;
|
|
12
36
|
if (hasFlag('no-color') || hasFlag('no-colors') || hasFlag('color=false') || hasFlag('color=never')) flagForceColor = 0;
|
|
13
37
|
else if (hasFlag('color') || hasFlag('colors') || hasFlag('color=true') || hasFlag('color=always')) flagForceColor = 1;
|
|
@@ -107,24 +131,6 @@ let errorStackRegExp = /at [^\r\n]{0,200}:\d+:\d+[\s\)]*$/;
|
|
|
107
131
|
let anonymousErrorStackRegExp = /at [^\r\n]{0,200}\(<anonymous>\)$/;
|
|
108
132
|
let indexErrorStackRegExp = /at [^\r\n]{0,200}\(index\s\d+\)$/;
|
|
109
133
|
let isErrorStackMessage = (message)=>errorStackRegExp.test(message) || anonymousErrorStackRegExp.test(message) || indexErrorStackRegExp.test(message);
|
|
110
|
-
let formatter = (open, close, replace = open)=>colorLevel >= 2 ? (input)=>{
|
|
111
|
-
let string = '' + input;
|
|
112
|
-
let index = string.indexOf(close, open.length);
|
|
113
|
-
return ~index ? open + replaceClose(string, close, replace, index) + close : open + string + close;
|
|
114
|
-
} : String;
|
|
115
|
-
let replaceClose = (string, close, replace, index)=>{
|
|
116
|
-
let start = string.substring(0, index) + replace;
|
|
117
|
-
let end = string.substring(index + close.length);
|
|
118
|
-
let nextIndex = end.indexOf(close);
|
|
119
|
-
return ~nextIndex ? start + replaceClose(end, close, replace, nextIndex) : start + end;
|
|
120
|
-
};
|
|
121
|
-
const bold = formatter('\x1b[1m', '\x1b[22m', '\x1b[22m\x1b[1m');
|
|
122
|
-
const red = formatter('\x1b[31m', '\x1b[39m');
|
|
123
|
-
const green = formatter('\x1b[32m', '\x1b[39m');
|
|
124
|
-
const yellow = formatter('\x1b[33m', '\x1b[39m');
|
|
125
|
-
const magenta = formatter('\x1b[35m', '\x1b[39m');
|
|
126
|
-
const cyan = formatter('\x1b[36m', '\x1b[39m');
|
|
127
|
-
const gray = formatter('\x1b[90m', '\x1b[39m');
|
|
128
134
|
let startColor = [
|
|
129
135
|
189,
|
|
130
136
|
255,
|
|
@@ -137,7 +143,7 @@ let endColor = [
|
|
|
137
143
|
];
|
|
138
144
|
let isWord = (char)=>!/[\s\n]/.test(char);
|
|
139
145
|
let gradient = (message)=>{
|
|
140
|
-
if (colorLevel < 3) return 2 === colorLevel ?
|
|
146
|
+
if (colorLevel < 3) return 2 === colorLevel ? color.cyan(message) : message;
|
|
141
147
|
let chars = [
|
|
142
148
|
...message
|
|
143
149
|
];
|
|
@@ -157,7 +163,7 @@ let gradient = (message)=>{
|
|
|
157
163
|
}
|
|
158
164
|
output += `\x1b[38;2;${Math.round(r)};${Math.round(g)};${Math.round(b)}m${char}\x1b[39m`;
|
|
159
165
|
}
|
|
160
|
-
return bold(output);
|
|
166
|
+
return color.bold(output);
|
|
161
167
|
};
|
|
162
168
|
let LOG_LEVEL = {
|
|
163
169
|
silent: -1,
|
|
@@ -171,32 +177,32 @@ let LOG_TYPES = {
|
|
|
171
177
|
error: {
|
|
172
178
|
label: 'error',
|
|
173
179
|
level: 'error',
|
|
174
|
-
color: red
|
|
180
|
+
color: color.red
|
|
175
181
|
},
|
|
176
182
|
warn: {
|
|
177
183
|
label: 'warn',
|
|
178
184
|
level: 'warn',
|
|
179
|
-
color: yellow
|
|
185
|
+
color: color.yellow
|
|
180
186
|
},
|
|
181
187
|
info: {
|
|
182
188
|
label: 'info',
|
|
183
189
|
level: 'info',
|
|
184
|
-
color: cyan
|
|
190
|
+
color: color.cyan
|
|
185
191
|
},
|
|
186
192
|
start: {
|
|
187
193
|
label: 'start',
|
|
188
194
|
level: 'info',
|
|
189
|
-
color: cyan
|
|
195
|
+
color: color.cyan
|
|
190
196
|
},
|
|
191
197
|
ready: {
|
|
192
198
|
label: 'ready',
|
|
193
199
|
level: 'info',
|
|
194
|
-
color: green
|
|
200
|
+
color: color.green
|
|
195
201
|
},
|
|
196
202
|
success: {
|
|
197
203
|
label: 'success',
|
|
198
204
|
level: 'info',
|
|
199
|
-
color: green
|
|
205
|
+
color: color.green
|
|
200
206
|
},
|
|
201
207
|
log: {
|
|
202
208
|
level: 'info'
|
|
@@ -204,14 +210,14 @@ let LOG_TYPES = {
|
|
|
204
210
|
debug: {
|
|
205
211
|
label: 'debug',
|
|
206
212
|
level: 'verbose',
|
|
207
|
-
color: magenta
|
|
213
|
+
color: color.magenta
|
|
208
214
|
}
|
|
209
215
|
};
|
|
210
216
|
const normalizeErrorMessage = (err)=>{
|
|
211
217
|
if (err.stack) {
|
|
212
218
|
let [name, ...rest] = err.stack.split('\n');
|
|
213
219
|
if (name.startsWith('Error: ')) name = name.slice(7);
|
|
214
|
-
return `${name}\n${gray(rest.join('\n'))}`;
|
|
220
|
+
return `${name}\n${color.gray(rest.join('\n'))}`;
|
|
215
221
|
}
|
|
216
222
|
return err.message;
|
|
217
223
|
};
|
|
@@ -226,18 +232,18 @@ let createLogger = (options = {})=>{
|
|
|
226
232
|
let text = '';
|
|
227
233
|
if ('label' in logType) {
|
|
228
234
|
label = (logType.label || '').padEnd(7);
|
|
229
|
-
label = bold(logType.color ? logType.color(label) : label);
|
|
235
|
+
label = color.bold(logType.color ? logType.color(label) : label);
|
|
230
236
|
}
|
|
231
237
|
if (message instanceof Error) {
|
|
232
238
|
text += normalizeErrorMessage(message);
|
|
233
239
|
const { cause } = message;
|
|
234
240
|
if (cause) {
|
|
235
|
-
text += yellow('\n [cause]: ');
|
|
241
|
+
text += color.yellow('\n [cause]: ');
|
|
236
242
|
text += cause instanceof Error ? normalizeErrorMessage(cause) : String(cause);
|
|
237
243
|
}
|
|
238
244
|
} else if ('error' === level && 'string' == typeof message) {
|
|
239
245
|
let lines = message.split('\n');
|
|
240
|
-
text = lines.map((line)=>isErrorStackMessage(line) ? gray(line) : line).join('\n');
|
|
246
|
+
text = lines.map((line)=>isErrorStackMessage(line) ? color.gray(line) : line).join('\n');
|
|
241
247
|
} else text = `${message}`;
|
|
242
248
|
const method = 'error' === level || 'warn' === level ? level : 'log';
|
|
243
249
|
console[method](label.length ? `${label} ${text}` : text, ...args);
|
|
@@ -260,4 +266,4 @@ let createLogger = (options = {})=>{
|
|
|
260
266
|
return logger;
|
|
261
267
|
};
|
|
262
268
|
let src_logger = createLogger();
|
|
263
|
-
export { createLogger, src_logger as logger };
|
|
269
|
+
export { color, createLogger, src_logger as logger };
|
package/package.json
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rslog",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
|
-
"url": "https://github.com/
|
|
6
|
+
"url": "https://github.com/rstackjs/rslog.git"
|
|
7
7
|
},
|
|
8
8
|
"license": "MIT",
|
|
9
9
|
"type": "module",
|
|
10
10
|
"exports": {
|
|
11
11
|
".": {
|
|
12
12
|
"types": "./dist/index.d.ts",
|
|
13
|
-
"
|
|
14
|
-
"require": "./dist/index.cjs"
|
|
13
|
+
"default": "./dist/index.js"
|
|
15
14
|
}
|
|
16
15
|
},
|
|
17
|
-
"main": "./dist/index.cjs",
|
|
18
16
|
"types": "./dist/index.d.ts",
|
|
19
17
|
"files": [
|
|
20
18
|
"dist"
|
|
@@ -28,16 +26,19 @@
|
|
|
28
26
|
"test": "rstest"
|
|
29
27
|
},
|
|
30
28
|
"devDependencies": {
|
|
31
|
-
"@rslib/core": "^0.
|
|
32
|
-
"@rstest/core": "^0.
|
|
33
|
-
"@types/node": "^24.
|
|
29
|
+
"@rslib/core": "^0.19.6",
|
|
30
|
+
"@rstest/core": "^0.8.5",
|
|
31
|
+
"@types/node": "^24.11.0",
|
|
34
32
|
"path-serializer": "^0.5.1",
|
|
35
|
-
"prettier": "~3.
|
|
36
|
-
"strip-ansi": "^7.
|
|
33
|
+
"prettier": "~3.8.1",
|
|
34
|
+
"strip-ansi": "^7.2.0",
|
|
37
35
|
"supports-color": "^10.2.2",
|
|
38
36
|
"typescript": "~5.9.3"
|
|
39
37
|
},
|
|
40
|
-
"packageManager": "pnpm@10.
|
|
38
|
+
"packageManager": "pnpm@10.30.3",
|
|
39
|
+
"engines": {
|
|
40
|
+
"node": "^20.19.0 || >=22.12.0"
|
|
41
|
+
},
|
|
41
42
|
"publishConfig": {
|
|
42
43
|
"access": "public",
|
|
43
44
|
"registry": "https://registry.npmjs.org/"
|
package/dist/index.cjs
DELETED
|
@@ -1,300 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __webpack_require__ = {};
|
|
3
|
-
(()=>{
|
|
4
|
-
__webpack_require__.d = (exports1, definition)=>{
|
|
5
|
-
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: definition[key]
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
})();
|
|
11
|
-
(()=>{
|
|
12
|
-
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
13
|
-
})();
|
|
14
|
-
(()=>{
|
|
15
|
-
__webpack_require__.r = (exports1)=>{
|
|
16
|
-
if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
|
-
value: 'Module'
|
|
18
|
-
});
|
|
19
|
-
Object.defineProperty(exports1, '__esModule', {
|
|
20
|
-
value: true
|
|
21
|
-
});
|
|
22
|
-
};
|
|
23
|
-
})();
|
|
24
|
-
var __webpack_exports__ = {};
|
|
25
|
-
__webpack_require__.r(__webpack_exports__);
|
|
26
|
-
__webpack_require__.d(__webpack_exports__, {
|
|
27
|
-
createLogger: ()=>createLogger,
|
|
28
|
-
logger: ()=>src_logger
|
|
29
|
-
});
|
|
30
|
-
const external_node_process_namespaceObject = require("node:process");
|
|
31
|
-
const external_node_os_namespaceObject = require("node:os");
|
|
32
|
-
const external_node_tty_namespaceObject = require("node:tty");
|
|
33
|
-
function hasFlag(flag, argv = globalThis.Deno ? globalThis.Deno.args : external_node_process_namespaceObject.argv) {
|
|
34
|
-
const prefix = flag.startsWith('-') ? '' : 1 === flag.length ? '-' : '--';
|
|
35
|
-
const position = argv.indexOf(prefix + flag);
|
|
36
|
-
const terminatorPosition = argv.indexOf('--');
|
|
37
|
-
return -1 !== position && (-1 === terminatorPosition || position < terminatorPosition);
|
|
38
|
-
}
|
|
39
|
-
const { env } = external_node_process_namespaceObject;
|
|
40
|
-
let flagForceColor;
|
|
41
|
-
if (hasFlag('no-color') || hasFlag('no-colors') || hasFlag('color=false') || hasFlag('color=never')) flagForceColor = 0;
|
|
42
|
-
else if (hasFlag('color') || hasFlag('colors') || hasFlag('color=true') || hasFlag('color=always')) flagForceColor = 1;
|
|
43
|
-
function envForceColor() {
|
|
44
|
-
if (!('FORCE_COLOR' in env)) return;
|
|
45
|
-
if ('true' === env.FORCE_COLOR) return 1;
|
|
46
|
-
if ('false' === env.FORCE_COLOR) return 0;
|
|
47
|
-
if (0 === env.FORCE_COLOR.length) return 1;
|
|
48
|
-
const level = Math.min(Number.parseInt(env.FORCE_COLOR, 10), 3);
|
|
49
|
-
if (![
|
|
50
|
-
0,
|
|
51
|
-
1,
|
|
52
|
-
2,
|
|
53
|
-
3
|
|
54
|
-
].includes(level)) return;
|
|
55
|
-
return level;
|
|
56
|
-
}
|
|
57
|
-
function translateLevel(level) {
|
|
58
|
-
if (0 === level) return false;
|
|
59
|
-
return {
|
|
60
|
-
level,
|
|
61
|
-
hasBasic: true,
|
|
62
|
-
has256: level >= 2,
|
|
63
|
-
has16m: level >= 3
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
function _supportsColor(haveStream, { streamIsTTY, sniffFlags = true } = {}) {
|
|
67
|
-
const noFlagForceColor = envForceColor();
|
|
68
|
-
if (void 0 !== noFlagForceColor) flagForceColor = noFlagForceColor;
|
|
69
|
-
const forceColor = sniffFlags ? flagForceColor : noFlagForceColor;
|
|
70
|
-
if (0 === forceColor) return 0;
|
|
71
|
-
if (sniffFlags) {
|
|
72
|
-
if (hasFlag('color=16m') || hasFlag('color=full') || hasFlag('color=truecolor')) return 3;
|
|
73
|
-
if (hasFlag('color=256')) return 2;
|
|
74
|
-
}
|
|
75
|
-
if ('TF_BUILD' in env && 'AGENT_NAME' in env) return 1;
|
|
76
|
-
if (haveStream && !streamIsTTY && void 0 === forceColor) return 0;
|
|
77
|
-
const min = forceColor || 0;
|
|
78
|
-
if ('dumb' === env.TERM) return min;
|
|
79
|
-
if ('win32' === external_node_process_namespaceObject.platform) {
|
|
80
|
-
const osRelease = external_node_os_namespaceObject.release().split('.');
|
|
81
|
-
if (Number(osRelease[0]) >= 10 && Number(osRelease[2]) >= 10586) return Number(osRelease[2]) >= 14931 ? 3 : 2;
|
|
82
|
-
return 1;
|
|
83
|
-
}
|
|
84
|
-
if ('CI' in env) {
|
|
85
|
-
if ([
|
|
86
|
-
'GITHUB_ACTIONS',
|
|
87
|
-
'GITEA_ACTIONS',
|
|
88
|
-
'CIRCLECI'
|
|
89
|
-
].some((key)=>key in env)) return 3;
|
|
90
|
-
if ([
|
|
91
|
-
'TRAVIS',
|
|
92
|
-
'APPVEYOR',
|
|
93
|
-
'GITLAB_CI',
|
|
94
|
-
'BUILDKITE',
|
|
95
|
-
'DRONE'
|
|
96
|
-
].some((sign)=>sign in env) || 'codeship' === env.CI_NAME) return 1;
|
|
97
|
-
return min;
|
|
98
|
-
}
|
|
99
|
-
if ('TEAMCITY_VERSION' in env) return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION) ? 1 : 0;
|
|
100
|
-
if ('truecolor' === env.COLORTERM) return 3;
|
|
101
|
-
if ('xterm-kitty' === env.TERM) return 3;
|
|
102
|
-
if ('xterm-ghostty' === env.TERM) return 3;
|
|
103
|
-
if ('wezterm' === env.TERM) return 3;
|
|
104
|
-
if ('TERM_PROGRAM' in env) {
|
|
105
|
-
const version = Number.parseInt((env.TERM_PROGRAM_VERSION || '').split('.')[0], 10);
|
|
106
|
-
switch(env.TERM_PROGRAM){
|
|
107
|
-
case 'iTerm.app':
|
|
108
|
-
return version >= 3 ? 3 : 2;
|
|
109
|
-
case 'Apple_Terminal':
|
|
110
|
-
return 2;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
if (/-256(color)?$/i.test(env.TERM)) return 2;
|
|
114
|
-
if (/^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux/i.test(env.TERM)) return 1;
|
|
115
|
-
if ('COLORTERM' in env) return 1;
|
|
116
|
-
return min;
|
|
117
|
-
}
|
|
118
|
-
function createSupportsColor(stream, options = {}) {
|
|
119
|
-
const level = _supportsColor(stream, {
|
|
120
|
-
streamIsTTY: stream && stream.isTTY,
|
|
121
|
-
...options
|
|
122
|
-
});
|
|
123
|
-
return translateLevel(level);
|
|
124
|
-
}
|
|
125
|
-
const supportsColor = {
|
|
126
|
-
stdout: createSupportsColor({
|
|
127
|
-
isTTY: external_node_tty_namespaceObject.isatty(1)
|
|
128
|
-
}),
|
|
129
|
-
stderr: createSupportsColor({
|
|
130
|
-
isTTY: external_node_tty_namespaceObject.isatty(2)
|
|
131
|
-
})
|
|
132
|
-
};
|
|
133
|
-
const supports_color = supportsColor;
|
|
134
|
-
const colorLevel = supports_color.stdout ? supports_color.stdout.level : 0;
|
|
135
|
-
let errorStackRegExp = /at [^\r\n]{0,200}:\d+:\d+[\s\)]*$/;
|
|
136
|
-
let anonymousErrorStackRegExp = /at [^\r\n]{0,200}\(<anonymous>\)$/;
|
|
137
|
-
let indexErrorStackRegExp = /at [^\r\n]{0,200}\(index\s\d+\)$/;
|
|
138
|
-
let isErrorStackMessage = (message)=>errorStackRegExp.test(message) || anonymousErrorStackRegExp.test(message) || indexErrorStackRegExp.test(message);
|
|
139
|
-
let formatter = (open, close, replace = open)=>colorLevel >= 2 ? (input)=>{
|
|
140
|
-
let string = '' + input;
|
|
141
|
-
let index = string.indexOf(close, open.length);
|
|
142
|
-
return ~index ? open + replaceClose(string, close, replace, index) + close : open + string + close;
|
|
143
|
-
} : String;
|
|
144
|
-
let replaceClose = (string, close, replace, index)=>{
|
|
145
|
-
let start = string.substring(0, index) + replace;
|
|
146
|
-
let end = string.substring(index + close.length);
|
|
147
|
-
let nextIndex = end.indexOf(close);
|
|
148
|
-
return ~nextIndex ? start + replaceClose(end, close, replace, nextIndex) : start + end;
|
|
149
|
-
};
|
|
150
|
-
const bold = formatter('\x1b[1m', '\x1b[22m', '\x1b[22m\x1b[1m');
|
|
151
|
-
const red = formatter('\x1b[31m', '\x1b[39m');
|
|
152
|
-
const green = formatter('\x1b[32m', '\x1b[39m');
|
|
153
|
-
const yellow = formatter('\x1b[33m', '\x1b[39m');
|
|
154
|
-
const magenta = formatter('\x1b[35m', '\x1b[39m');
|
|
155
|
-
const cyan = formatter('\x1b[36m', '\x1b[39m');
|
|
156
|
-
const gray = formatter('\x1b[90m', '\x1b[39m');
|
|
157
|
-
let startColor = [
|
|
158
|
-
189,
|
|
159
|
-
255,
|
|
160
|
-
243
|
|
161
|
-
];
|
|
162
|
-
let endColor = [
|
|
163
|
-
74,
|
|
164
|
-
194,
|
|
165
|
-
154
|
|
166
|
-
];
|
|
167
|
-
let isWord = (char)=>!/[\s\n]/.test(char);
|
|
168
|
-
let gradient = (message)=>{
|
|
169
|
-
if (colorLevel < 3) return 2 === colorLevel ? bold(cyan(message)) : message;
|
|
170
|
-
let chars = [
|
|
171
|
-
...message
|
|
172
|
-
];
|
|
173
|
-
let steps = chars.filter(isWord).length;
|
|
174
|
-
let r = startColor[0];
|
|
175
|
-
let g = startColor[1];
|
|
176
|
-
let b = startColor[2];
|
|
177
|
-
let rStep = (endColor[0] - r) / steps;
|
|
178
|
-
let gStep = (endColor[1] - g) / steps;
|
|
179
|
-
let bStep = (endColor[2] - b) / steps;
|
|
180
|
-
let output = '';
|
|
181
|
-
for (let char of chars){
|
|
182
|
-
if (isWord(char)) {
|
|
183
|
-
r += rStep;
|
|
184
|
-
g += gStep;
|
|
185
|
-
b += bStep;
|
|
186
|
-
}
|
|
187
|
-
output += `\x1b[38;2;${Math.round(r)};${Math.round(g)};${Math.round(b)}m${char}\x1b[39m`;
|
|
188
|
-
}
|
|
189
|
-
return bold(output);
|
|
190
|
-
};
|
|
191
|
-
let LOG_LEVEL = {
|
|
192
|
-
silent: -1,
|
|
193
|
-
error: 0,
|
|
194
|
-
warn: 1,
|
|
195
|
-
info: 2,
|
|
196
|
-
log: 2,
|
|
197
|
-
verbose: 3
|
|
198
|
-
};
|
|
199
|
-
let LOG_TYPES = {
|
|
200
|
-
error: {
|
|
201
|
-
label: 'error',
|
|
202
|
-
level: 'error',
|
|
203
|
-
color: red
|
|
204
|
-
},
|
|
205
|
-
warn: {
|
|
206
|
-
label: 'warn',
|
|
207
|
-
level: 'warn',
|
|
208
|
-
color: yellow
|
|
209
|
-
},
|
|
210
|
-
info: {
|
|
211
|
-
label: 'info',
|
|
212
|
-
level: 'info',
|
|
213
|
-
color: cyan
|
|
214
|
-
},
|
|
215
|
-
start: {
|
|
216
|
-
label: 'start',
|
|
217
|
-
level: 'info',
|
|
218
|
-
color: cyan
|
|
219
|
-
},
|
|
220
|
-
ready: {
|
|
221
|
-
label: 'ready',
|
|
222
|
-
level: 'info',
|
|
223
|
-
color: green
|
|
224
|
-
},
|
|
225
|
-
success: {
|
|
226
|
-
label: 'success',
|
|
227
|
-
level: 'info',
|
|
228
|
-
color: green
|
|
229
|
-
},
|
|
230
|
-
log: {
|
|
231
|
-
level: 'info'
|
|
232
|
-
},
|
|
233
|
-
debug: {
|
|
234
|
-
label: 'debug',
|
|
235
|
-
level: 'verbose',
|
|
236
|
-
color: magenta
|
|
237
|
-
}
|
|
238
|
-
};
|
|
239
|
-
const normalizeErrorMessage = (err)=>{
|
|
240
|
-
if (err.stack) {
|
|
241
|
-
let [name, ...rest] = err.stack.split('\n');
|
|
242
|
-
if (name.startsWith('Error: ')) name = name.slice(7);
|
|
243
|
-
return `${name}\n${gray(rest.join('\n'))}`;
|
|
244
|
-
}
|
|
245
|
-
return err.message;
|
|
246
|
-
};
|
|
247
|
-
let createLogger = (options = {})=>{
|
|
248
|
-
let maxLevel = options.level || 'info';
|
|
249
|
-
let log = (type, message, ...args)=>{
|
|
250
|
-
let logType = LOG_TYPES[type];
|
|
251
|
-
const { level } = logType;
|
|
252
|
-
if (LOG_LEVEL[level] > LOG_LEVEL[maxLevel]) return;
|
|
253
|
-
if (null == message) return console.log();
|
|
254
|
-
let label = '';
|
|
255
|
-
let text = '';
|
|
256
|
-
if ('label' in logType) {
|
|
257
|
-
label = (logType.label || '').padEnd(7);
|
|
258
|
-
label = bold(logType.color ? logType.color(label) : label);
|
|
259
|
-
}
|
|
260
|
-
if (message instanceof Error) {
|
|
261
|
-
text += normalizeErrorMessage(message);
|
|
262
|
-
const { cause } = message;
|
|
263
|
-
if (cause) {
|
|
264
|
-
text += yellow('\n [cause]: ');
|
|
265
|
-
text += cause instanceof Error ? normalizeErrorMessage(cause) : String(cause);
|
|
266
|
-
}
|
|
267
|
-
} else if ('error' === level && 'string' == typeof message) {
|
|
268
|
-
let lines = message.split('\n');
|
|
269
|
-
text = lines.map((line)=>isErrorStackMessage(line) ? gray(line) : line).join('\n');
|
|
270
|
-
} else text = `${message}`;
|
|
271
|
-
const method = 'error' === level || 'warn' === level ? level : 'log';
|
|
272
|
-
console[method](label.length ? `${label} ${text}` : text, ...args);
|
|
273
|
-
};
|
|
274
|
-
let logger = {
|
|
275
|
-
greet: (message)=>log('log', gradient(message))
|
|
276
|
-
};
|
|
277
|
-
Object.keys(LOG_TYPES).forEach((key)=>{
|
|
278
|
-
logger[key] = (...args)=>log(key, ...args);
|
|
279
|
-
});
|
|
280
|
-
Object.defineProperty(logger, 'level', {
|
|
281
|
-
get: ()=>maxLevel,
|
|
282
|
-
set (val) {
|
|
283
|
-
maxLevel = val;
|
|
284
|
-
}
|
|
285
|
-
});
|
|
286
|
-
logger.override = (customLogger)=>{
|
|
287
|
-
Object.assign(logger, customLogger);
|
|
288
|
-
};
|
|
289
|
-
return logger;
|
|
290
|
-
};
|
|
291
|
-
let src_logger = createLogger();
|
|
292
|
-
exports.createLogger = __webpack_exports__.createLogger;
|
|
293
|
-
exports.logger = __webpack_exports__.logger;
|
|
294
|
-
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
|
295
|
-
"createLogger",
|
|
296
|
-
"logger"
|
|
297
|
-
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
|
298
|
-
Object.defineProperty(exports, '__esModule', {
|
|
299
|
-
value: true
|
|
300
|
-
});
|