p-api-log 0.0.1
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 +128 -0
- package/dist/index.d.cts +21 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +93 -0
- package/package.json +32 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
WinstonLogger: () => WinstonLogger
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(index_exports);
|
|
36
|
+
var import_winston = __toESM(require("winston"), 1);
|
|
37
|
+
var import_winston_daily_rotate_file = require("winston-daily-rotate-file");
|
|
38
|
+
var WinstonLogger = class {
|
|
39
|
+
constructor(config) {
|
|
40
|
+
this.config = config;
|
|
41
|
+
const logFormat = import_winston.format.printf(({ level, message, timestamp, stack }) => {
|
|
42
|
+
return `${timestamp} [${level}] ${stack || message}`;
|
|
43
|
+
});
|
|
44
|
+
this.log = import_winston.default.createLogger({
|
|
45
|
+
level: "info",
|
|
46
|
+
format: import_winston.format.combine(
|
|
47
|
+
import_winston.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }),
|
|
48
|
+
import_winston.format.errors({ stack: true }),
|
|
49
|
+
// 自动记录 error.stack
|
|
50
|
+
logFormat
|
|
51
|
+
),
|
|
52
|
+
transports: [
|
|
53
|
+
// info 日志
|
|
54
|
+
new import_winston.transports.DailyRotateFile({
|
|
55
|
+
dirname: config.logger_path,
|
|
56
|
+
filename: "info-%DATE%.log",
|
|
57
|
+
datePattern: "YYYY-MM-DD",
|
|
58
|
+
maxSize: "20m",
|
|
59
|
+
maxFiles: "14d"
|
|
60
|
+
}),
|
|
61
|
+
// error 日志
|
|
62
|
+
new import_winston.transports.DailyRotateFile({
|
|
63
|
+
dirname: config.logger_path,
|
|
64
|
+
filename: "error-%DATE%.log",
|
|
65
|
+
level: "error",
|
|
66
|
+
datePattern: "YYYY-MM-DD",
|
|
67
|
+
maxSize: "20m",
|
|
68
|
+
maxFiles: "30d"
|
|
69
|
+
})
|
|
70
|
+
],
|
|
71
|
+
exitOnError: false
|
|
72
|
+
// 防止异常退出
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
i(pos, msg) {
|
|
76
|
+
if (this.config.debug) {
|
|
77
|
+
this.console_info(pos, msg);
|
|
78
|
+
} else {
|
|
79
|
+
this.file_info(pos, msg);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
e(pos, msg) {
|
|
83
|
+
if (this.config.debug) {
|
|
84
|
+
this.console_error(pos, msg);
|
|
85
|
+
} else {
|
|
86
|
+
this.file_error(pos, msg);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
console_info(pos, msg) {
|
|
90
|
+
console.log(`%c${this.formatDate()} %c[INFO]-%c<${pos}> %c${this.formatMsg(msg)}`, "color: green;", "color: green; font-weight: bold;", "color: default;", "color: default;");
|
|
91
|
+
}
|
|
92
|
+
console_error(pos, msg) {
|
|
93
|
+
console.log(`%c${this.formatDate()} %c[ERROR]-%c<${pos}> %c${this.formatMsg(msg)}`, "color: red;", "color: red; font-weight: bold;", "color: default;", "color: default;");
|
|
94
|
+
}
|
|
95
|
+
file_info(pos, msg) {
|
|
96
|
+
this.log.info(`<${pos}> ${this.formatMsg(msg)}`);
|
|
97
|
+
}
|
|
98
|
+
file_error(pos, msg) {
|
|
99
|
+
this.log.error(`<${pos}> ${this.formatMsg(msg)}`);
|
|
100
|
+
}
|
|
101
|
+
formatDate(date = /* @__PURE__ */ new Date()) {
|
|
102
|
+
const Y = date.getFullYear();
|
|
103
|
+
const M = (date.getMonth() + 1).toString().padStart(2, "0");
|
|
104
|
+
const D = date.getDate().toString().padStart(2, "0");
|
|
105
|
+
const h = date.getHours().toString().padStart(2, "0");
|
|
106
|
+
const m = date.getMinutes().toString().padStart(2, "0");
|
|
107
|
+
const s = date.getSeconds().toString().padStart(2, "0");
|
|
108
|
+
return `${Y}-${M}-${D} ${h}:${m}:${s}`;
|
|
109
|
+
}
|
|
110
|
+
formatMsg(msg) {
|
|
111
|
+
if (msg === null || msg === void 0) return String(msg);
|
|
112
|
+
if (msg instanceof Error) {
|
|
113
|
+
return (msg.stack || msg.message || String(msg)).replace(/\n/g, " ");
|
|
114
|
+
}
|
|
115
|
+
if (typeof msg === "object") {
|
|
116
|
+
try {
|
|
117
|
+
return JSON.stringify(msg);
|
|
118
|
+
} catch {
|
|
119
|
+
return String(msg);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return String(msg);
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
126
|
+
0 && (module.exports = {
|
|
127
|
+
WinstonLogger
|
|
128
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Logger, Logform } from 'winston';
|
|
2
|
+
|
|
3
|
+
declare class WinstonLogger {
|
|
4
|
+
private config;
|
|
5
|
+
log: Logger;
|
|
6
|
+
constructor(config: {
|
|
7
|
+
logger_path: string;
|
|
8
|
+
debug?: boolean;
|
|
9
|
+
log_format?: (log_info: Logform.TransformableInfo) => string;
|
|
10
|
+
});
|
|
11
|
+
i(pos: string, msg: any): void;
|
|
12
|
+
e(pos: string, msg: any): void;
|
|
13
|
+
private console_info;
|
|
14
|
+
private console_error;
|
|
15
|
+
private file_info;
|
|
16
|
+
private file_error;
|
|
17
|
+
private formatDate;
|
|
18
|
+
private formatMsg;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export { WinstonLogger };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Logger, Logform } from 'winston';
|
|
2
|
+
|
|
3
|
+
declare class WinstonLogger {
|
|
4
|
+
private config;
|
|
5
|
+
log: Logger;
|
|
6
|
+
constructor(config: {
|
|
7
|
+
logger_path: string;
|
|
8
|
+
debug?: boolean;
|
|
9
|
+
log_format?: (log_info: Logform.TransformableInfo) => string;
|
|
10
|
+
});
|
|
11
|
+
i(pos: string, msg: any): void;
|
|
12
|
+
e(pos: string, msg: any): void;
|
|
13
|
+
private console_info;
|
|
14
|
+
private console_error;
|
|
15
|
+
private file_info;
|
|
16
|
+
private file_error;
|
|
17
|
+
private formatDate;
|
|
18
|
+
private formatMsg;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export { WinstonLogger };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import winston, { format, transports } from "winston";
|
|
3
|
+
import "winston-daily-rotate-file";
|
|
4
|
+
var WinstonLogger = class {
|
|
5
|
+
constructor(config) {
|
|
6
|
+
this.config = config;
|
|
7
|
+
const logFormat = format.printf(({ level, message, timestamp, stack }) => {
|
|
8
|
+
return `${timestamp} [${level}] ${stack || message}`;
|
|
9
|
+
});
|
|
10
|
+
this.log = winston.createLogger({
|
|
11
|
+
level: "info",
|
|
12
|
+
format: format.combine(
|
|
13
|
+
format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }),
|
|
14
|
+
format.errors({ stack: true }),
|
|
15
|
+
// 自动记录 error.stack
|
|
16
|
+
logFormat
|
|
17
|
+
),
|
|
18
|
+
transports: [
|
|
19
|
+
// info 日志
|
|
20
|
+
new transports.DailyRotateFile({
|
|
21
|
+
dirname: config.logger_path,
|
|
22
|
+
filename: "info-%DATE%.log",
|
|
23
|
+
datePattern: "YYYY-MM-DD",
|
|
24
|
+
maxSize: "20m",
|
|
25
|
+
maxFiles: "14d"
|
|
26
|
+
}),
|
|
27
|
+
// error 日志
|
|
28
|
+
new transports.DailyRotateFile({
|
|
29
|
+
dirname: config.logger_path,
|
|
30
|
+
filename: "error-%DATE%.log",
|
|
31
|
+
level: "error",
|
|
32
|
+
datePattern: "YYYY-MM-DD",
|
|
33
|
+
maxSize: "20m",
|
|
34
|
+
maxFiles: "30d"
|
|
35
|
+
})
|
|
36
|
+
],
|
|
37
|
+
exitOnError: false
|
|
38
|
+
// 防止异常退出
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
i(pos, msg) {
|
|
42
|
+
if (this.config.debug) {
|
|
43
|
+
this.console_info(pos, msg);
|
|
44
|
+
} else {
|
|
45
|
+
this.file_info(pos, msg);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
e(pos, msg) {
|
|
49
|
+
if (this.config.debug) {
|
|
50
|
+
this.console_error(pos, msg);
|
|
51
|
+
} else {
|
|
52
|
+
this.file_error(pos, msg);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
console_info(pos, msg) {
|
|
56
|
+
console.log(`%c${this.formatDate()} %c[INFO]-%c<${pos}> %c${this.formatMsg(msg)}`, "color: green;", "color: green; font-weight: bold;", "color: default;", "color: default;");
|
|
57
|
+
}
|
|
58
|
+
console_error(pos, msg) {
|
|
59
|
+
console.log(`%c${this.formatDate()} %c[ERROR]-%c<${pos}> %c${this.formatMsg(msg)}`, "color: red;", "color: red; font-weight: bold;", "color: default;", "color: default;");
|
|
60
|
+
}
|
|
61
|
+
file_info(pos, msg) {
|
|
62
|
+
this.log.info(`<${pos}> ${this.formatMsg(msg)}`);
|
|
63
|
+
}
|
|
64
|
+
file_error(pos, msg) {
|
|
65
|
+
this.log.error(`<${pos}> ${this.formatMsg(msg)}`);
|
|
66
|
+
}
|
|
67
|
+
formatDate(date = /* @__PURE__ */ new Date()) {
|
|
68
|
+
const Y = date.getFullYear();
|
|
69
|
+
const M = (date.getMonth() + 1).toString().padStart(2, "0");
|
|
70
|
+
const D = date.getDate().toString().padStart(2, "0");
|
|
71
|
+
const h = date.getHours().toString().padStart(2, "0");
|
|
72
|
+
const m = date.getMinutes().toString().padStart(2, "0");
|
|
73
|
+
const s = date.getSeconds().toString().padStart(2, "0");
|
|
74
|
+
return `${Y}-${M}-${D} ${h}:${m}:${s}`;
|
|
75
|
+
}
|
|
76
|
+
formatMsg(msg) {
|
|
77
|
+
if (msg === null || msg === void 0) return String(msg);
|
|
78
|
+
if (msg instanceof Error) {
|
|
79
|
+
return (msg.stack || msg.message || String(msg)).replace(/\n/g, " ");
|
|
80
|
+
}
|
|
81
|
+
if (typeof msg === "object") {
|
|
82
|
+
try {
|
|
83
|
+
return JSON.stringify(msg);
|
|
84
|
+
} catch {
|
|
85
|
+
return String(msg);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return String(msg);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
export {
|
|
92
|
+
WinstonLogger
|
|
93
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"type": "module",
|
|
3
|
+
"name": "p-api-log",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": "./dist/index.mjs",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"types": "dist/index.d.ts",
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsup"
|
|
19
|
+
},
|
|
20
|
+
"author": "",
|
|
21
|
+
"license": "ISC",
|
|
22
|
+
"description": "",
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"javascript-obfuscator": "^4.1.1",
|
|
25
|
+
"tsup": "^8.4.0",
|
|
26
|
+
"typescript": "5.8.3"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"winston": "^3.19.0",
|
|
30
|
+
"winston-daily-rotate-file": "^5.0.0"
|
|
31
|
+
}
|
|
32
|
+
}
|