@tomjs/logger 2.0.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 CHANGED
@@ -30,13 +30,35 @@ let node_path = require("node:path");
30
30
  node_path = __toESM(node_path);
31
31
  let dayjs = require("dayjs");
32
32
  dayjs = __toESM(dayjs);
33
- let log_symbols = require("log-symbols");
34
- log_symbols = __toESM(log_symbols);
35
33
  let picocolors = require("picocolors");
36
34
  picocolors = __toESM(picocolors);
37
35
  let strip_ansi = require("strip-ansi");
38
36
  strip_ansi = __toESM(strip_ansi);
37
+ let is_unicode_supported = require("is-unicode-supported");
38
+ is_unicode_supported = __toESM(is_unicode_supported);
39
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
40
62
  //#region src/index.ts
41
63
  let timeFormatter;
42
64
  function getTimeFormatter() {
@@ -74,11 +96,16 @@ var Logger = class {
74
96
  const logFile = node_path.default.join(this._logDir, `${(0, dayjs.default)().format("YYYYMMDD")}.log`);
75
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`);
76
98
  }
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);
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);
82
109
  }
83
110
  /**
84
111
  * set debug mode or not
@@ -90,14 +117,14 @@ var Logger = class {
90
117
  * set debug mode or not
91
118
  */
92
119
  setOptions(options) {
93
- this._opts = Object.assign({}, options);
120
+ this._opts = Object.assign({}, this._opts, options);
94
121
  this.initLogDir();
95
122
  }
96
123
  /**
97
124
  * like console.log
98
125
  */
99
126
  log(...args) {
100
- this._log(...args);
127
+ this._log("log", ...args);
101
128
  }
102
129
  /**
103
130
  * write log to file
@@ -109,38 +136,31 @@ var Logger = class {
109
136
  * only show in debug mode
110
137
  */
111
138
  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
- }));
139
+ if (this._opts.debug) this._log("log", ...args);
116
140
  }
117
141
  /**
118
142
  * add the specified red prefix or error symbol before the log content
119
143
  */
120
144
  error(...args) {
121
- const { prefix } = this._opts;
122
- this._log(prefix ? picocolors.default.red(prefix) : log_symbols.default.error, ...args);
145
+ this._log(`error`, ...args);
123
146
  }
124
147
  /**
125
148
  * add the specified blue prefix or info symbol before the log content
126
149
  */
127
150
  info(...args) {
128
- const { prefix } = this._opts;
129
- this._log(prefix ? picocolors.default.blue(prefix) : log_symbols.default.info, ...args);
151
+ this._log("info", ...args);
130
152
  }
131
153
  /**
132
154
  * add the specified green prefix or success symbol before the log content
133
155
  */
134
156
  success(...args) {
135
- const { prefix } = this._opts;
136
- this._log(prefix ? picocolors.default.green(prefix) : log_symbols.default.success, ...args);
157
+ this._log("success", ...args);
137
158
  }
138
159
  /**
139
160
  * add the specified yellow prefix or warning symbol before the log content
140
161
  */
141
162
  warning(...args) {
142
- const { prefix } = this._opts;
143
- this._log(prefix ? picocolors.default.yellow(prefix) : log_symbols.default.warning, ...args);
163
+ this._log("warn", ...args);
144
164
  }
145
165
  /**
146
166
  * add the specified yellow prefix or warning symbol before the log content
package/dist/index.d.cts CHANGED
@@ -10,10 +10,16 @@ interface LoggerOptions {
10
10
  */
11
11
  debug?: boolean;
12
12
  /**
13
- * show time in log
13
+ * show time in log. use `flag:"time"` replace time
14
14
  * @default false
15
+ * @deprecated
15
16
  */
16
17
  time?: boolean;
18
+ /**
19
+ * show log symbols
20
+ * @default 'symbol'
21
+ */
22
+ flag?: 'time' | 'symbol' | 'none';
17
23
  /**
18
24
  * specify the log directory name
19
25
  */
package/dist/index.d.mts CHANGED
@@ -10,10 +10,16 @@ interface LoggerOptions {
10
10
  */
11
11
  debug?: boolean;
12
12
  /**
13
- * show time in log
13
+ * show time in log. use `flag:"time"` replace time
14
14
  * @default false
15
+ * @deprecated
15
16
  */
16
17
  time?: boolean;
18
+ /**
19
+ * show log symbols
20
+ * @default 'symbol'
21
+ */
22
+ flag?: 'time' | 'symbol' | 'none';
17
23
  /**
18
24
  * specify the log directory name
19
25
  */
package/dist/index.mjs CHANGED
@@ -2,10 +2,32 @@ import fs from "node:fs";
2
2
  import os from "node:os";
3
3
  import path from "node:path";
4
4
  import dayjs from "dayjs";
5
- import logSymbols from "log-symbols";
6
- import chalk from "picocolors";
5
+ import colors from "picocolors";
7
6
  import stripAnsi from "strip-ansi";
7
+ import isUnicodeSupported from "is-unicode-supported";
8
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
9
31
  //#region src/index.ts
10
32
  let timeFormatter;
11
33
  function getTimeFormatter() {
@@ -43,11 +65,16 @@ var Logger = class {
43
65
  const logFile = path.join(this._logDir, `${dayjs().format("YYYYMMDD")}.log`);
44
66
  fs.appendFileSync(logFile, `${dayjs().format("YYYY-MM-DD HH:mm:ss.SSS")} ${stripAnsi(this.format(...args))}\n`);
45
67
  }
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);
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);
51
78
  }
52
79
  /**
53
80
  * set debug mode or not
@@ -59,14 +86,14 @@ var Logger = class {
59
86
  * set debug mode or not
60
87
  */
61
88
  setOptions(options) {
62
- this._opts = Object.assign({}, options);
89
+ this._opts = Object.assign({}, this._opts, options);
63
90
  this.initLogDir();
64
91
  }
65
92
  /**
66
93
  * like console.log
67
94
  */
68
95
  log(...args) {
69
- this._log(...args);
96
+ this._log("log", ...args);
70
97
  }
71
98
  /**
72
99
  * write log to file
@@ -78,38 +105,31 @@ var Logger = class {
78
105
  * only show in debug mode
79
106
  */
80
107
  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
- }));
108
+ if (this._opts.debug) this._log("log", ...args);
85
109
  }
86
110
  /**
87
111
  * add the specified red prefix or error symbol before the log content
88
112
  */
89
113
  error(...args) {
90
- const { prefix } = this._opts;
91
- this._log(prefix ? chalk.red(prefix) : logSymbols.error, ...args);
114
+ this._log(`error`, ...args);
92
115
  }
93
116
  /**
94
117
  * add the specified blue prefix or info symbol before the log content
95
118
  */
96
119
  info(...args) {
97
- const { prefix } = this._opts;
98
- this._log(prefix ? chalk.blue(prefix) : logSymbols.info, ...args);
120
+ this._log("info", ...args);
99
121
  }
100
122
  /**
101
123
  * add the specified green prefix or success symbol before the log content
102
124
  */
103
125
  success(...args) {
104
- const { prefix } = this._opts;
105
- this._log(prefix ? chalk.green(prefix) : logSymbols.success, ...args);
126
+ this._log("success", ...args);
106
127
  }
107
128
  /**
108
129
  * add the specified yellow prefix or warning symbol before the log content
109
130
  */
110
131
  warning(...args) {
111
- const { prefix } = this._opts;
112
- this._log(prefix ? chalk.yellow(prefix) : logSymbols.warning, ...args);
132
+ this._log("warn", ...args);
113
133
  }
114
134
  /**
115
135
  * add the specified yellow prefix or warning symbol before the log content
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tomjs/logger",
3
- "version": "2.0.0",
3
+ "version": "3.0.0",
4
4
  "description": "logger for `node.js`",
5
5
  "author": {
6
6
  "name": "Tom Gao",
@@ -43,12 +43,12 @@
43
43
  },
44
44
  "dependencies": {
45
45
  "dayjs": "^1.11.13",
46
- "log-symbols": "^4.1.0",
46
+ "is-unicode-supported": "^2.1.0",
47
47
  "picocolors": "^1.1.1",
48
48
  "strip-ansi": "^6.0.1"
49
49
  },
50
50
  "scripts": {
51
51
  "build": "tsdown",
52
- "test": "vitest"
52
+ "print": "tsx ./scripts/print.ts"
53
53
  }
54
54
  }