@tomjs/logger 1.3.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 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
@@ -22,6 +23,10 @@ interface LoggerOptions {
22
23
  * @default 30
23
24
  */
24
25
  cleanup?: number;
26
+ /**
27
+ * log file root directory,default is '~/.tomjs'
28
+ */
29
+ root?: string;
25
30
  }
26
31
  /**
27
32
  * log tool
@@ -75,5 +80,5 @@ declare class Logger {
75
80
  */
76
81
  warn(...args: any[]): void;
77
82
  }
78
-
79
- export { Logger, type LoggerOptions, Logger as default };
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
@@ -22,6 +23,10 @@ interface LoggerOptions {
22
23
  * @default 30
23
24
  */
24
25
  cleanup?: number;
26
+ /**
27
+ * log file root directory,default is '~/.tomjs'
28
+ */
29
+ root?: string;
25
30
  }
26
31
  /**
27
32
  * log tool
@@ -75,5 +80,5 @@ declare class Logger {
75
80
  */
76
81
  warn(...args: any[]): void;
77
82
  }
78
-
79
- export { Logger, type LoggerOptions, Logger as default };
83
+ //#endregion
84
+ export { Logger, Logger as default, LoggerOptions };
package/dist/index.mjs CHANGED
@@ -1,103 +1,124 @@
1
- import fs from 'node:fs';
2
- import os from 'node:os';
3
- import path from 'node:path';
4
- import chalk from 'chalk';
5
- import dayjs from 'dayjs';
6
- import logSymbols from 'log-symbols';
7
- import stripAnsi from 'strip-ansi';
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
- constructor(options) {
10
- this._opts = {};
11
- this.setOptions(Object.assign({}, options));
12
- }
13
- initLogDir() {
14
- const { directory, cleanup } = this._opts;
15
- if (!directory) {
16
- return;
17
- }
18
- const logDir = path.join(os.homedir(), '.tomjs', directory);
19
- this._logDir = logDir;
20
- if (!fs.existsSync(logDir)) {
21
- fs.mkdirSync(logDir, { recursive: true });
22
- }
23
- fs.readdirSync(logDir).forEach(s => {
24
- if (
25
- dayjs(s.substring(0, 8)).isBefore(
26
- dayjs()
27
- .endOf('day')
28
- .subtract(Math.max(1, cleanup ?? 30), 'day'),
29
- )
30
- ) {
31
- fs.rmSync(path.join(logDir, s), { force: true });
32
- }
33
- });
34
- }
35
- format(...args) {
36
- return args.map(s => (typeof s === 'object' ? JSON.stringify(s) : s || '')).join(' ');
37
- }
38
- _writeLog(...args) {
39
- if (!this._logDir) {
40
- return;
41
- }
42
- const logFile = path.join(this._logDir, `${dayjs().format('YYYYMMDD')}.log`);
43
- fs.appendFileSync(
44
- logFile,
45
- `${dayjs().format('YYYY-MM-DD HH:mm:ss.SSS')} ${stripAnsi(this.format(...args))}
46
- `,
47
- );
48
- }
49
- _log(...args) {
50
- this._writeLog(...args);
51
- let list = [...args];
52
- if (this._opts.time) {
53
- list = [dayjs().format('HH:mm:ss'), ...list];
54
- }
55
- console.log(list.map(s => (typeof s === 'object' ? '%o' : '%s')).join(' '), ...list);
56
- }
57
- enableDebug(debug) {
58
- this._opts.debug = !!debug;
59
- }
60
- setOptions(options) {
61
- this._opts = Object.assign({}, options);
62
- this.initLogDir();
63
- }
64
- log(...args) {
65
- this._log(...args);
66
- }
67
- write(...args) {
68
- this._writeLog(...args);
69
- }
70
- debug(...args) {
71
- if (this._opts.debug) {
72
- this._log(
73
- ...args.map(s => {
74
- if (typeof s !== 'object') {
75
- return chalk.gray(s);
76
- }
77
- return s;
78
- }),
79
- );
80
- }
81
- }
82
- error(...args) {
83
- const { prefix } = this._opts;
84
- this._log(prefix ? chalk.red(prefix) : logSymbols.error, ...args);
85
- }
86
- info(...args) {
87
- const { prefix } = this._opts;
88
- this._log(prefix ? chalk.blue(prefix) : logSymbols.info, ...args);
89
- }
90
- success(...args) {
91
- const { prefix } = this._opts;
92
- this._log(prefix ? chalk.green(prefix) : logSymbols.success, ...args);
93
- }
94
- warning(...args) {
95
- const { prefix } = this._opts;
96
- this._log(prefix ? chalk.yellow(prefix) : logSymbols.warning, ...args);
97
- }
98
- warn(...args) {
99
- this.warning(...args);
100
- }
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
+ }
101
120
  };
102
121
  var src_default = Logger;
103
- export { Logger, src_default as default };
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": "1.3.0",
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
- "main": "./dist/index.js",
15
- "module": "./dist/index.mjs",
16
- "types": "./dist/index.d.ts",
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
- "default": "./dist/index.mjs",
25
- "types": "./dist/index.d.mts"
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": "tsup && prettier --write ./dist",
51
+ "build": "tsdown",
52
52
  "test": "vitest"
53
53
  }
54
54
  }
package/dist/index.js DELETED
@@ -1,133 +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 logDir = _path2.default.join(_os2.default.homedir(), '.tomjs', directory);
38
- this._logDir = logDir;
39
- if (!_fs2.default.existsSync(logDir)) {
40
- _fs2.default.mkdirSync(logDir, { recursive: true });
41
- }
42
- _fs2.default.readdirSync(logDir).forEach(s => {
43
- if (
44
- _dayjs2.default.call(void 0, s.substring(0, 8)).isBefore(
45
- _dayjs2.default
46
- .call(void 0)
47
- .endOf('day')
48
- .subtract(
49
- Math.max(
50
- 1,
51
- _nullishCoalesce(cleanup, () => 30),
52
- ),
53
- 'day',
54
- ),
55
- )
56
- ) {
57
- _fs2.default.rmSync(_path2.default.join(logDir, s), { force: true });
58
- }
59
- });
60
- }
61
- format(...args) {
62
- return args.map(s => (typeof s === 'object' ? JSON.stringify(s) : s || '')).join(' ');
63
- }
64
- _writeLog(...args) {
65
- if (!this._logDir) {
66
- return;
67
- }
68
- const logFile = _path2.default.join(
69
- this._logDir,
70
- `${_dayjs2.default.call(void 0).format('YYYYMMDD')}.log`,
71
- );
72
- _fs2.default.appendFileSync(
73
- logFile,
74
- `${_dayjs2.default.call(void 0).format('YYYY-MM-DD HH:mm:ss.SSS')} ${_stripansi2.default.call(void 0, this.format(...args))}
75
- `,
76
- );
77
- }
78
- _log(...args) {
79
- this._writeLog(...args);
80
- let list = [...args];
81
- if (this._opts.time) {
82
- list = [_dayjs2.default.call(void 0).format('HH:mm:ss'), ...list];
83
- }
84
- console.log(list.map(s => (typeof s === 'object' ? '%o' : '%s')).join(' '), ...list);
85
- }
86
- enableDebug(debug) {
87
- this._opts.debug = !!debug;
88
- }
89
- setOptions(options) {
90
- this._opts = Object.assign({}, options);
91
- this.initLogDir();
92
- }
93
- log(...args) {
94
- this._log(...args);
95
- }
96
- write(...args) {
97
- this._writeLog(...args);
98
- }
99
- debug(...args) {
100
- if (this._opts.debug) {
101
- this._log(
102
- ...args.map(s => {
103
- if (typeof s !== 'object') {
104
- return _chalk2.default.gray(s);
105
- }
106
- return s;
107
- }),
108
- );
109
- }
110
- }
111
- error(...args) {
112
- const { prefix } = this._opts;
113
- this._log(prefix ? _chalk2.default.red(prefix) : _logsymbols2.default.error, ...args);
114
- }
115
- info(...args) {
116
- const { prefix } = this._opts;
117
- this._log(prefix ? _chalk2.default.blue(prefix) : _logsymbols2.default.info, ...args);
118
- }
119
- success(...args) {
120
- const { prefix } = this._opts;
121
- this._log(prefix ? _chalk2.default.green(prefix) : _logsymbols2.default.success, ...args);
122
- }
123
- warning(...args) {
124
- const { prefix } = this._opts;
125
- this._log(prefix ? _chalk2.default.yellow(prefix) : _logsymbols2.default.warning, ...args);
126
- }
127
- warn(...args) {
128
- this.warning(...args);
129
- }
130
- };
131
- var src_default = Logger;
132
- exports.Logger = Logger;
133
- exports.default = src_default;