@lark-apaas/nestjs-logger 0.1.0-alpha.3 → 0.1.0-alpha.4
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 +70 -15
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +11 -1
- package/dist/index.d.ts +11 -1
- package/dist/index.js +70 -16
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -14375,6 +14375,7 @@ __export(index_exports, {
|
|
|
14375
14375
|
AppLogger: () => AppLogger,
|
|
14376
14376
|
LoggerContextMiddleware: () => LoggerContextMiddleware,
|
|
14377
14377
|
LoggerModule: () => LoggerModule,
|
|
14378
|
+
PinoLoggerService: () => PinoLoggerService,
|
|
14378
14379
|
TRACE_LOGGER: () => TRACE_LOGGER
|
|
14379
14380
|
});
|
|
14380
14381
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -14521,6 +14522,36 @@ var BasePinoLogger = class _BasePinoLogger {
|
|
|
14521
14522
|
fatal(message, ...optionalParams) {
|
|
14522
14523
|
this.write("fatal", message, optionalParams, true);
|
|
14523
14524
|
}
|
|
14525
|
+
/**
|
|
14526
|
+
* 记录结构化日志,将 meta 对象合并到日志字段中
|
|
14527
|
+
* @param level 日志级别
|
|
14528
|
+
* @param message 消息文本
|
|
14529
|
+
* @param meta 要合并的元数据对象
|
|
14530
|
+
* @param context 上下文标识
|
|
14531
|
+
*/
|
|
14532
|
+
logStructured(level, message, meta, context) {
|
|
14533
|
+
if (!this.levelState.isEnabled(level)) {
|
|
14534
|
+
return;
|
|
14535
|
+
}
|
|
14536
|
+
const requestState = this.contextStore.getContext();
|
|
14537
|
+
const traceId = requestState?.requestId ?? null;
|
|
14538
|
+
const payload = {
|
|
14539
|
+
trace_id: traceId,
|
|
14540
|
+
path: requestState?.path,
|
|
14541
|
+
method: requestState?.method,
|
|
14542
|
+
user_id: requestState?.userId ?? null,
|
|
14543
|
+
app_id: requestState?.appId ?? null,
|
|
14544
|
+
tenant_id: requestState?.tenantId ?? null,
|
|
14545
|
+
pid: process.pid,
|
|
14546
|
+
...sanitizeValue(meta)
|
|
14547
|
+
};
|
|
14548
|
+
if (context) {
|
|
14549
|
+
payload.context = context;
|
|
14550
|
+
}
|
|
14551
|
+
const pinoLevel = mapLogLevelToPino(level);
|
|
14552
|
+
const sanitizedPayload = sanitizeValue(payload);
|
|
14553
|
+
this.logger[pinoLevel](sanitizedPayload, message);
|
|
14554
|
+
}
|
|
14524
14555
|
write(level, message, optionalParams, treatStack = false) {
|
|
14525
14556
|
if (!this.levelState.isEnabled(level)) {
|
|
14526
14557
|
return;
|
|
@@ -14742,10 +14773,12 @@ var LoggingInterceptor = class {
|
|
|
14742
14773
|
}
|
|
14743
14774
|
traceLogger;
|
|
14744
14775
|
requestContext;
|
|
14776
|
+
appLogger;
|
|
14745
14777
|
config;
|
|
14746
|
-
constructor(traceLogger, requestContext, config) {
|
|
14778
|
+
constructor(traceLogger, requestContext, appLogger, config) {
|
|
14747
14779
|
this.traceLogger = traceLogger;
|
|
14748
14780
|
this.requestContext = requestContext;
|
|
14781
|
+
this.appLogger = appLogger;
|
|
14749
14782
|
this.config = config;
|
|
14750
14783
|
}
|
|
14751
14784
|
intercept(context, next) {
|
|
@@ -14772,45 +14805,64 @@ var LoggingInterceptor = class {
|
|
|
14772
14805
|
ip: req.ip ?? null,
|
|
14773
14806
|
pid: process.pid
|
|
14774
14807
|
};
|
|
14808
|
+
this.appLogger.logStructured("log", "HTTP request started", baseMeta, "HTTPTraceInterceptor");
|
|
14775
14809
|
const requestMeta = {
|
|
14776
14810
|
...baseMeta
|
|
14777
14811
|
};
|
|
14778
14812
|
if (this.config.logRequestBody && req.body) {
|
|
14779
|
-
requestMeta["
|
|
14813
|
+
requestMeta["request_body"] = this.sanitizeAndTruncate(req.body);
|
|
14780
14814
|
}
|
|
14781
14815
|
if (this.config.logRequestBody && Object.keys(req.query || {}).length > 0) {
|
|
14782
|
-
requestMeta["
|
|
14816
|
+
requestMeta["query_params"] = this.sanitizeAndTruncate(req.query);
|
|
14783
14817
|
}
|
|
14784
|
-
this.traceLogger.
|
|
14818
|
+
this.traceLogger.logStructured("verbose", "HTTP request started", requestMeta, "HTTPTraceInterceptor");
|
|
14785
14819
|
return next.handle().pipe((0, import_operators.tap)((responseData) => {
|
|
14786
14820
|
const durationMs = Date.now() - startedAt;
|
|
14787
14821
|
const statusCode = res.statusCode;
|
|
14822
|
+
this.appLogger.logStructured("log", "HTTP request completed", {
|
|
14823
|
+
...baseMeta,
|
|
14824
|
+
status_code: statusCode,
|
|
14825
|
+
duration_ms: durationMs
|
|
14826
|
+
}, "HTTPTraceInterceptor");
|
|
14788
14827
|
const responseMeta = {
|
|
14789
14828
|
...baseMeta,
|
|
14790
|
-
statusCode,
|
|
14791
|
-
durationMs
|
|
14829
|
+
status_code: statusCode,
|
|
14830
|
+
duration_ms: durationMs
|
|
14792
14831
|
};
|
|
14793
14832
|
if (this.config.logResponseBody && responseData !== void 0) {
|
|
14794
14833
|
const contentType = res.getHeader("content-type");
|
|
14795
14834
|
const isJsonResponse = this.isJsonContentType(contentType);
|
|
14796
14835
|
if (isJsonResponse) {
|
|
14797
|
-
responseMeta["
|
|
14836
|
+
responseMeta["response_body"] = this.sanitizeAndTruncate(responseData);
|
|
14798
14837
|
}
|
|
14799
14838
|
}
|
|
14800
|
-
this.traceLogger.
|
|
14839
|
+
this.traceLogger.logStructured("verbose", "HTTP request completed", responseMeta, "HTTPTraceInterceptor");
|
|
14801
14840
|
}), (0, import_operators.catchError)((error) => {
|
|
14802
14841
|
const durationMs = Date.now() - startedAt;
|
|
14803
14842
|
const statusCode = res.statusCode >= 400 ? res.statusCode : 500;
|
|
14843
|
+
const linkMeta = {
|
|
14844
|
+
...baseMeta,
|
|
14845
|
+
status_code: statusCode,
|
|
14846
|
+
duration_ms: durationMs
|
|
14847
|
+
};
|
|
14848
|
+
if (error instanceof Error) {
|
|
14849
|
+
linkMeta["error_message"] = error.message;
|
|
14850
|
+
}
|
|
14851
|
+
this.appLogger.logStructured("log", "HTTP request failed", linkMeta, "HTTPTraceInterceptor");
|
|
14804
14852
|
const meta = {
|
|
14805
14853
|
...baseMeta,
|
|
14806
|
-
statusCode,
|
|
14807
|
-
durationMs
|
|
14854
|
+
status_code: statusCode,
|
|
14855
|
+
duration_ms: durationMs
|
|
14808
14856
|
};
|
|
14809
14857
|
if (error instanceof Error) {
|
|
14810
|
-
|
|
14858
|
+
meta["error"] = {
|
|
14859
|
+
message: error.message,
|
|
14860
|
+
stack: error.stack
|
|
14861
|
+
};
|
|
14811
14862
|
} else {
|
|
14812
|
-
|
|
14863
|
+
meta["error"] = String(error);
|
|
14813
14864
|
}
|
|
14865
|
+
this.traceLogger.logStructured("verbose", "HTTP request failed", meta, "HTTPTraceInterceptor");
|
|
14814
14866
|
throw error;
|
|
14815
14867
|
}));
|
|
14816
14868
|
}
|
|
@@ -14847,7 +14899,7 @@ var LoggingInterceptor = class {
|
|
|
14847
14899
|
return false;
|
|
14848
14900
|
}
|
|
14849
14901
|
const contentTypeLower = contentType.toLowerCase();
|
|
14850
|
-
return contentTypeLower.includes("application/json")
|
|
14902
|
+
return contentTypeLower.includes("application/json");
|
|
14851
14903
|
}
|
|
14852
14904
|
/**
|
|
14853
14905
|
* 脱敏敏感字段
|
|
@@ -14880,11 +14932,13 @@ var LoggingInterceptor = class {
|
|
|
14880
14932
|
LoggingInterceptor = _ts_decorate3([
|
|
14881
14933
|
(0, import_common3.Injectable)(),
|
|
14882
14934
|
_ts_param2(0, (0, import_common3.Inject)(TRACE_LOGGER)),
|
|
14883
|
-
_ts_param2(2, (0, import_common3.Inject)(
|
|
14935
|
+
_ts_param2(2, (0, import_common3.Inject)(AppLogger)),
|
|
14936
|
+
_ts_param2(3, (0, import_common3.Inject)(logger_config_default.KEY)),
|
|
14884
14937
|
_ts_metadata2("design:type", Function),
|
|
14885
14938
|
_ts_metadata2("design:paramtypes", [
|
|
14886
|
-
typeof
|
|
14939
|
+
typeof PinoLoggerService === "undefined" ? Object : PinoLoggerService,
|
|
14887
14940
|
typeof RequestContextService === "undefined" ? Object : RequestContextService,
|
|
14941
|
+
typeof AppLogger === "undefined" ? Object : AppLogger,
|
|
14888
14942
|
typeof import_config2.ConfigType === "undefined" ? Object : import_config2.ConfigType
|
|
14889
14943
|
])
|
|
14890
14944
|
], LoggingInterceptor);
|
|
@@ -15051,6 +15105,7 @@ LoggerModule = _ts_decorate5([
|
|
|
15051
15105
|
AppLogger,
|
|
15052
15106
|
LoggerContextMiddleware,
|
|
15053
15107
|
LoggerModule,
|
|
15108
|
+
PinoLoggerService,
|
|
15054
15109
|
TRACE_LOGGER
|
|
15055
15110
|
});
|
|
15056
15111
|
//# sourceMappingURL=index.cjs.map
|