@lark-apaas/nestjs-logger 0.1.0-alpha.3 → 0.1.0-alpha.5
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 +71 -29
- 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 +71 -30
- 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,60 @@ 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
|
-
|
|
14795
|
-
const isJsonResponse = this.isJsonContentType(contentType);
|
|
14796
|
-
if (isJsonResponse) {
|
|
14797
|
-
responseMeta["responseBody"] = this.sanitizeAndTruncate(responseData);
|
|
14798
|
-
}
|
|
14833
|
+
responseMeta["response_body"] = this.sanitizeAndTruncate(responseData);
|
|
14799
14834
|
}
|
|
14800
|
-
this.traceLogger.
|
|
14835
|
+
this.traceLogger.logStructured("verbose", "HTTP request completed", responseMeta, "HTTPTraceInterceptor");
|
|
14801
14836
|
}), (0, import_operators.catchError)((error) => {
|
|
14802
14837
|
const durationMs = Date.now() - startedAt;
|
|
14803
14838
|
const statusCode = res.statusCode >= 400 ? res.statusCode : 500;
|
|
14839
|
+
const linkMeta = {
|
|
14840
|
+
...baseMeta,
|
|
14841
|
+
status_code: statusCode,
|
|
14842
|
+
duration_ms: durationMs
|
|
14843
|
+
};
|
|
14844
|
+
if (error instanceof Error) {
|
|
14845
|
+
linkMeta["error_message"] = error.message;
|
|
14846
|
+
}
|
|
14847
|
+
this.appLogger.logStructured("log", "HTTP request failed", linkMeta, "HTTPTraceInterceptor");
|
|
14804
14848
|
const meta = {
|
|
14805
14849
|
...baseMeta,
|
|
14806
|
-
statusCode,
|
|
14807
|
-
durationMs
|
|
14850
|
+
status_code: statusCode,
|
|
14851
|
+
duration_ms: durationMs
|
|
14808
14852
|
};
|
|
14809
14853
|
if (error instanceof Error) {
|
|
14810
|
-
|
|
14854
|
+
meta["error"] = {
|
|
14855
|
+
message: error.message,
|
|
14856
|
+
stack: error.stack
|
|
14857
|
+
};
|
|
14811
14858
|
} else {
|
|
14812
|
-
|
|
14859
|
+
meta["error"] = String(error);
|
|
14813
14860
|
}
|
|
14861
|
+
this.traceLogger.logStructured("verbose", "HTTP request failed", meta, "HTTPTraceInterceptor");
|
|
14814
14862
|
throw error;
|
|
14815
14863
|
}));
|
|
14816
14864
|
}
|
|
@@ -14835,21 +14883,12 @@ var LoggingInterceptor = class {
|
|
|
14835
14883
|
} catch (error) {
|
|
14836
14884
|
return {
|
|
14837
14885
|
_error: "Failed to serialize data",
|
|
14838
|
-
|
|
14886
|
+
_type: typeof data,
|
|
14887
|
+
_constructor: data?.constructor?.name
|
|
14839
14888
|
};
|
|
14840
14889
|
}
|
|
14841
14890
|
}
|
|
14842
14891
|
/**
|
|
14843
|
-
* 判断是否是 JSON 响应类型
|
|
14844
|
-
*/
|
|
14845
|
-
isJsonContentType(contentType) {
|
|
14846
|
-
if (typeof contentType !== "string") {
|
|
14847
|
-
return false;
|
|
14848
|
-
}
|
|
14849
|
-
const contentTypeLower = contentType.toLowerCase();
|
|
14850
|
-
return contentTypeLower.includes("application/json") || contentTypeLower.includes("application/vnd.api+json") || contentTypeLower.includes("+json");
|
|
14851
|
-
}
|
|
14852
|
-
/**
|
|
14853
14892
|
* 脱敏敏感字段
|
|
14854
14893
|
*/
|
|
14855
14894
|
maskSensitiveFields(data) {
|
|
@@ -14880,11 +14919,13 @@ var LoggingInterceptor = class {
|
|
|
14880
14919
|
LoggingInterceptor = _ts_decorate3([
|
|
14881
14920
|
(0, import_common3.Injectable)(),
|
|
14882
14921
|
_ts_param2(0, (0, import_common3.Inject)(TRACE_LOGGER)),
|
|
14883
|
-
_ts_param2(2, (0, import_common3.Inject)(
|
|
14922
|
+
_ts_param2(2, (0, import_common3.Inject)(AppLogger)),
|
|
14923
|
+
_ts_param2(3, (0, import_common3.Inject)(logger_config_default.KEY)),
|
|
14884
14924
|
_ts_metadata2("design:type", Function),
|
|
14885
14925
|
_ts_metadata2("design:paramtypes", [
|
|
14886
|
-
typeof
|
|
14926
|
+
typeof PinoLoggerService === "undefined" ? Object : PinoLoggerService,
|
|
14887
14927
|
typeof RequestContextService === "undefined" ? Object : RequestContextService,
|
|
14928
|
+
typeof AppLogger === "undefined" ? Object : AppLogger,
|
|
14888
14929
|
typeof import_config2.ConfigType === "undefined" ? Object : import_config2.ConfigType
|
|
14889
14930
|
])
|
|
14890
14931
|
], LoggingInterceptor);
|
|
@@ -15051,6 +15092,7 @@ LoggerModule = _ts_decorate5([
|
|
|
15051
15092
|
AppLogger,
|
|
15052
15093
|
LoggerContextMiddleware,
|
|
15053
15094
|
LoggerModule,
|
|
15095
|
+
PinoLoggerService,
|
|
15054
15096
|
TRACE_LOGGER
|
|
15055
15097
|
});
|
|
15056
15098
|
//# sourceMappingURL=index.cjs.map
|