@tomjs/logger 1.4.0 → 2.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 +156 -0
- package/dist/{index.d.ts → index.d.cts} +3 -2
- package/dist/index.d.mts +3 -2
- package/dist/index.mjs +121 -101
- package/package.json +21 -21
- package/dist/index.js +0 -134
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
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 log_symbols = require("log-symbols");
|
|
34
|
+
log_symbols = __toESM(log_symbols);
|
|
35
|
+
let picocolors = require("picocolors");
|
|
36
|
+
picocolors = __toESM(picocolors);
|
|
37
|
+
let strip_ansi = require("strip-ansi");
|
|
38
|
+
strip_ansi = __toESM(strip_ansi);
|
|
39
|
+
|
|
40
|
+
//#region src/index.ts
|
|
41
|
+
let timeFormatter;
|
|
42
|
+
function getTimeFormatter() {
|
|
43
|
+
timeFormatter ??= new Intl.DateTimeFormat(void 0, {
|
|
44
|
+
hour: "numeric",
|
|
45
|
+
minute: "numeric",
|
|
46
|
+
second: "numeric"
|
|
47
|
+
});
|
|
48
|
+
return timeFormatter;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* log tool
|
|
52
|
+
*/
|
|
53
|
+
var Logger = class {
|
|
54
|
+
constructor(options) {
|
|
55
|
+
this._opts = {};
|
|
56
|
+
this.setOptions(Object.assign({}, options));
|
|
57
|
+
}
|
|
58
|
+
initLogDir() {
|
|
59
|
+
const { directory, cleanup } = this._opts;
|
|
60
|
+
if (!directory) return;
|
|
61
|
+
const root = this._opts.root || node_path.default.join(node_os.default.homedir(), ".tomjs");
|
|
62
|
+
const logDir = node_path.default.join(root, directory);
|
|
63
|
+
this._logDir = logDir;
|
|
64
|
+
if (!node_fs.default.existsSync(logDir)) node_fs.default.mkdirSync(logDir, { recursive: true });
|
|
65
|
+
node_fs.default.readdirSync(logDir).forEach((s) => {
|
|
66
|
+
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 });
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
format(...args) {
|
|
70
|
+
return args.map((s) => typeof s === "object" ? JSON.stringify(s) : s || "").join(" ");
|
|
71
|
+
}
|
|
72
|
+
_writeLog(...args) {
|
|
73
|
+
if (!this._logDir) return;
|
|
74
|
+
const logFile = node_path.default.join(this._logDir, `${(0, dayjs.default)().format("YYYYMMDD")}.log`);
|
|
75
|
+
node_fs.default.appendFileSync(logFile, `${(0, dayjs.default)().format("YYYY-MM-DD HH:mm:ss.SSS")} ${(0, strip_ansi.default)(this.format(...args))}\n`);
|
|
76
|
+
}
|
|
77
|
+
_log(...args) {
|
|
78
|
+
this._writeLog(...args);
|
|
79
|
+
let list = [...args];
|
|
80
|
+
if (this._opts.time) list = [picocolors.default.dim(getTimeFormatter().format(/* @__PURE__ */ new Date())), ...list];
|
|
81
|
+
console.log(list.map((s) => typeof s === "object" ? "%o" : "%s").join(" "), ...list);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* set debug mode or not
|
|
85
|
+
*/
|
|
86
|
+
enableDebug(debug) {
|
|
87
|
+
this._opts.debug = !!debug;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* set debug mode or not
|
|
91
|
+
*/
|
|
92
|
+
setOptions(options) {
|
|
93
|
+
this._opts = Object.assign({}, options);
|
|
94
|
+
this.initLogDir();
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* like console.log
|
|
98
|
+
*/
|
|
99
|
+
log(...args) {
|
|
100
|
+
this._log(...args);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* write log to file
|
|
104
|
+
*/
|
|
105
|
+
write(...args) {
|
|
106
|
+
this._writeLog(...args);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* only show in debug mode
|
|
110
|
+
*/
|
|
111
|
+
debug(...args) {
|
|
112
|
+
if (this._opts.debug) this._log(...args.map((s) => {
|
|
113
|
+
if (typeof s !== "object") return picocolors.default.gray(s);
|
|
114
|
+
return s;
|
|
115
|
+
}));
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* add the specified red prefix or error symbol before the log content
|
|
119
|
+
*/
|
|
120
|
+
error(...args) {
|
|
121
|
+
const { prefix } = this._opts;
|
|
122
|
+
this._log(prefix ? picocolors.default.red(prefix) : log_symbols.default.error, ...args);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* add the specified blue prefix or info symbol before the log content
|
|
126
|
+
*/
|
|
127
|
+
info(...args) {
|
|
128
|
+
const { prefix } = this._opts;
|
|
129
|
+
this._log(prefix ? picocolors.default.blue(prefix) : log_symbols.default.info, ...args);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* add the specified green prefix or success symbol before the log content
|
|
133
|
+
*/
|
|
134
|
+
success(...args) {
|
|
135
|
+
const { prefix } = this._opts;
|
|
136
|
+
this._log(prefix ? picocolors.default.green(prefix) : log_symbols.default.success, ...args);
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* add the specified yellow prefix or warning symbol before the log content
|
|
140
|
+
*/
|
|
141
|
+
warning(...args) {
|
|
142
|
+
const { prefix } = this._opts;
|
|
143
|
+
this._log(prefix ? picocolors.default.yellow(prefix) : log_symbols.default.warning, ...args);
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* add the specified yellow prefix or warning symbol before the log content
|
|
147
|
+
*/
|
|
148
|
+
warn(...args) {
|
|
149
|
+
this.warning(...args);
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
var src_default = Logger;
|
|
153
|
+
|
|
154
|
+
//#endregion
|
|
155
|
+
exports.Logger = Logger;
|
|
156
|
+
exports.default = src_default;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
//#region src/index.d.ts
|
|
1
2
|
interface LoggerOptions {
|
|
2
3
|
/**
|
|
3
4
|
* log prefix
|
|
@@ -79,5 +80,5 @@ declare class Logger {
|
|
|
79
80
|
*/
|
|
80
81
|
warn(...args: any[]): void;
|
|
81
82
|
}
|
|
82
|
-
|
|
83
|
-
export { Logger,
|
|
83
|
+
//#endregion
|
|
84
|
+
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
|
|
@@ -79,5 +80,5 @@ declare class Logger {
|
|
|
79
80
|
*/
|
|
80
81
|
warn(...args: any[]): void;
|
|
81
82
|
}
|
|
82
|
-
|
|
83
|
-
export { Logger,
|
|
83
|
+
//#endregion
|
|
84
|
+
export { Logger, Logger as default, LoggerOptions };
|
package/dist/index.mjs
CHANGED
|
@@ -1,104 +1,124 @@
|
|
|
1
|
-
import fs from
|
|
2
|
-
import os from
|
|
3
|
-
import path from
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import stripAnsi from
|
|
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 logSymbols from "log-symbols";
|
|
6
|
+
import chalk from "picocolors";
|
|
7
|
+
import stripAnsi from "strip-ansi";
|
|
8
|
+
|
|
9
|
+
//#region src/index.ts
|
|
10
|
+
let timeFormatter;
|
|
11
|
+
function getTimeFormatter() {
|
|
12
|
+
timeFormatter ??= new Intl.DateTimeFormat(void 0, {
|
|
13
|
+
hour: "numeric",
|
|
14
|
+
minute: "numeric",
|
|
15
|
+
second: "numeric"
|
|
16
|
+
});
|
|
17
|
+
return timeFormatter;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* log tool
|
|
21
|
+
*/
|
|
8
22
|
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
|
-
|
|
23
|
+
constructor(options) {
|
|
24
|
+
this._opts = {};
|
|
25
|
+
this.setOptions(Object.assign({}, options));
|
|
26
|
+
}
|
|
27
|
+
initLogDir() {
|
|
28
|
+
const { directory, cleanup } = this._opts;
|
|
29
|
+
if (!directory) return;
|
|
30
|
+
const root = this._opts.root || path.join(os.homedir(), ".tomjs");
|
|
31
|
+
const logDir = path.join(root, directory);
|
|
32
|
+
this._logDir = logDir;
|
|
33
|
+
if (!fs.existsSync(logDir)) fs.mkdirSync(logDir, { recursive: true });
|
|
34
|
+
fs.readdirSync(logDir).forEach((s) => {
|
|
35
|
+
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 });
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
format(...args) {
|
|
39
|
+
return args.map((s) => typeof s === "object" ? JSON.stringify(s) : s || "").join(" ");
|
|
40
|
+
}
|
|
41
|
+
_writeLog(...args) {
|
|
42
|
+
if (!this._logDir) return;
|
|
43
|
+
const logFile = path.join(this._logDir, `${dayjs().format("YYYYMMDD")}.log`);
|
|
44
|
+
fs.appendFileSync(logFile, `${dayjs().format("YYYY-MM-DD HH:mm:ss.SSS")} ${stripAnsi(this.format(...args))}\n`);
|
|
45
|
+
}
|
|
46
|
+
_log(...args) {
|
|
47
|
+
this._writeLog(...args);
|
|
48
|
+
let list = [...args];
|
|
49
|
+
if (this._opts.time) list = [chalk.dim(getTimeFormatter().format(/* @__PURE__ */ new Date())), ...list];
|
|
50
|
+
console.log(list.map((s) => typeof s === "object" ? "%o" : "%s").join(" "), ...list);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* set debug mode or not
|
|
54
|
+
*/
|
|
55
|
+
enableDebug(debug) {
|
|
56
|
+
this._opts.debug = !!debug;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* set debug mode or not
|
|
60
|
+
*/
|
|
61
|
+
setOptions(options) {
|
|
62
|
+
this._opts = Object.assign({}, options);
|
|
63
|
+
this.initLogDir();
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* like console.log
|
|
67
|
+
*/
|
|
68
|
+
log(...args) {
|
|
69
|
+
this._log(...args);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* write log to file
|
|
73
|
+
*/
|
|
74
|
+
write(...args) {
|
|
75
|
+
this._writeLog(...args);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* only show in debug mode
|
|
79
|
+
*/
|
|
80
|
+
debug(...args) {
|
|
81
|
+
if (this._opts.debug) this._log(...args.map((s) => {
|
|
82
|
+
if (typeof s !== "object") return chalk.gray(s);
|
|
83
|
+
return s;
|
|
84
|
+
}));
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* add the specified red prefix or error symbol before the log content
|
|
88
|
+
*/
|
|
89
|
+
error(...args) {
|
|
90
|
+
const { prefix } = this._opts;
|
|
91
|
+
this._log(prefix ? chalk.red(prefix) : logSymbols.error, ...args);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* add the specified blue prefix or info symbol before the log content
|
|
95
|
+
*/
|
|
96
|
+
info(...args) {
|
|
97
|
+
const { prefix } = this._opts;
|
|
98
|
+
this._log(prefix ? chalk.blue(prefix) : logSymbols.info, ...args);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* add the specified green prefix or success symbol before the log content
|
|
102
|
+
*/
|
|
103
|
+
success(...args) {
|
|
104
|
+
const { prefix } = this._opts;
|
|
105
|
+
this._log(prefix ? chalk.green(prefix) : logSymbols.success, ...args);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* add the specified yellow prefix or warning symbol before the log content
|
|
109
|
+
*/
|
|
110
|
+
warning(...args) {
|
|
111
|
+
const { prefix } = this._opts;
|
|
112
|
+
this._log(prefix ? chalk.yellow(prefix) : logSymbols.warning, ...args);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* add the specified yellow prefix or warning symbol before the log content
|
|
116
|
+
*/
|
|
117
|
+
warn(...args) {
|
|
118
|
+
this.warning(...args);
|
|
119
|
+
}
|
|
102
120
|
};
|
|
103
121
|
var src_default = Logger;
|
|
104
|
-
|
|
122
|
+
|
|
123
|
+
//#endregion
|
|
124
|
+
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": "2.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
|
"log-symbols": "^4.1.0",
|
|
47
|
+
"picocolors": "^1.1.1",
|
|
48
48
|
"strip-ansi": "^6.0.1"
|
|
49
49
|
},
|
|
50
50
|
"scripts": {
|
|
51
|
-
"build": "
|
|
51
|
+
"build": "tsdown",
|
|
52
52
|
"test": "vitest"
|
|
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;
|