@lark-apaas/nestjs-logger 1.0.10 → 1.0.12
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/README.md +5 -3
- package/dist/index.cjs +48 -36
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +49 -37
- package/dist/index.js.map +1 -1
- package/package.json +5 -4
package/README.md
CHANGED
|
@@ -365,9 +365,11 @@ NestJS LoggerService 级别到 Pino 级别的映射:
|
|
|
365
365
|
|
|
366
366
|
### 存储
|
|
367
367
|
|
|
368
|
-
1.
|
|
369
|
-
2.
|
|
370
|
-
3.
|
|
368
|
+
1. 开发环境默认启用日志轮转(基于 [rotating-file-stream](https://github.com/iccicci/rotating-file-stream)),活跃文件始终为 `server.log`,归档为 `server.log.1`(最新)→ `server.log.N`(最旧)
|
|
369
|
+
2. 默认单文件 10MB,最多保留 5 个归档,总占用上限约 60MB
|
|
370
|
+
3. 可通过环境变量控制轮转行为:`LOG_ROTATION_ENABLED`、`LOG_ROTATION_MAX_SIZE`、`LOG_ROTATION_MAX_FILES`
|
|
371
|
+
4. verbose 级别 + 请求/响应体日志会显著增加日志量
|
|
372
|
+
5. 建议设置 `LOG_MAX_BODY_LENGTH` 限制单条日志大小(默认不限制)
|
|
371
373
|
|
|
372
374
|
## 最佳实践
|
|
373
375
|
|
package/dist/index.cjs
CHANGED
|
@@ -9022,7 +9022,7 @@ var import_nestjs_common = require("@lark-apaas/nestjs-common");
|
|
|
9022
9022
|
function safeStringify(obj) {
|
|
9023
9023
|
const seen = /* @__PURE__ */ new Set();
|
|
9024
9024
|
try {
|
|
9025
|
-
return JSON.stringify(obj, (
|
|
9025
|
+
return JSON.stringify(obj, (_key, value) => {
|
|
9026
9026
|
if (typeof value === "object" && value !== null) {
|
|
9027
9027
|
if (seen.has(value)) {
|
|
9028
9028
|
return "[Circular]";
|
|
@@ -9482,7 +9482,12 @@ var logger_config_default = (0, import_config.registerAs)("logger", () => {
|
|
|
9482
9482
|
logDir: process.env.LOG_DIR || "logs",
|
|
9483
9483
|
logRequestBody: process.env.LOG_REQUEST_BODY === "true",
|
|
9484
9484
|
logResponseBody: process.env.LOG_RESPONSE_BODY === "true",
|
|
9485
|
-
maxBodyLength
|
|
9485
|
+
maxBodyLength,
|
|
9486
|
+
rotation: {
|
|
9487
|
+
enabled: process.env.LOG_ROTATION_ENABLED !== "false",
|
|
9488
|
+
maxSize: process.env.LOG_ROTATION_MAX_SIZE || "10m",
|
|
9489
|
+
maxFiles: Number(process.env.LOG_ROTATION_MAX_FILES) || 5
|
|
9490
|
+
}
|
|
9486
9491
|
};
|
|
9487
9492
|
});
|
|
9488
9493
|
|
|
@@ -9716,38 +9721,28 @@ LoggingInterceptor = _ts_decorate2([
|
|
|
9716
9721
|
var import_fs = require("fs");
|
|
9717
9722
|
var import_path = require("path");
|
|
9718
9723
|
var import_pino = __toESM(require("pino"), 1);
|
|
9719
|
-
|
|
9720
|
-
|
|
9721
|
-
|
|
9722
|
-
|
|
9723
|
-
|
|
9724
|
-
|
|
9725
|
-
|
|
9726
|
-
|
|
9727
|
-
|
|
9728
|
-
|
|
9729
|
-
level: label.toUpperCase()
|
|
9730
|
-
};
|
|
9731
|
-
}
|
|
9732
|
-
}
|
|
9733
|
-
};
|
|
9734
|
-
const streams = [
|
|
9735
|
-
{
|
|
9736
|
-
level: config.level,
|
|
9737
|
-
stream: createFileDestination(config.filePath)
|
|
9724
|
+
var import_rotating_file_stream = require("rotating-file-stream");
|
|
9725
|
+
var pinoBaseOptions = {
|
|
9726
|
+
base: void 0,
|
|
9727
|
+
messageKey: "message",
|
|
9728
|
+
timestamp: import_pino.stdTimeFunctions.isoTime,
|
|
9729
|
+
formatters: {
|
|
9730
|
+
level(label) {
|
|
9731
|
+
return {
|
|
9732
|
+
level: label.toUpperCase()
|
|
9733
|
+
};
|
|
9738
9734
|
}
|
|
9739
|
-
];
|
|
9740
|
-
if (streams.length === 0) {
|
|
9741
|
-
return (0, import_pino.default)(options);
|
|
9742
9735
|
}
|
|
9743
|
-
|
|
9744
|
-
|
|
9736
|
+
};
|
|
9737
|
+
function createPinoLogger(config) {
|
|
9738
|
+
const baseLevel = normalizeLevel(config.level);
|
|
9739
|
+
if (process.env.NODE_ENV !== "development") {
|
|
9740
|
+
return (0, import_pino.default)({
|
|
9741
|
+
...pinoBaseOptions,
|
|
9742
|
+
level: "silent"
|
|
9743
|
+
});
|
|
9745
9744
|
}
|
|
9746
|
-
|
|
9747
|
-
}
|
|
9748
|
-
__name(createPinoLogger, "createPinoLogger");
|
|
9749
|
-
function createFileDestination(pathname) {
|
|
9750
|
-
const target = pathname && pathname.length > 0 ? pathname : "logs/server.log";
|
|
9745
|
+
const target = config.filePath || "logs/server.log";
|
|
9751
9746
|
const resolved = (0, import_path.isAbsolute)(target) ? target : (0, import_path.join)(process.cwd(), target);
|
|
9752
9747
|
const dir = (0, import_path.dirname)(resolved);
|
|
9753
9748
|
if (!(0, import_fs.existsSync)(dir)) {
|
|
@@ -9755,12 +9750,27 @@ function createFileDestination(pathname) {
|
|
|
9755
9750
|
recursive: true
|
|
9756
9751
|
});
|
|
9757
9752
|
}
|
|
9758
|
-
|
|
9753
|
+
if (config.rotation?.enabled) {
|
|
9754
|
+
const size = config.rotation.maxSize.replace(/[kmgb]$/i, (c) => c.toUpperCase());
|
|
9755
|
+
const stream = (0, import_rotating_file_stream.createStream)((0, import_path.basename)(resolved), {
|
|
9756
|
+
size,
|
|
9757
|
+
rotate: config.rotation.maxFiles,
|
|
9758
|
+
path: dir
|
|
9759
|
+
});
|
|
9760
|
+
return (0, import_pino.default)({
|
|
9761
|
+
...pinoBaseOptions,
|
|
9762
|
+
level: baseLevel
|
|
9763
|
+
}, stream);
|
|
9764
|
+
}
|
|
9765
|
+
return (0, import_pino.default)({
|
|
9766
|
+
...pinoBaseOptions,
|
|
9767
|
+
level: baseLevel
|
|
9768
|
+
}, import_pino.default.destination({
|
|
9759
9769
|
dest: resolved,
|
|
9760
9770
|
mkdir: true
|
|
9761
|
-
});
|
|
9771
|
+
}));
|
|
9762
9772
|
}
|
|
9763
|
-
__name(
|
|
9773
|
+
__name(createPinoLogger, "createPinoLogger");
|
|
9764
9774
|
|
|
9765
9775
|
// src/middleware/logger-context.middleware.ts
|
|
9766
9776
|
var import_common3 = require("@nestjs/common");
|
|
@@ -9846,7 +9856,8 @@ LoggerModule = _ts_decorate4([
|
|
|
9846
9856
|
useFactory: /* @__PURE__ */ __name((config) => {
|
|
9847
9857
|
return createPinoLogger({
|
|
9848
9858
|
level: config.level,
|
|
9849
|
-
filePath: `${config.logDir}/server.log
|
|
9859
|
+
filePath: `${config.logDir}/server.log`,
|
|
9860
|
+
rotation: config.rotation
|
|
9850
9861
|
});
|
|
9851
9862
|
}, "useFactory"),
|
|
9852
9863
|
inject: [
|
|
@@ -9857,7 +9868,8 @@ LoggerModule = _ts_decorate4([
|
|
|
9857
9868
|
provide: TRACE_LOGGER,
|
|
9858
9869
|
useFactory: /* @__PURE__ */ __name((requestContext, config, observableService) => new PinoLoggerService(createPinoLogger({
|
|
9859
9870
|
level: config.level,
|
|
9860
|
-
filePath: `${config.logDir}/trace.log
|
|
9871
|
+
filePath: `${config.logDir}/trace.log`,
|
|
9872
|
+
rotation: config.rotation
|
|
9861
9873
|
}), requestContext, observableService), "useFactory"),
|
|
9862
9874
|
inject: [
|
|
9863
9875
|
import_nestjs_common4.RequestContextService,
|