rslog 1.2.2 → 1.2.4
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 +10 -1
- package/dist/color.d.ts +8 -0
- package/dist/constants.d.ts +48 -0
- package/dist/createLogger.d.ts +2 -0
- package/dist/gradient.d.ts +1 -0
- package/dist/index.d.ts +3 -66
- package/dist/index.js +274 -0
- package/dist/index.mjs +216 -273
- package/dist/types.d.ts +19 -0
- package/dist/utils.d.ts +2 -0
- package/package.json +13 -22
- package/dist/index.cjs +0 -334
package/README.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# Rslog
|
|
2
2
|
|
|
3
|
+
<p>
|
|
4
|
+
<a href="https://npmjs.com/package/rslog">
|
|
5
|
+
<img src="https://img.shields.io/npm/v/rslog?style=flat-square&colorA=564341&colorB=EDED91" alt="npm version" />
|
|
6
|
+
</a>
|
|
7
|
+
<img src="https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="license" />
|
|
8
|
+
<a href="https://npmcharts.com/compare/rslog?minimal=true"><img src="https://img.shields.io/npm/dm/rslog.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="downloads" /></a>
|
|
9
|
+
</p>
|
|
10
|
+
|
|
3
11
|
A tiny, intuitive, type-friendly logger for Node.js.
|
|
4
12
|
|
|
5
13
|
- **Tiny**. [2kB gzipped](https://bundlephobia.com/package/rslog@1).
|
|
@@ -99,7 +107,8 @@ The log levels of each method are as follows:
|
|
|
99
107
|
|
|
100
108
|
| Level | Method |
|
|
101
109
|
| ------- | ----------------------------------- |
|
|
102
|
-
|
|
|
110
|
+
| silent | No log will be output |
|
|
111
|
+
| error | only `error` logs |
|
|
103
112
|
| warn | `warn` |
|
|
104
113
|
| info | `info`, `start`, `ready`, `success` |
|
|
105
114
|
| log | `log` |
|
package/dist/color.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type ColorFn = (input: string | number | null | undefined) => string;
|
|
2
|
+
export declare const bold: ColorFn;
|
|
3
|
+
export declare const red: ColorFn;
|
|
4
|
+
export declare const green: ColorFn;
|
|
5
|
+
export declare const yellow: ColorFn;
|
|
6
|
+
export declare const magenta: ColorFn;
|
|
7
|
+
export declare const cyan: ColorFn;
|
|
8
|
+
export declare const gray: ColorFn;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export declare let LOG_LEVEL: {
|
|
2
|
+
readonly silent: -1;
|
|
3
|
+
readonly error: 0;
|
|
4
|
+
readonly warn: 1;
|
|
5
|
+
readonly info: 2;
|
|
6
|
+
readonly log: 3;
|
|
7
|
+
readonly verbose: 4;
|
|
8
|
+
};
|
|
9
|
+
export declare let LOG_TYPES: {
|
|
10
|
+
error: {
|
|
11
|
+
label: string;
|
|
12
|
+
level: "error";
|
|
13
|
+
color: import("./color").ColorFn;
|
|
14
|
+
};
|
|
15
|
+
warn: {
|
|
16
|
+
label: string;
|
|
17
|
+
level: "warn";
|
|
18
|
+
color: import("./color").ColorFn;
|
|
19
|
+
};
|
|
20
|
+
info: {
|
|
21
|
+
label: string;
|
|
22
|
+
level: "info";
|
|
23
|
+
color: import("./color").ColorFn;
|
|
24
|
+
};
|
|
25
|
+
start: {
|
|
26
|
+
label: string;
|
|
27
|
+
level: "info";
|
|
28
|
+
color: import("./color").ColorFn;
|
|
29
|
+
};
|
|
30
|
+
ready: {
|
|
31
|
+
label: string;
|
|
32
|
+
level: "info";
|
|
33
|
+
color: import("./color").ColorFn;
|
|
34
|
+
};
|
|
35
|
+
success: {
|
|
36
|
+
label: string;
|
|
37
|
+
level: "info";
|
|
38
|
+
color: import("./color").ColorFn;
|
|
39
|
+
};
|
|
40
|
+
log: {
|
|
41
|
+
level: "log";
|
|
42
|
+
};
|
|
43
|
+
debug: {
|
|
44
|
+
label: string;
|
|
45
|
+
level: "verbose";
|
|
46
|
+
color: import("./color").ColorFn;
|
|
47
|
+
};
|
|
48
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare let gradient: (message: string) => string;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,66 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
error: {
|
|
5
|
-
label: string;
|
|
6
|
-
level: "error";
|
|
7
|
-
color: ColorFn;
|
|
8
|
-
};
|
|
9
|
-
warn: {
|
|
10
|
-
label: string;
|
|
11
|
-
level: "warn";
|
|
12
|
-
color: ColorFn;
|
|
13
|
-
};
|
|
14
|
-
info: {
|
|
15
|
-
label: string;
|
|
16
|
-
level: "info";
|
|
17
|
-
color: ColorFn;
|
|
18
|
-
};
|
|
19
|
-
start: {
|
|
20
|
-
label: string;
|
|
21
|
-
level: "info";
|
|
22
|
-
color: ColorFn;
|
|
23
|
-
};
|
|
24
|
-
ready: {
|
|
25
|
-
label: string;
|
|
26
|
-
level: "info";
|
|
27
|
-
color: ColorFn;
|
|
28
|
-
};
|
|
29
|
-
success: {
|
|
30
|
-
label: string;
|
|
31
|
-
level: "info";
|
|
32
|
-
color: ColorFn;
|
|
33
|
-
};
|
|
34
|
-
log: {
|
|
35
|
-
level: "log";
|
|
36
|
-
};
|
|
37
|
-
debug: {
|
|
38
|
-
label: string;
|
|
39
|
-
level: "verbose";
|
|
40
|
-
color: ColorFn;
|
|
41
|
-
};
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
type LogLevel = 'error' | 'warn' | 'info' | 'log' | 'verbose';
|
|
45
|
-
type LogMessage = unknown;
|
|
46
|
-
interface LogType {
|
|
47
|
-
label?: string;
|
|
48
|
-
level: LogLevel;
|
|
49
|
-
color?: ColorFn;
|
|
50
|
-
}
|
|
51
|
-
type LogFunction = (message?: LogMessage, ...args: any[]) => void;
|
|
52
|
-
interface Options {
|
|
53
|
-
level?: LogLevel;
|
|
54
|
-
}
|
|
55
|
-
type LogMethods = keyof typeof LOG_TYPES;
|
|
56
|
-
type Logger = Record<LogMethods, LogFunction> & {
|
|
57
|
-
greet: (message: string) => void;
|
|
58
|
-
level: LogLevel;
|
|
59
|
-
override: (customLogger: Partial<Record<LogMethods, LogFunction>>) => void;
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
declare let createLogger: (options?: Options) => Logger;
|
|
63
|
-
|
|
64
|
-
declare let logger: Logger;
|
|
65
|
-
|
|
66
|
-
export { type LogFunction, type LogLevel, type LogMessage, type LogType, type Logger, type Options, createLogger, logger };
|
|
1
|
+
export { createLogger } from './createLogger';
|
|
2
|
+
export declare let logger: import("./types").Logger;
|
|
3
|
+
export type { Options, Logger, LogType, LogLevel, LogMessage, LogFunction, } from './types';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = function(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 = function(obj, prop) {
|
|
13
|
+
return Object.prototype.hasOwnProperty.call(obj, prop);
|
|
14
|
+
};
|
|
15
|
+
})();
|
|
16
|
+
(()=>{
|
|
17
|
+
__webpack_require__.r = function(exports1) {
|
|
18
|
+
if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
19
|
+
value: 'Module'
|
|
20
|
+
});
|
|
21
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
22
|
+
value: true
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
})();
|
|
26
|
+
var __webpack_exports__ = {};
|
|
27
|
+
__webpack_require__.r(__webpack_exports__);
|
|
28
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
29
|
+
createLogger: ()=>createLogger,
|
|
30
|
+
logger: ()=>src_logger
|
|
31
|
+
});
|
|
32
|
+
const external_node_process_namespaceObject = require("node:process");
|
|
33
|
+
const external_node_os_namespaceObject = require("node:os");
|
|
34
|
+
const external_node_tty_namespaceObject = require("node:tty");
|
|
35
|
+
function hasFlag(flag, argv = globalThis.Deno ? globalThis.Deno.args : external_node_process_namespaceObject.argv) {
|
|
36
|
+
const prefix = flag.startsWith('-') ? '' : 1 === flag.length ? '-' : '--';
|
|
37
|
+
const position = argv.indexOf(prefix + flag);
|
|
38
|
+
const terminatorPosition = argv.indexOf('--');
|
|
39
|
+
return -1 !== position && (-1 === terminatorPosition || position < terminatorPosition);
|
|
40
|
+
}
|
|
41
|
+
const { env } = external_node_process_namespaceObject;
|
|
42
|
+
let flagForceColor;
|
|
43
|
+
if (hasFlag('no-color') || hasFlag('no-colors') || hasFlag('color=false') || hasFlag('color=never')) flagForceColor = 0;
|
|
44
|
+
else if (hasFlag('color') || hasFlag('colors') || hasFlag('color=true') || hasFlag('color=always')) flagForceColor = 1;
|
|
45
|
+
function envForceColor() {
|
|
46
|
+
if ('FORCE_COLOR' in env) {
|
|
47
|
+
if ('true' === env.FORCE_COLOR) return 1;
|
|
48
|
+
if ('false' === env.FORCE_COLOR) return 0;
|
|
49
|
+
return 0 === env.FORCE_COLOR.length ? 1 : Math.min(Number.parseInt(env.FORCE_COLOR, 10), 3);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function translateLevel(level) {
|
|
53
|
+
if (0 === level) return false;
|
|
54
|
+
return {
|
|
55
|
+
level,
|
|
56
|
+
hasBasic: true,
|
|
57
|
+
has256: level >= 2,
|
|
58
|
+
has16m: level >= 3
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function _supportsColor(haveStream, { streamIsTTY, sniffFlags = true } = {}) {
|
|
62
|
+
const noFlagForceColor = envForceColor();
|
|
63
|
+
if (void 0 !== noFlagForceColor) flagForceColor = noFlagForceColor;
|
|
64
|
+
const forceColor = sniffFlags ? flagForceColor : noFlagForceColor;
|
|
65
|
+
if (0 === forceColor) return 0;
|
|
66
|
+
if (sniffFlags) {
|
|
67
|
+
if (hasFlag('color=16m') || hasFlag('color=full') || hasFlag('color=truecolor')) return 3;
|
|
68
|
+
if (hasFlag('color=256')) return 2;
|
|
69
|
+
}
|
|
70
|
+
if ('TF_BUILD' in env && 'AGENT_NAME' in env) return 1;
|
|
71
|
+
if (haveStream && !streamIsTTY && void 0 === forceColor) return 0;
|
|
72
|
+
const min = forceColor || 0;
|
|
73
|
+
if ('dumb' === env.TERM) return min;
|
|
74
|
+
if ('win32' === external_node_process_namespaceObject.platform) {
|
|
75
|
+
const osRelease = external_node_os_namespaceObject.release().split('.');
|
|
76
|
+
if (Number(osRelease[0]) >= 10 && Number(osRelease[2]) >= 10586) return Number(osRelease[2]) >= 14931 ? 3 : 2;
|
|
77
|
+
return 1;
|
|
78
|
+
}
|
|
79
|
+
if ('CI' in env) {
|
|
80
|
+
if ('GITHUB_ACTIONS' in env || 'GITEA_ACTIONS' in env) return 3;
|
|
81
|
+
if ([
|
|
82
|
+
'TRAVIS',
|
|
83
|
+
'CIRCLECI',
|
|
84
|
+
'APPVEYOR',
|
|
85
|
+
'GITLAB_CI',
|
|
86
|
+
'BUILDKITE',
|
|
87
|
+
'DRONE'
|
|
88
|
+
].some((sign)=>sign in env) || 'codeship' === env.CI_NAME) return 1;
|
|
89
|
+
return min;
|
|
90
|
+
}
|
|
91
|
+
if ('TEAMCITY_VERSION' in env) return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION) ? 1 : 0;
|
|
92
|
+
if ('truecolor' === env.COLORTERM) return 3;
|
|
93
|
+
if ('xterm-kitty' === env.TERM) return 3;
|
|
94
|
+
if ('TERM_PROGRAM' in env) {
|
|
95
|
+
const version = Number.parseInt((env.TERM_PROGRAM_VERSION || '').split('.')[0], 10);
|
|
96
|
+
switch(env.TERM_PROGRAM){
|
|
97
|
+
case 'iTerm.app':
|
|
98
|
+
return version >= 3 ? 3 : 2;
|
|
99
|
+
case 'Apple_Terminal':
|
|
100
|
+
return 2;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (/-256(color)?$/i.test(env.TERM)) return 2;
|
|
104
|
+
if (/^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux/i.test(env.TERM)) return 1;
|
|
105
|
+
if ('COLORTERM' in env) return 1;
|
|
106
|
+
return min;
|
|
107
|
+
}
|
|
108
|
+
function createSupportsColor(stream, options = {}) {
|
|
109
|
+
const level = _supportsColor(stream, {
|
|
110
|
+
streamIsTTY: stream && stream.isTTY,
|
|
111
|
+
...options
|
|
112
|
+
});
|
|
113
|
+
return translateLevel(level);
|
|
114
|
+
}
|
|
115
|
+
const supportsColor = {
|
|
116
|
+
stdout: createSupportsColor({
|
|
117
|
+
isTTY: external_node_tty_namespaceObject.isatty(1)
|
|
118
|
+
}),
|
|
119
|
+
stderr: createSupportsColor({
|
|
120
|
+
isTTY: external_node_tty_namespaceObject.isatty(2)
|
|
121
|
+
})
|
|
122
|
+
};
|
|
123
|
+
const supports_color = supportsColor;
|
|
124
|
+
const colorLevel = supports_color.stdout ? supports_color.stdout.level : 0;
|
|
125
|
+
let errorStackRegExp = /at\s.*:\d+:\d+[\s\)]*$/;
|
|
126
|
+
let anonymousErrorStackRegExp = /at\s.*\(<anonymous>\)$/;
|
|
127
|
+
let isErrorStackMessage = (message)=>errorStackRegExp.test(message) || anonymousErrorStackRegExp.test(message);
|
|
128
|
+
let formatter = (open, close, replace = open)=>colorLevel >= 2 ? (input)=>{
|
|
129
|
+
let string = '' + input;
|
|
130
|
+
let index = string.indexOf(close, open.length);
|
|
131
|
+
return ~index ? open + replaceClose(string, close, replace, index) + close : open + string + close;
|
|
132
|
+
} : String;
|
|
133
|
+
let replaceClose = (string, close, replace, index)=>{
|
|
134
|
+
let start = string.substring(0, index) + replace;
|
|
135
|
+
let end = string.substring(index + close.length);
|
|
136
|
+
let nextIndex = end.indexOf(close);
|
|
137
|
+
return ~nextIndex ? start + replaceClose(end, close, replace, nextIndex) : start + end;
|
|
138
|
+
};
|
|
139
|
+
const bold = formatter('\x1b[1m', '\x1b[22m', '\x1b[22m\x1b[1m');
|
|
140
|
+
const red = formatter('\x1b[31m', '\x1b[39m');
|
|
141
|
+
const green = formatter('\x1b[32m', '\x1b[39m');
|
|
142
|
+
const yellow = formatter('\x1b[33m', '\x1b[39m');
|
|
143
|
+
const magenta = formatter('\x1b[35m', '\x1b[39m');
|
|
144
|
+
const cyan = formatter('\x1b[36m', '\x1b[39m');
|
|
145
|
+
const gray = formatter('\x1b[90m', '\x1b[39m');
|
|
146
|
+
let startColor = [
|
|
147
|
+
189,
|
|
148
|
+
255,
|
|
149
|
+
243
|
|
150
|
+
];
|
|
151
|
+
let endColor = [
|
|
152
|
+
74,
|
|
153
|
+
194,
|
|
154
|
+
154
|
|
155
|
+
];
|
|
156
|
+
let isWord = (char)=>!/[\s\n]/.test(char);
|
|
157
|
+
let gradient = (message)=>{
|
|
158
|
+
if (colorLevel < 3) return 2 === colorLevel ? bold(cyan(message)) : message;
|
|
159
|
+
let chars = [
|
|
160
|
+
...message
|
|
161
|
+
];
|
|
162
|
+
let steps = chars.filter(isWord).length;
|
|
163
|
+
let r = startColor[0];
|
|
164
|
+
let g = startColor[1];
|
|
165
|
+
let b = startColor[2];
|
|
166
|
+
let rStep = (endColor[0] - r) / steps;
|
|
167
|
+
let gStep = (endColor[1] - g) / steps;
|
|
168
|
+
let bStep = (endColor[2] - b) / steps;
|
|
169
|
+
let output = '';
|
|
170
|
+
for (let char of chars){
|
|
171
|
+
if (isWord(char)) {
|
|
172
|
+
r += rStep;
|
|
173
|
+
g += gStep;
|
|
174
|
+
b += bStep;
|
|
175
|
+
}
|
|
176
|
+
output += `\x1b[38;2;${Math.round(r)};${Math.round(g)};${Math.round(b)}m${char}\x1b[39m`;
|
|
177
|
+
}
|
|
178
|
+
return bold(output);
|
|
179
|
+
};
|
|
180
|
+
let LOG_LEVEL = {
|
|
181
|
+
silent: -1,
|
|
182
|
+
error: 0,
|
|
183
|
+
warn: 1,
|
|
184
|
+
info: 2,
|
|
185
|
+
log: 3,
|
|
186
|
+
verbose: 4
|
|
187
|
+
};
|
|
188
|
+
let LOG_TYPES = {
|
|
189
|
+
error: {
|
|
190
|
+
label: 'error',
|
|
191
|
+
level: 'error',
|
|
192
|
+
color: red
|
|
193
|
+
},
|
|
194
|
+
warn: {
|
|
195
|
+
label: 'warn',
|
|
196
|
+
level: 'warn',
|
|
197
|
+
color: yellow
|
|
198
|
+
},
|
|
199
|
+
info: {
|
|
200
|
+
label: 'info',
|
|
201
|
+
level: 'info',
|
|
202
|
+
color: cyan
|
|
203
|
+
},
|
|
204
|
+
start: {
|
|
205
|
+
label: 'start',
|
|
206
|
+
level: 'info',
|
|
207
|
+
color: cyan
|
|
208
|
+
},
|
|
209
|
+
ready: {
|
|
210
|
+
label: 'ready',
|
|
211
|
+
level: 'info',
|
|
212
|
+
color: green
|
|
213
|
+
},
|
|
214
|
+
success: {
|
|
215
|
+
label: 'success',
|
|
216
|
+
level: 'info',
|
|
217
|
+
color: green
|
|
218
|
+
},
|
|
219
|
+
log: {
|
|
220
|
+
level: 'log'
|
|
221
|
+
},
|
|
222
|
+
debug: {
|
|
223
|
+
label: 'debug',
|
|
224
|
+
level: 'verbose',
|
|
225
|
+
color: magenta
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
let createLogger = (options = {})=>{
|
|
229
|
+
let maxLevel = options.level || 'log';
|
|
230
|
+
let log = (type, message, ...args)=>{
|
|
231
|
+
if (LOG_LEVEL[LOG_TYPES[type].level] > LOG_LEVEL[maxLevel]) return;
|
|
232
|
+
if (null == message) return console.log();
|
|
233
|
+
let logType = LOG_TYPES[type];
|
|
234
|
+
let label = '';
|
|
235
|
+
let text = '';
|
|
236
|
+
if ('label' in logType) {
|
|
237
|
+
label = (logType.label || '').padEnd(7);
|
|
238
|
+
label = bold(logType.color ? logType.color(label) : label);
|
|
239
|
+
}
|
|
240
|
+
if (message instanceof Error) {
|
|
241
|
+
if (message.stack) {
|
|
242
|
+
let [name, ...rest] = message.stack.split('\n');
|
|
243
|
+
if (name.startsWith('Error: ')) name = name.slice(7);
|
|
244
|
+
text = `${name}\n${gray(rest.join('\n'))}`;
|
|
245
|
+
} else text = message.message;
|
|
246
|
+
} else if ('error' === logType.level && 'string' == typeof message) {
|
|
247
|
+
let lines = message.split('\n');
|
|
248
|
+
text = lines.map((line)=>isErrorStackMessage(line) ? gray(line) : line).join('\n');
|
|
249
|
+
} else text = `${message}`;
|
|
250
|
+
console.log(label.length ? `${label} ${text}` : text, ...args);
|
|
251
|
+
};
|
|
252
|
+
let logger = {
|
|
253
|
+
greet: (message)=>log('log', gradient(message))
|
|
254
|
+
};
|
|
255
|
+
Object.keys(LOG_TYPES).forEach((key)=>{
|
|
256
|
+
logger[key] = (...args)=>log(key, ...args);
|
|
257
|
+
});
|
|
258
|
+
Object.defineProperty(logger, 'level', {
|
|
259
|
+
get: ()=>maxLevel,
|
|
260
|
+
set (val) {
|
|
261
|
+
maxLevel = val;
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
logger.override = (customLogger)=>{
|
|
265
|
+
Object.assign(logger, customLogger);
|
|
266
|
+
};
|
|
267
|
+
return logger;
|
|
268
|
+
};
|
|
269
|
+
let src_logger = createLogger();
|
|
270
|
+
var __webpack_export_target__ = exports;
|
|
271
|
+
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
|
272
|
+
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
|
|
273
|
+
value: true
|
|
274
|
+
});
|