@lark-apaas/nestjs-logger 1.0.4 → 1.0.6-alpha.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/dist/index.cjs +116 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +116 -8
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -7247,7 +7247,7 @@ var require_tap = __commonJS({
|
|
|
7247
7247
|
var lift_1 = require_lift();
|
|
7248
7248
|
var OperatorSubscriber_1 = require_OperatorSubscriber();
|
|
7249
7249
|
var identity_1 = require_identity();
|
|
7250
|
-
function
|
|
7250
|
+
function tap2(observerOrNext, error, complete) {
|
|
7251
7251
|
var tapObserver = isFunction_1.isFunction(observerOrNext) || error || complete ? {
|
|
7252
7252
|
next: observerOrNext,
|
|
7253
7253
|
error,
|
|
@@ -7280,8 +7280,8 @@ var require_tap = __commonJS({
|
|
|
7280
7280
|
}));
|
|
7281
7281
|
}) : identity_1.identity;
|
|
7282
7282
|
}
|
|
7283
|
-
__name(
|
|
7284
|
-
exports2.tap =
|
|
7283
|
+
__name(tap2, "tap");
|
|
7284
|
+
exports2.tap = tap2;
|
|
7285
7285
|
}
|
|
7286
7286
|
});
|
|
7287
7287
|
|
|
@@ -9017,6 +9017,47 @@ var OBSERVABLE_LOGGER_SERVICE = /* @__PURE__ */ Symbol("OBSERVABLE_LOGGER_SERVIC
|
|
|
9017
9017
|
|
|
9018
9018
|
// src/service/app-logger.service.ts
|
|
9019
9019
|
var import_nestjs_common = require("@lark-apaas/nestjs-common");
|
|
9020
|
+
|
|
9021
|
+
// src/utils/safeStringify.ts
|
|
9022
|
+
function safeStringify(obj) {
|
|
9023
|
+
const seen = /* @__PURE__ */ new Set();
|
|
9024
|
+
try {
|
|
9025
|
+
return JSON.stringify(obj, (key, value) => {
|
|
9026
|
+
if (typeof value === "object" && value !== null) {
|
|
9027
|
+
if (seen.has(value)) {
|
|
9028
|
+
return "[Circular]";
|
|
9029
|
+
}
|
|
9030
|
+
seen.add(value);
|
|
9031
|
+
}
|
|
9032
|
+
if (typeof value === "bigint") {
|
|
9033
|
+
return value.toString();
|
|
9034
|
+
}
|
|
9035
|
+
if (value instanceof Date) {
|
|
9036
|
+
return value.toISOString();
|
|
9037
|
+
}
|
|
9038
|
+
if (value instanceof Map) {
|
|
9039
|
+
return Object.fromEntries(value);
|
|
9040
|
+
}
|
|
9041
|
+
if (value instanceof Set) {
|
|
9042
|
+
return Array.from(value);
|
|
9043
|
+
}
|
|
9044
|
+
if (typeof value === "undefined") {
|
|
9045
|
+
return "undefined";
|
|
9046
|
+
}
|
|
9047
|
+
if (typeof value === "symbol") {
|
|
9048
|
+
return value.toString();
|
|
9049
|
+
}
|
|
9050
|
+
return value;
|
|
9051
|
+
});
|
|
9052
|
+
} catch {
|
|
9053
|
+
return "";
|
|
9054
|
+
} finally {
|
|
9055
|
+
seen.clear();
|
|
9056
|
+
}
|
|
9057
|
+
}
|
|
9058
|
+
__name(safeStringify, "safeStringify");
|
|
9059
|
+
|
|
9060
|
+
// src/service/app-logger.service.ts
|
|
9020
9061
|
function _ts_decorate(decorators, target, key, desc) {
|
|
9021
9062
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
9022
9063
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -9200,6 +9241,7 @@ var BasePinoLogger = class _BasePinoLogger {
|
|
|
9200
9241
|
const { context, stack, extras } = this.extractOptionalParams(optionalParams, treatStack);
|
|
9201
9242
|
const requestState = this.contextStore.getContext();
|
|
9202
9243
|
const traceId = requestState?.requestId ?? null;
|
|
9244
|
+
const firstParam = optionalParams[0];
|
|
9203
9245
|
const flatObject = /* @__PURE__ */ __name((extra) => {
|
|
9204
9246
|
let res = {};
|
|
9205
9247
|
try {
|
|
@@ -9244,16 +9286,50 @@ var BasePinoLogger = class _BasePinoLogger {
|
|
|
9244
9286
|
}
|
|
9245
9287
|
const pinoLevel = mapLogLevelToPino(level);
|
|
9246
9288
|
const sanitizedPayload = sanitizeValue(payload);
|
|
9289
|
+
const isTriggerLog = firstParam?.module === "trigger";
|
|
9290
|
+
const platformModule = isTriggerLog ? "trigger" : firstParam?.paas_attributes_module;
|
|
9247
9291
|
if (process.env.NODE_ENV === "development") {
|
|
9292
|
+
if (platformModule) {
|
|
9293
|
+
return;
|
|
9294
|
+
}
|
|
9248
9295
|
if (messageText) {
|
|
9249
9296
|
this.logger[pinoLevel](sanitizedPayload, messageText);
|
|
9250
9297
|
} else {
|
|
9251
9298
|
this.logger[pinoLevel](sanitizedPayload);
|
|
9252
9299
|
}
|
|
9253
9300
|
} else {
|
|
9254
|
-
|
|
9301
|
+
let finalMessage = messageText;
|
|
9302
|
+
if (platformModule) {
|
|
9303
|
+
if (typeof message !== "string") {
|
|
9304
|
+
finalMessage = safeStringify(message);
|
|
9305
|
+
}
|
|
9306
|
+
this.observableService.log(level, finalMessage ?? "", {
|
|
9307
|
+
...payload,
|
|
9308
|
+
module: platformModule
|
|
9309
|
+
});
|
|
9310
|
+
} else {
|
|
9311
|
+
const reportMessage = this.processLogParams([
|
|
9312
|
+
message,
|
|
9313
|
+
...optionalParams
|
|
9314
|
+
]);
|
|
9315
|
+
this.observableService.log(level, reportMessage, {});
|
|
9316
|
+
}
|
|
9255
9317
|
}
|
|
9256
9318
|
}
|
|
9319
|
+
processLogParams(args) {
|
|
9320
|
+
if (args.length === 1) {
|
|
9321
|
+
const firstParam = args[0];
|
|
9322
|
+
if (typeof firstParam !== "string") {
|
|
9323
|
+
return safeStringify(firstParam);
|
|
9324
|
+
}
|
|
9325
|
+
return firstParam;
|
|
9326
|
+
}
|
|
9327
|
+
const obj = {};
|
|
9328
|
+
for (let i = 0; i < args.length; i++) {
|
|
9329
|
+
obj[i.toString()] = args[i];
|
|
9330
|
+
}
|
|
9331
|
+
return safeStringify(obj);
|
|
9332
|
+
}
|
|
9257
9333
|
extractOptionalParams(optionalParams, treatStack) {
|
|
9258
9334
|
const params = [
|
|
9259
9335
|
...optionalParams
|
|
@@ -9451,9 +9527,14 @@ var LoggingInterceptor = class {
|
|
|
9451
9527
|
if (context.getType() !== "http") {
|
|
9452
9528
|
return next.handle();
|
|
9453
9529
|
}
|
|
9530
|
+
const isDev = process.env.NODE_ENV === "development";
|
|
9454
9531
|
const httpContext = context.switchToHttp();
|
|
9455
9532
|
const req = httpContext.getRequest();
|
|
9456
9533
|
const res = httpContext.getResponse();
|
|
9534
|
+
const protocol = req.protocol;
|
|
9535
|
+
const host = req.get("Host");
|
|
9536
|
+
const originalUrl = req.originalUrl;
|
|
9537
|
+
const fullUrl = `${protocol}://${host}${originalUrl}`;
|
|
9457
9538
|
const method = req.method;
|
|
9458
9539
|
const path = req.path;
|
|
9459
9540
|
this.requestContext.setContext({
|
|
@@ -9490,8 +9571,10 @@ var LoggingInterceptor = class {
|
|
|
9490
9571
|
if (this.config.logRequestBody && Object.keys(req.query || {}).length > 0) {
|
|
9491
9572
|
requestMeta["query_params"] = this.sanitizeAndTruncate(req.query);
|
|
9492
9573
|
}
|
|
9493
|
-
|
|
9574
|
+
if (isDev) {
|
|
9575
|
+
this.appLogger.log(`HTTP request started
|
|
9494
9576
|
url=${req.url}`, "HTTPTraceInterceptor");
|
|
9577
|
+
}
|
|
9495
9578
|
this.traceLogger.logStructured("verbose", "HTTP request started", requestMeta, "HTTPTraceInterceptor");
|
|
9496
9579
|
let logged = false;
|
|
9497
9580
|
res.on("finish", () => {
|
|
@@ -9529,7 +9612,7 @@ url=${req.url}
|
|
|
9529
9612
|
status_code=${statusCode}
|
|
9530
9613
|
duration_ms=${durationMs}`, "HTTPTraceInterceptor");
|
|
9531
9614
|
}
|
|
9532
|
-
} else {
|
|
9615
|
+
} else if (isDev) {
|
|
9533
9616
|
if (shouldLogBody) {
|
|
9534
9617
|
appLog(`HTTP request completed
|
|
9535
9618
|
url=${req.url}
|
|
@@ -9545,8 +9628,33 @@ duration_ms=${durationMs}`, "HTTPTraceInterceptor");
|
|
|
9545
9628
|
}
|
|
9546
9629
|
this.traceLogger.logStructured(traceLevel, isError ? "HTTP request failed" : "HTTP request completed", responseMeta, "HTTPTraceInterceptor");
|
|
9547
9630
|
});
|
|
9548
|
-
return next.handle().pipe((0, import_operators.
|
|
9549
|
-
|
|
9631
|
+
return next.handle().pipe((0, import_operators.tap)({
|
|
9632
|
+
next: /* @__PURE__ */ __name((data) => {
|
|
9633
|
+
const statusCode = res.statusCode;
|
|
9634
|
+
const durationMs = Date.now() - startedAt;
|
|
9635
|
+
const isError = statusCode > 400;
|
|
9636
|
+
const responseData = {
|
|
9637
|
+
method,
|
|
9638
|
+
path,
|
|
9639
|
+
url: fullUrl,
|
|
9640
|
+
duration_ms: durationMs,
|
|
9641
|
+
error_message: isError ? data?.message ?? "" : ""
|
|
9642
|
+
};
|
|
9643
|
+
if (req.body) responseData.request_body = req.body;
|
|
9644
|
+
if (data) responseData.response = data;
|
|
9645
|
+
const reportData = JSON.stringify(responseData);
|
|
9646
|
+
if (!isDev) {
|
|
9647
|
+
this.appLogger.log(reportData, {
|
|
9648
|
+
paas_attributes_module: "app_server",
|
|
9649
|
+
source_type: "platform"
|
|
9650
|
+
}, "HTTPTraceInterceptor");
|
|
9651
|
+
}
|
|
9652
|
+
}, "next")
|
|
9653
|
+
}), (0, import_operators.catchError)((error) => {
|
|
9654
|
+
this.appLogger.error((0, import_util.format)("HTTP request exception\nmethod=%s url=%s\nerror=%o", req.method, req.url, error), {
|
|
9655
|
+
paas_attributes_module: "app_server",
|
|
9656
|
+
source_type: "platform"
|
|
9657
|
+
}, "HTTPTraceInterceptor");
|
|
9550
9658
|
throw error;
|
|
9551
9659
|
}));
|
|
9552
9660
|
}
|