@tomjs/logger 1.4.0 → 3.0.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.
- package/dist/index.cjs +176 -0
- package/dist/{index.d.ts → index.d.cts} +10 -3
- package/dist/index.d.mts +10 -3
- package/dist/index.mjs +141 -101
- package/package.json +23 -23
- package/dist/index.js +0 -134
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
|
+
//#region rolldown:runtime
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
|
+
get: ((k) => from[k]).bind(null, key),
|
|
14
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
20
|
+
value: mod,
|
|
21
|
+
enumerable: true
|
|
22
|
+
}) : target, mod));
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
let node_fs = require("node:fs");
|
|
26
|
+
node_fs = __toESM(node_fs);
|
|
27
|
+
let node_os = require("node:os");
|
|
28
|
+
node_os = __toESM(node_os);
|
|
29
|
+
let node_path = require("node:path");
|
|
30
|
+
node_path = __toESM(node_path);
|
|
31
|
+
let dayjs = require("dayjs");
|
|
32
|
+
dayjs = __toESM(dayjs);
|
|
33
|
+
let picocolors = require("picocolors");
|
|
34
|
+
picocolors = __toESM(picocolors);
|
|
35
|
+
let strip_ansi = require("strip-ansi");
|
|
36
|
+
strip_ansi = __toESM(strip_ansi);
|
|
37
|
+
let is_unicode_supported = require("is-unicode-supported");
|
|
38
|
+
is_unicode_supported = __toESM(is_unicode_supported);
|
|
39
|
+
|
|
40
|
+
//#region src/colors.ts
|
|
41
|
+
const supported = (0, is_unicode_supported.default)();
|
|
42
|
+
const logColors = {
|
|
43
|
+
info: picocolors.default.cyan,
|
|
44
|
+
success: picocolors.default.green,
|
|
45
|
+
warn: picocolors.default.yellow,
|
|
46
|
+
warning: picocolors.default.yellow,
|
|
47
|
+
error: picocolors.default.red,
|
|
48
|
+
debug: picocolors.default.gray,
|
|
49
|
+
log: picocolors.default.gray
|
|
50
|
+
};
|
|
51
|
+
const logSymbols = {
|
|
52
|
+
info: picocolors.default.cyan(supported ? "ℹ" : "i"),
|
|
53
|
+
success: picocolors.default.green(supported ? "✔" : "√"),
|
|
54
|
+
warn: picocolors.default.yellow(supported ? "⚠" : "!"),
|
|
55
|
+
warning: picocolors.default.yellow(supported ? "⚠" : "!"),
|
|
56
|
+
error: picocolors.default.red(supported ? "✖" : "x"),
|
|
57
|
+
debug: picocolors.default.gray(supported ? "◎" : "#"),
|
|
58
|
+
log: picocolors.default.gray(supported ? "◎" : "#")
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
//#endregion
|
|
62
|
+
//#region src/index.ts
|
|
63
|
+
let timeFormatter;
|
|
64
|
+
function getTimeFormatter() {
|
|
65
|
+
timeFormatter ??= new Intl.DateTimeFormat(void 0, {
|
|
66
|
+
hour: "numeric",
|
|
67
|
+
minute: "numeric",
|
|
68
|
+
second: "numeric"
|
|
69
|
+
});
|
|
70
|
+
return timeFormatter;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* log tool
|
|
74
|
+
*/
|
|
75
|
+
var Logger = class {
|
|
76
|
+
constructor(options) {
|
|
77
|
+
this._opts = {};
|
|
78
|
+
this.setOptions(Object.assign({}, options));
|
|
79
|
+
}
|
|
80
|
+
initLogDir() {
|
|
81
|
+
const { directory, cleanup } = this._opts;
|
|
82
|
+
if (!directory) return;
|
|
83
|
+
const root = this._opts.root || node_path.default.join(node_os.default.homedir(), ".tomjs");
|
|
84
|
+
const logDir = node_path.default.join(root, directory);
|
|
85
|
+
this._logDir = logDir;
|
|
86
|
+
if (!node_fs.default.existsSync(logDir)) node_fs.default.mkdirSync(logDir, { recursive: true });
|
|
87
|
+
node_fs.default.readdirSync(logDir).forEach((s) => {
|
|
88
|
+
if ((0, dayjs.default)(s.substring(0, 8)).isBefore((0, dayjs.default)().endOf("day").subtract(Math.max(1, cleanup ?? 30), "day"))) node_fs.default.rmSync(node_path.default.join(logDir, s), { force: true });
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
format(...args) {
|
|
92
|
+
return args.map((s) => typeof s === "object" ? JSON.stringify(s) : s || "").join(" ");
|
|
93
|
+
}
|
|
94
|
+
_writeLog(...args) {
|
|
95
|
+
if (!this._logDir) return;
|
|
96
|
+
const logFile = node_path.default.join(this._logDir, `${(0, dayjs.default)().format("YYYYMMDD")}.log`);
|
|
97
|
+
node_fs.default.appendFileSync(logFile, `${(0, dayjs.default)().format("YYYY-MM-DD HH:mm:ss.SSS")} ${(0, strip_ansi.default)(this.format(...args))}\n`);
|
|
98
|
+
}
|
|
99
|
+
_log(type, ...args) {
|
|
100
|
+
this._writeLog(type, ...args);
|
|
101
|
+
const flag = this._opts.flag || "symbol";
|
|
102
|
+
const preList = [];
|
|
103
|
+
if (flag === "time") preList.push(picocolors.default.dim(getTimeFormatter().format(/* @__PURE__ */ new Date())));
|
|
104
|
+
else if (flag === "symbol") preList.push(logSymbols[type]);
|
|
105
|
+
const { prefix } = this._opts;
|
|
106
|
+
if (prefix) preList.push(logColors[type](picocolors.default.bold(prefix)));
|
|
107
|
+
const list = preList.concat(args);
|
|
108
|
+
console.log(preList.concat(args).map((s) => typeof s === "object" ? "%o" : "%s").join(" "), ...list);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* set debug mode or not
|
|
112
|
+
*/
|
|
113
|
+
enableDebug(debug) {
|
|
114
|
+
this._opts.debug = !!debug;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* set debug mode or not
|
|
118
|
+
*/
|
|
119
|
+
setOptions(options) {
|
|
120
|
+
this._opts = Object.assign({}, this._opts, options);
|
|
121
|
+
this.initLogDir();
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* like console.log
|
|
125
|
+
*/
|
|
126
|
+
log(...args) {
|
|
127
|
+
this._log("log", ...args);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* write log to file
|
|
131
|
+
*/
|
|
132
|
+
write(...args) {
|
|
133
|
+
this._writeLog(...args);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* only show in debug mode
|
|
137
|
+
*/
|
|
138
|
+
debug(...args) {
|
|
139
|
+
if (this._opts.debug) this._log("log", ...args);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* add the specified red prefix or error symbol before the log content
|
|
143
|
+
*/
|
|
144
|
+
error(...args) {
|
|
145
|
+
this._log(`error`, ...args);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* add the specified blue prefix or info symbol before the log content
|
|
149
|
+
*/
|
|
150
|
+
info(...args) {
|
|
151
|
+
this._log("info", ...args);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* add the specified green prefix or success symbol before the log content
|
|
155
|
+
*/
|
|
156
|
+
success(...args) {
|
|
157
|
+
this._log("success", ...args);
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* add the specified yellow prefix or warning symbol before the log content
|
|
161
|
+
*/
|
|
162
|
+
warning(...args) {
|
|
163
|
+
this._log("warn", ...args);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* add the specified yellow prefix or warning symbol before the log content
|
|
167
|
+
*/
|
|
168
|
+
warn(...args) {
|
|
169
|
+
this.warning(...args);
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
var src_default = Logger;
|
|
173
|
+
|
|
174
|
+
//#endregion
|
|
175
|
+
exports.Logger = Logger;
|
|
176
|
+
exports.default = src_default;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
//#region src/index.d.ts
|
|
1
2
|
interface LoggerOptions {
|
|
2
3
|
/**
|
|
3
4
|
* log prefix
|
|
@@ -9,10 +10,16 @@ interface LoggerOptions {
|
|
|
9
10
|
*/
|
|
10
11
|
debug?: boolean;
|
|
11
12
|
/**
|
|
12
|
-
* show time in log
|
|
13
|
+
* show time in log. use `flag:"time"` replace time
|
|
13
14
|
* @default false
|
|
15
|
+
* @deprecated
|
|
14
16
|
*/
|
|
15
17
|
time?: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* show log symbols
|
|
20
|
+
* @default 'symbol'
|
|
21
|
+
*/
|
|
22
|
+
flag?: 'time' | 'symbol' | 'none';
|
|
16
23
|
/**
|
|
17
24
|
* specify the log directory name
|
|
18
25
|
*/
|
|
@@ -79,5 +86,5 @@ declare class Logger {
|
|
|
79
86
|
*/
|
|
80
87
|
warn(...args: any[]): void;
|
|
81
88
|
}
|
|
82
|
-
|
|
83
|
-
export { Logger,
|
|
89
|
+
//#endregion
|
|
90
|
+
export { Logger, Logger as default, LoggerOptions };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
//#region src/index.d.ts
|
|
1
2
|
interface LoggerOptions {
|
|
2
3
|
/**
|
|
3
4
|
* log prefix
|
|
@@ -9,10 +10,16 @@ interface LoggerOptions {
|
|
|
9
10
|
*/
|
|
10
11
|
debug?: boolean;
|
|
11
12
|
/**
|
|
12
|
-
* show time in log
|
|
13
|
+
* show time in log. use `flag:"time"` replace time
|
|
13
14
|
* @default false
|
|
15
|
+
* @deprecated
|
|
14
16
|
*/
|
|
15
17
|
time?: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* show log symbols
|
|
20
|
+
* @default 'symbol'
|
|
21
|
+
*/
|
|
22
|
+
flag?: 'time' | 'symbol' | 'none';
|
|
16
23
|
/**
|
|
17
24
|
* specify the log directory name
|
|
18
25
|
*/
|
|
@@ -79,5 +86,5 @@ declare class Logger {
|
|
|
79
86
|
*/
|
|
80
87
|
warn(...args: any[]): void;
|
|
81
88
|
}
|
|
82
|
-
|
|
83
|
-
export { Logger,
|
|
89
|
+
//#endregion
|
|
90
|
+
export { Logger, Logger as default, LoggerOptions };
|
package/dist/index.mjs
CHANGED
|
@@ -1,104 +1,144 @@
|
|
|
1
|
-
import fs from
|
|
2
|
-
import os from
|
|
3
|
-
import path from
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import dayjs from "dayjs";
|
|
5
|
+
import colors from "picocolors";
|
|
6
|
+
import stripAnsi from "strip-ansi";
|
|
7
|
+
import isUnicodeSupported from "is-unicode-supported";
|
|
8
|
+
|
|
9
|
+
//#region src/colors.ts
|
|
10
|
+
const supported = isUnicodeSupported();
|
|
11
|
+
const logColors = {
|
|
12
|
+
info: colors.cyan,
|
|
13
|
+
success: colors.green,
|
|
14
|
+
warn: colors.yellow,
|
|
15
|
+
warning: colors.yellow,
|
|
16
|
+
error: colors.red,
|
|
17
|
+
debug: colors.gray,
|
|
18
|
+
log: colors.gray
|
|
19
|
+
};
|
|
20
|
+
const logSymbols = {
|
|
21
|
+
info: colors.cyan(supported ? "ℹ" : "i"),
|
|
22
|
+
success: colors.green(supported ? "✔" : "√"),
|
|
23
|
+
warn: colors.yellow(supported ? "⚠" : "!"),
|
|
24
|
+
warning: colors.yellow(supported ? "⚠" : "!"),
|
|
25
|
+
error: colors.red(supported ? "✖" : "x"),
|
|
26
|
+
debug: colors.gray(supported ? "◎" : "#"),
|
|
27
|
+
log: colors.gray(supported ? "◎" : "#")
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region src/index.ts
|
|
32
|
+
let timeFormatter;
|
|
33
|
+
function getTimeFormatter() {
|
|
34
|
+
timeFormatter ??= new Intl.DateTimeFormat(void 0, {
|
|
35
|
+
hour: "numeric",
|
|
36
|
+
minute: "numeric",
|
|
37
|
+
second: "numeric"
|
|
38
|
+
});
|
|
39
|
+
return timeFormatter;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* log tool
|
|
43
|
+
*/
|
|
8
44
|
var Logger = class {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
45
|
+
constructor(options) {
|
|
46
|
+
this._opts = {};
|
|
47
|
+
this.setOptions(Object.assign({}, options));
|
|
48
|
+
}
|
|
49
|
+
initLogDir() {
|
|
50
|
+
const { directory, cleanup } = this._opts;
|
|
51
|
+
if (!directory) return;
|
|
52
|
+
const root = this._opts.root || path.join(os.homedir(), ".tomjs");
|
|
53
|
+
const logDir = path.join(root, directory);
|
|
54
|
+
this._logDir = logDir;
|
|
55
|
+
if (!fs.existsSync(logDir)) fs.mkdirSync(logDir, { recursive: true });
|
|
56
|
+
fs.readdirSync(logDir).forEach((s) => {
|
|
57
|
+
if (dayjs(s.substring(0, 8)).isBefore(dayjs().endOf("day").subtract(Math.max(1, cleanup ?? 30), "day"))) fs.rmSync(path.join(logDir, s), { force: true });
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
format(...args) {
|
|
61
|
+
return args.map((s) => typeof s === "object" ? JSON.stringify(s) : s || "").join(" ");
|
|
62
|
+
}
|
|
63
|
+
_writeLog(...args) {
|
|
64
|
+
if (!this._logDir) return;
|
|
65
|
+
const logFile = path.join(this._logDir, `${dayjs().format("YYYYMMDD")}.log`);
|
|
66
|
+
fs.appendFileSync(logFile, `${dayjs().format("YYYY-MM-DD HH:mm:ss.SSS")} ${stripAnsi(this.format(...args))}\n`);
|
|
67
|
+
}
|
|
68
|
+
_log(type, ...args) {
|
|
69
|
+
this._writeLog(type, ...args);
|
|
70
|
+
const flag = this._opts.flag || "symbol";
|
|
71
|
+
const preList = [];
|
|
72
|
+
if (flag === "time") preList.push(colors.dim(getTimeFormatter().format(/* @__PURE__ */ new Date())));
|
|
73
|
+
else if (flag === "symbol") preList.push(logSymbols[type]);
|
|
74
|
+
const { prefix } = this._opts;
|
|
75
|
+
if (prefix) preList.push(logColors[type](colors.bold(prefix)));
|
|
76
|
+
const list = preList.concat(args);
|
|
77
|
+
console.log(preList.concat(args).map((s) => typeof s === "object" ? "%o" : "%s").join(" "), ...list);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* set debug mode or not
|
|
81
|
+
*/
|
|
82
|
+
enableDebug(debug) {
|
|
83
|
+
this._opts.debug = !!debug;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* set debug mode or not
|
|
87
|
+
*/
|
|
88
|
+
setOptions(options) {
|
|
89
|
+
this._opts = Object.assign({}, this._opts, options);
|
|
90
|
+
this.initLogDir();
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* like console.log
|
|
94
|
+
*/
|
|
95
|
+
log(...args) {
|
|
96
|
+
this._log("log", ...args);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* write log to file
|
|
100
|
+
*/
|
|
101
|
+
write(...args) {
|
|
102
|
+
this._writeLog(...args);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* only show in debug mode
|
|
106
|
+
*/
|
|
107
|
+
debug(...args) {
|
|
108
|
+
if (this._opts.debug) this._log("log", ...args);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* add the specified red prefix or error symbol before the log content
|
|
112
|
+
*/
|
|
113
|
+
error(...args) {
|
|
114
|
+
this._log(`error`, ...args);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* add the specified blue prefix or info symbol before the log content
|
|
118
|
+
*/
|
|
119
|
+
info(...args) {
|
|
120
|
+
this._log("info", ...args);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* add the specified green prefix or success symbol before the log content
|
|
124
|
+
*/
|
|
125
|
+
success(...args) {
|
|
126
|
+
this._log("success", ...args);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* add the specified yellow prefix or warning symbol before the log content
|
|
130
|
+
*/
|
|
131
|
+
warning(...args) {
|
|
132
|
+
this._log("warn", ...args);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* add the specified yellow prefix or warning symbol before the log content
|
|
136
|
+
*/
|
|
137
|
+
warn(...args) {
|
|
138
|
+
this.warning(...args);
|
|
139
|
+
}
|
|
102
140
|
};
|
|
103
141
|
var src_default = Logger;
|
|
104
|
-
|
|
142
|
+
|
|
143
|
+
//#endregion
|
|
144
|
+
export { Logger, src_default as default };
|
package/package.json
CHANGED
|
@@ -1,31 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tomjs/logger",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "logger for `node.js`",
|
|
5
|
-
"keywords": [
|
|
6
|
-
"node",
|
|
7
|
-
"utils"
|
|
8
|
-
],
|
|
9
5
|
"author": {
|
|
10
6
|
"name": "Tom Gao",
|
|
11
7
|
"email": "tom@tomgao.cc"
|
|
12
8
|
},
|
|
13
9
|
"license": "MIT",
|
|
14
|
-
"
|
|
15
|
-
|
|
16
|
-
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/tomjs/utils.git",
|
|
13
|
+
"directory": "packages/logger"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"node",
|
|
17
|
+
"utils"
|
|
18
|
+
],
|
|
17
19
|
"exports": {
|
|
18
20
|
".": {
|
|
19
|
-
"require": {
|
|
20
|
-
"default": "./dist/index.js",
|
|
21
|
-
"types": "./dist/index.d.ts"
|
|
22
|
-
},
|
|
23
21
|
"import": {
|
|
24
|
-
"
|
|
25
|
-
"
|
|
22
|
+
"types": "./dist/index.d.mts",
|
|
23
|
+
"default": "./dist/index.mjs"
|
|
24
|
+
},
|
|
25
|
+
"require": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"default": "./dist/index.js"
|
|
26
28
|
}
|
|
27
29
|
}
|
|
28
30
|
},
|
|
31
|
+
"main": "./dist/index.js",
|
|
32
|
+
"module": "./dist/index.mjs",
|
|
33
|
+
"types": "./dist/index.d.ts",
|
|
29
34
|
"files": [
|
|
30
35
|
"dist"
|
|
31
36
|
],
|
|
@@ -36,19 +41,14 @@
|
|
|
36
41
|
"access": "public",
|
|
37
42
|
"registry": "https://registry.npmjs.org/"
|
|
38
43
|
},
|
|
39
|
-
"repository": {
|
|
40
|
-
"type": "git",
|
|
41
|
-
"url": "git+https://github.com/tomjs/utils.git",
|
|
42
|
-
"directory": "packages/logger"
|
|
43
|
-
},
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"chalk": "^4.1.2",
|
|
46
45
|
"dayjs": "^1.11.13",
|
|
47
|
-
"
|
|
46
|
+
"is-unicode-supported": "^2.1.0",
|
|
47
|
+
"picocolors": "^1.1.1",
|
|
48
48
|
"strip-ansi": "^6.0.1"
|
|
49
49
|
},
|
|
50
50
|
"scripts": {
|
|
51
|
-
"build": "
|
|
52
|
-
"
|
|
51
|
+
"build": "tsdown",
|
|
52
|
+
"print": "tsx ./scripts/print.ts"
|
|
53
53
|
}
|
|
54
54
|
}
|
package/dist/index.js
DELETED
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
-
function _interopRequireDefault(obj) {
|
|
4
|
-
return obj && obj.__esModule ? obj : { default: obj };
|
|
5
|
-
}
|
|
6
|
-
function _nullishCoalesce(lhs, rhsFn) {
|
|
7
|
-
if (lhs != null) {
|
|
8
|
-
return lhs;
|
|
9
|
-
} else {
|
|
10
|
-
return rhsFn();
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
var _fs = require('fs');
|
|
14
|
-
var _fs2 = _interopRequireDefault(_fs);
|
|
15
|
-
var _os = require('os');
|
|
16
|
-
var _os2 = _interopRequireDefault(_os);
|
|
17
|
-
var _path = require('path');
|
|
18
|
-
var _path2 = _interopRequireDefault(_path);
|
|
19
|
-
var _chalk = require('chalk');
|
|
20
|
-
var _chalk2 = _interopRequireDefault(_chalk);
|
|
21
|
-
var _dayjs = require('dayjs');
|
|
22
|
-
var _dayjs2 = _interopRequireDefault(_dayjs);
|
|
23
|
-
var _logsymbols = require('log-symbols');
|
|
24
|
-
var _logsymbols2 = _interopRequireDefault(_logsymbols);
|
|
25
|
-
var _stripansi = require('strip-ansi');
|
|
26
|
-
var _stripansi2 = _interopRequireDefault(_stripansi);
|
|
27
|
-
var Logger = class {
|
|
28
|
-
constructor(options) {
|
|
29
|
-
this._opts = {};
|
|
30
|
-
this.setOptions(Object.assign({}, options));
|
|
31
|
-
}
|
|
32
|
-
initLogDir() {
|
|
33
|
-
const { directory, cleanup } = this._opts;
|
|
34
|
-
if (!directory) {
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
const root = this._opts.root || _path2.default.join(_os2.default.homedir(), '.tomjs');
|
|
38
|
-
const logDir = _path2.default.join(root, directory);
|
|
39
|
-
this._logDir = logDir;
|
|
40
|
-
if (!_fs2.default.existsSync(logDir)) {
|
|
41
|
-
_fs2.default.mkdirSync(logDir, { recursive: true });
|
|
42
|
-
}
|
|
43
|
-
_fs2.default.readdirSync(logDir).forEach(s => {
|
|
44
|
-
if (
|
|
45
|
-
_dayjs2.default.call(void 0, s.substring(0, 8)).isBefore(
|
|
46
|
-
_dayjs2.default
|
|
47
|
-
.call(void 0)
|
|
48
|
-
.endOf('day')
|
|
49
|
-
.subtract(
|
|
50
|
-
Math.max(
|
|
51
|
-
1,
|
|
52
|
-
_nullishCoalesce(cleanup, () => 30),
|
|
53
|
-
),
|
|
54
|
-
'day',
|
|
55
|
-
),
|
|
56
|
-
)
|
|
57
|
-
) {
|
|
58
|
-
_fs2.default.rmSync(_path2.default.join(logDir, s), { force: true });
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
format(...args) {
|
|
63
|
-
return args.map(s => (typeof s === 'object' ? JSON.stringify(s) : s || '')).join(' ');
|
|
64
|
-
}
|
|
65
|
-
_writeLog(...args) {
|
|
66
|
-
if (!this._logDir) {
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
const logFile = _path2.default.join(
|
|
70
|
-
this._logDir,
|
|
71
|
-
`${_dayjs2.default.call(void 0).format('YYYYMMDD')}.log`,
|
|
72
|
-
);
|
|
73
|
-
_fs2.default.appendFileSync(
|
|
74
|
-
logFile,
|
|
75
|
-
`${_dayjs2.default.call(void 0).format('YYYY-MM-DD HH:mm:ss.SSS')} ${_stripansi2.default.call(void 0, this.format(...args))}
|
|
76
|
-
`,
|
|
77
|
-
);
|
|
78
|
-
}
|
|
79
|
-
_log(...args) {
|
|
80
|
-
this._writeLog(...args);
|
|
81
|
-
let list = [...args];
|
|
82
|
-
if (this._opts.time) {
|
|
83
|
-
list = [_chalk2.default.gray(_dayjs2.default.call(void 0).format('HH:mm:ss')), ...list];
|
|
84
|
-
}
|
|
85
|
-
console.log(list.map(s => (typeof s === 'object' ? '%o' : '%s')).join(' '), ...list);
|
|
86
|
-
}
|
|
87
|
-
enableDebug(debug) {
|
|
88
|
-
this._opts.debug = !!debug;
|
|
89
|
-
}
|
|
90
|
-
setOptions(options) {
|
|
91
|
-
this._opts = Object.assign({}, options);
|
|
92
|
-
this.initLogDir();
|
|
93
|
-
}
|
|
94
|
-
log(...args) {
|
|
95
|
-
this._log(...args);
|
|
96
|
-
}
|
|
97
|
-
write(...args) {
|
|
98
|
-
this._writeLog(...args);
|
|
99
|
-
}
|
|
100
|
-
debug(...args) {
|
|
101
|
-
if (this._opts.debug) {
|
|
102
|
-
this._log(
|
|
103
|
-
...args.map(s => {
|
|
104
|
-
if (typeof s !== 'object') {
|
|
105
|
-
return _chalk2.default.gray(s);
|
|
106
|
-
}
|
|
107
|
-
return s;
|
|
108
|
-
}),
|
|
109
|
-
);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
error(...args) {
|
|
113
|
-
const { prefix } = this._opts;
|
|
114
|
-
this._log(prefix ? _chalk2.default.red(prefix) : _logsymbols2.default.error, ...args);
|
|
115
|
-
}
|
|
116
|
-
info(...args) {
|
|
117
|
-
const { prefix } = this._opts;
|
|
118
|
-
this._log(prefix ? _chalk2.default.blue(prefix) : _logsymbols2.default.info, ...args);
|
|
119
|
-
}
|
|
120
|
-
success(...args) {
|
|
121
|
-
const { prefix } = this._opts;
|
|
122
|
-
this._log(prefix ? _chalk2.default.green(prefix) : _logsymbols2.default.success, ...args);
|
|
123
|
-
}
|
|
124
|
-
warning(...args) {
|
|
125
|
-
const { prefix } = this._opts;
|
|
126
|
-
this._log(prefix ? _chalk2.default.yellow(prefix) : _logsymbols2.default.warning, ...args);
|
|
127
|
-
}
|
|
128
|
-
warn(...args) {
|
|
129
|
-
this.warning(...args);
|
|
130
|
-
}
|
|
131
|
-
};
|
|
132
|
-
var src_default = Logger;
|
|
133
|
-
exports.Logger = Logger;
|
|
134
|
-
exports.default = src_default;
|