@tomjs/logger 1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024-PRESENT Tom Gao<tom@tomgao.cc>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # @tomjs/logger
2
+
3
+ [![npm](https://img.shields.io/npm/v/@tomjs/logger)](https://www.npmjs.com/package/@tomjs/logger) ![node-current (scoped)](https://img.shields.io/logger/v/@tomjs/logger) ![NPM](https://img.shields.io/npm/l/@tomjs/logger) [![Docs](https://www.paka.dev/badges/v0/cute.svg)](https://www.paka.dev/npm/@tomjs/logger)
4
+
5
+ **English** | [中文](./README.zh_CN.md)
6
+
7
+ > logger for [tomjs](https://github.com/tomjs)
8
+
9
+ ## Install
10
+
11
+ ```bash
12
+ # pnpm
13
+ pnpm add @tomjs/logger
14
+
15
+ # yarn
16
+ yarn add @tomjs/logger
17
+
18
+ # npm
19
+ npm i @tomjs/logger
20
+ ```
21
+
22
+ ## Documentation
23
+
24
+ - [API Documentation](https://paka.dev/npm/@tomjs/logger) provided by [paka.dev](https://paka.dev).
25
+ - [index.d.ts](https://www.unpkg.com/browse/@tomjs/logger/dist/index.d.ts) provided by [unpkg.com](https://www.unpkg.com).
@@ -0,0 +1,25 @@
1
+ # @tomjs/logger
2
+
3
+ [![npm](https://img.shields.io/npm/v/@tomjs/logger)](https://www.npmjs.com/package/@tomjs/logger) ![node-current (scoped)](https://img.shields.io/logger/v/@tomjs/logger) ![NPM](https://img.shields.io/npm/l/@tomjs/logger) [![Docs](https://www.paka.dev/badges/v0/cute.svg)](https://www.paka.dev/npm/@tomjs/logger)
4
+
5
+ [English](./README.md) | **中文**
6
+
7
+ > [tomjs](https://github.com/tomjs) 的日志库
8
+
9
+ ## 安装
10
+
11
+ ```bash
12
+ # pnpm
13
+ pnpm add @tomjs/logger
14
+
15
+ # yarn
16
+ yarn add @tomjs/logger
17
+
18
+ # npm
19
+ npm i @tomjs/logger
20
+ ```
21
+
22
+ ## 文档
23
+
24
+ - [paka.dev](https://paka.dev) 提供的 [API文档](https://paka.dev/npm/@tomjs/logger).
25
+ - [unpkg.com](https://www.unpkg.com/) 提供的 [index.d.ts](https://www.unpkg.com/browse/@tomjs/logger/dist/index.d.ts).
@@ -0,0 +1,75 @@
1
+ /**
2
+ * log tool
3
+ */
4
+ declare class Logger {
5
+ private _opts;
6
+ private _logDir?;
7
+ private _debug;
8
+ constructor(opts?: LoggerOptions);
9
+ private initLogDir;
10
+ private format;
11
+ private _writeLog;
12
+ private _log;
13
+ /**
14
+ * set debug mode or not
15
+ */
16
+ enableDebug(debug: boolean): void;
17
+ /**
18
+ * like console.log
19
+ */
20
+ log(...args: any[]): void;
21
+ /**
22
+ * write log to file
23
+ */
24
+ write(...args: any[]): void;
25
+ /**
26
+ * only show in debug mode
27
+ */
28
+ debug(...args: any[]): void;
29
+ /**
30
+ * add the specified red prefix or error symbol before the log content
31
+ */
32
+ error(...args: any[]): void;
33
+ /**
34
+ * add the specified blue prefix or info symbol before the log content
35
+ */
36
+ info(...args: any[]): void;
37
+ /**
38
+ * add the specified green prefix or success symbol before the log content
39
+ */
40
+ success(...args: any[]): void;
41
+ /**
42
+ * add the specified yellow prefix or warning symbol before the log content
43
+ */
44
+ warning(...args: any[]): void;
45
+ }
46
+ export { Logger }
47
+ export default Logger;
48
+
49
+ export declare interface LoggerOptions {
50
+ /**
51
+ * log prefix
52
+ */
53
+ prefix?: string;
54
+ /**
55
+ * enable debug mode
56
+ * @default false
57
+ */
58
+ debug?: boolean;
59
+ /**
60
+ * show time in log
61
+ * @default false
62
+ */
63
+ time?: boolean;
64
+ /**
65
+ * specify the log directory name
66
+ */
67
+ directory?: string;
68
+ /**
69
+ * specify the log cleanup period
70
+ * @default 30
71
+ */
72
+ cleanup?: number;
73
+ }
74
+
75
+ export { }
@@ -0,0 +1,75 @@
1
+ /**
2
+ * log tool
3
+ */
4
+ declare class Logger {
5
+ private _opts;
6
+ private _logDir?;
7
+ private _debug;
8
+ constructor(opts?: LoggerOptions);
9
+ private initLogDir;
10
+ private format;
11
+ private _writeLog;
12
+ private _log;
13
+ /**
14
+ * set debug mode or not
15
+ */
16
+ enableDebug(debug: boolean): void;
17
+ /**
18
+ * like console.log
19
+ */
20
+ log(...args: any[]): void;
21
+ /**
22
+ * write log to file
23
+ */
24
+ write(...args: any[]): void;
25
+ /**
26
+ * only show in debug mode
27
+ */
28
+ debug(...args: any[]): void;
29
+ /**
30
+ * add the specified red prefix or error symbol before the log content
31
+ */
32
+ error(...args: any[]): void;
33
+ /**
34
+ * add the specified blue prefix or info symbol before the log content
35
+ */
36
+ info(...args: any[]): void;
37
+ /**
38
+ * add the specified green prefix or success symbol before the log content
39
+ */
40
+ success(...args: any[]): void;
41
+ /**
42
+ * add the specified yellow prefix or warning symbol before the log content
43
+ */
44
+ warning(...args: any[]): void;
45
+ }
46
+ export { Logger }
47
+ export default Logger;
48
+
49
+ export declare interface LoggerOptions {
50
+ /**
51
+ * log prefix
52
+ */
53
+ prefix?: string;
54
+ /**
55
+ * enable debug mode
56
+ * @default false
57
+ */
58
+ debug?: boolean;
59
+ /**
60
+ * show time in log
61
+ * @default false
62
+ */
63
+ time?: boolean;
64
+ /**
65
+ * specify the log directory name
66
+ */
67
+ directory?: string;
68
+ /**
69
+ * specify the log cleanup period
70
+ * @default 30
71
+ */
72
+ cleanup?: number;
73
+ }
74
+
75
+ export { }
package/dist/index.js ADDED
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
3
+ const fs = require("node:fs");
4
+ const os = require("node:os");
5
+ const path = require("node:path");
6
+ const chalk = require("chalk");
7
+ const dayjs = require("dayjs");
8
+ const logSymbols = require("log-symbols");
9
+ const stripAnsi = require("strip-ansi");
10
+ class Logger {
11
+ constructor(opts) {
12
+ this._opts = {};
13
+ this._debug = false;
14
+ this._opts = Object.assign({}, opts);
15
+ this.initLogDir();
16
+ }
17
+ initLogDir() {
18
+ const { directory, cleanup } = this._opts;
19
+ if (!directory) {
20
+ return;
21
+ }
22
+ const logDir = path.join(os.homedir(), ".tomjs", directory);
23
+ this._logDir = logDir;
24
+ if (!fs.existsSync(logDir)) {
25
+ fs.mkdirSync(logDir, { recursive: true });
26
+ }
27
+ fs.readdirSync(logDir).forEach((s) => {
28
+ if (dayjs(s.substring(0, 8)).isBefore(
29
+ dayjs().endOf("day").subtract(Math.max(1, cleanup ?? 30), "day")
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
+ /**
58
+ * set debug mode or not
59
+ */
60
+ enableDebug(debug) {
61
+ this._debug = debug ?? true;
62
+ }
63
+ /**
64
+ * like console.log
65
+ */
66
+ log(...args) {
67
+ this._log(...args);
68
+ }
69
+ /**
70
+ * write log to file
71
+ */
72
+ write(...args) {
73
+ this._writeLog(...args);
74
+ }
75
+ /**
76
+ * only show in debug mode
77
+ */
78
+ debug(...args) {
79
+ if (this._debug) {
80
+ this._log(
81
+ ...args.map((s) => {
82
+ if (typeof s !== "object") {
83
+ return chalk.gray(s);
84
+ }
85
+ return s;
86
+ })
87
+ );
88
+ }
89
+ }
90
+ /**
91
+ * add the specified red prefix or error symbol before the log content
92
+ */
93
+ error(...args) {
94
+ const { prefix } = this._opts;
95
+ this._log(prefix ? chalk.red(prefix) : logSymbols.error, ...args);
96
+ }
97
+ /**
98
+ * add the specified blue prefix or info symbol before the log content
99
+ */
100
+ info(...args) {
101
+ const { prefix } = this._opts;
102
+ this._log(prefix ? chalk.blue(prefix) : logSymbols.info, ...args);
103
+ }
104
+ /**
105
+ * add the specified green prefix or success symbol before the log content
106
+ */
107
+ success(...args) {
108
+ const { prefix } = this._opts;
109
+ this._log(prefix ? chalk.green(prefix) : logSymbols.success, ...args);
110
+ }
111
+ /**
112
+ * add the specified yellow prefix or warning symbol before the log content
113
+ */
114
+ warning(...args) {
115
+ const { prefix } = this._opts;
116
+ this._log(prefix ? chalk.yellow(prefix) : logSymbols.warning, ...args);
117
+ }
118
+ }
119
+ exports.Logger = Logger;
120
+ exports.default = Logger;
package/dist/index.mjs ADDED
@@ -0,0 +1,120 @@
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";
8
+ class Logger {
9
+ constructor(opts) {
10
+ this._opts = {};
11
+ this._debug = false;
12
+ this._opts = Object.assign({}, opts);
13
+ this.initLogDir();
14
+ }
15
+ initLogDir() {
16
+ const { directory, cleanup } = this._opts;
17
+ if (!directory) {
18
+ return;
19
+ }
20
+ const logDir = path.join(os.homedir(), ".tomjs", directory);
21
+ this._logDir = logDir;
22
+ if (!fs.existsSync(logDir)) {
23
+ fs.mkdirSync(logDir, { recursive: true });
24
+ }
25
+ fs.readdirSync(logDir).forEach((s) => {
26
+ if (dayjs(s.substring(0, 8)).isBefore(
27
+ dayjs().endOf("day").subtract(Math.max(1, cleanup ?? 30), "day")
28
+ )) {
29
+ fs.rmSync(path.join(logDir, s), { force: true });
30
+ }
31
+ });
32
+ }
33
+ format(...args) {
34
+ return args.map((s) => typeof s === "object" ? JSON.stringify(s) : s || "").join(" ");
35
+ }
36
+ _writeLog(...args) {
37
+ if (!this._logDir) {
38
+ return;
39
+ }
40
+ const logFile = path.join(this._logDir, `${dayjs().format("YYYYMMDD")}.log`);
41
+ fs.appendFileSync(
42
+ logFile,
43
+ `${dayjs().format("YYYY-MM-DD HH:mm:ss.SSS")} ${stripAnsi(this.format(...args))}
44
+ `
45
+ );
46
+ }
47
+ _log(...args) {
48
+ this._writeLog(...args);
49
+ let list = [...args];
50
+ if (this._opts.time) {
51
+ list = [dayjs().format("HH:mm:ss"), ...list];
52
+ }
53
+ console.log(list.map((s) => typeof s === "object" ? "%o" : "%s").join(" "), ...list);
54
+ }
55
+ /**
56
+ * set debug mode or not
57
+ */
58
+ enableDebug(debug) {
59
+ this._debug = debug ?? true;
60
+ }
61
+ /**
62
+ * like console.log
63
+ */
64
+ log(...args) {
65
+ this._log(...args);
66
+ }
67
+ /**
68
+ * write log to file
69
+ */
70
+ write(...args) {
71
+ this._writeLog(...args);
72
+ }
73
+ /**
74
+ * only show in debug mode
75
+ */
76
+ debug(...args) {
77
+ if (this._debug) {
78
+ this._log(
79
+ ...args.map((s) => {
80
+ if (typeof s !== "object") {
81
+ return chalk.gray(s);
82
+ }
83
+ return s;
84
+ })
85
+ );
86
+ }
87
+ }
88
+ /**
89
+ * add the specified red prefix or error symbol before the log content
90
+ */
91
+ error(...args) {
92
+ const { prefix } = this._opts;
93
+ this._log(prefix ? chalk.red(prefix) : logSymbols.error, ...args);
94
+ }
95
+ /**
96
+ * add the specified blue prefix or info symbol before the log content
97
+ */
98
+ info(...args) {
99
+ const { prefix } = this._opts;
100
+ this._log(prefix ? chalk.blue(prefix) : logSymbols.info, ...args);
101
+ }
102
+ /**
103
+ * add the specified green prefix or success symbol before the log content
104
+ */
105
+ success(...args) {
106
+ const { prefix } = this._opts;
107
+ this._log(prefix ? chalk.green(prefix) : logSymbols.success, ...args);
108
+ }
109
+ /**
110
+ * add the specified yellow prefix or warning symbol before the log content
111
+ */
112
+ warning(...args) {
113
+ const { prefix } = this._opts;
114
+ this._log(prefix ? chalk.yellow(prefix) : logSymbols.warning, ...args);
115
+ }
116
+ }
117
+ export {
118
+ Logger,
119
+ Logger as default
120
+ };
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@tomjs/logger",
3
+ "version": "1.0.0",
4
+ "description": "logger for `node.js`",
5
+ "keywords": [
6
+ "node",
7
+ "utils"
8
+ ],
9
+ "author": {
10
+ "name": "Tom Gao",
11
+ "email": "tom@tomgao.cc"
12
+ },
13
+ "license": "MIT",
14
+ "main": "./dist/index.js",
15
+ "module": "./dist/index.mjs",
16
+ "types": "./dist/index.d.ts",
17
+ "exports": {
18
+ ".": {
19
+ "require": {
20
+ "default": "./dist/index.js",
21
+ "types": "./dist/index.d.ts"
22
+ },
23
+ "import": {
24
+ "default": "./dist/index.mjs",
25
+ "types": "./dist/index.d.mts"
26
+ }
27
+ }
28
+ },
29
+ "files": [
30
+ "dist"
31
+ ],
32
+ "engines": {
33
+ "node": ">=16"
34
+ },
35
+ "publishConfig": {
36
+ "access": "public",
37
+ "registry": "https://registry.npmjs.org/"
38
+ },
39
+ "repository": {
40
+ "type": "git",
41
+ "url": "git+https://github.com/tomjs/utils.git",
42
+ "directory": "packages/logger"
43
+ },
44
+ "dependencies": {
45
+ "chalk": "^4.1.2",
46
+ "dayjs": "^1.11.11",
47
+ "log-symbols": "^4.1.0",
48
+ "strip-ansi": "^6.0.1"
49
+ },
50
+ "scripts": {
51
+ "build": "vite build",
52
+ "test": "vitest"
53
+ }
54
+ }