@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.d.cts
CHANGED
|
@@ -25,6 +25,7 @@ declare abstract class BasePinoLogger implements LoggerService {
|
|
|
25
25
|
*/
|
|
26
26
|
logStructured(level: LogLevel, message: string, meta: Record<string, unknown>, context?: string): void;
|
|
27
27
|
protected write(level: LogLevel, message: unknown, optionalParams: unknown[], treatStack?: boolean): void;
|
|
28
|
+
private processLogParams;
|
|
28
29
|
private extractOptionalParams;
|
|
29
30
|
private buildMessagePayload;
|
|
30
31
|
private static looksLikeStack;
|
package/dist/index.d.ts
CHANGED
|
@@ -25,6 +25,7 @@ declare abstract class BasePinoLogger implements LoggerService {
|
|
|
25
25
|
*/
|
|
26
26
|
logStructured(level: LogLevel, message: string, meta: Record<string, unknown>, context?: string): void;
|
|
27
27
|
protected write(level: LogLevel, message: unknown, optionalParams: unknown[], treatStack?: boolean): void;
|
|
28
|
+
private processLogParams;
|
|
28
29
|
private extractOptionalParams;
|
|
29
30
|
private buildMessagePayload;
|
|
30
31
|
private static looksLikeStack;
|
package/dist/index.js
CHANGED
|
@@ -7241,7 +7241,7 @@ var require_tap = __commonJS({
|
|
|
7241
7241
|
var lift_1 = require_lift();
|
|
7242
7242
|
var OperatorSubscriber_1 = require_OperatorSubscriber();
|
|
7243
7243
|
var identity_1 = require_identity();
|
|
7244
|
-
function
|
|
7244
|
+
function tap2(observerOrNext, error, complete) {
|
|
7245
7245
|
var tapObserver = isFunction_1.isFunction(observerOrNext) || error || complete ? {
|
|
7246
7246
|
next: observerOrNext,
|
|
7247
7247
|
error,
|
|
@@ -7274,8 +7274,8 @@ var require_tap = __commonJS({
|
|
|
7274
7274
|
}));
|
|
7275
7275
|
}) : identity_1.identity;
|
|
7276
7276
|
}
|
|
7277
|
-
__name(
|
|
7278
|
-
exports.tap =
|
|
7277
|
+
__name(tap2, "tap");
|
|
7278
|
+
exports.tap = tap2;
|
|
7279
7279
|
}
|
|
7280
7280
|
});
|
|
7281
7281
|
|
|
@@ -9000,6 +9000,47 @@ var OBSERVABLE_LOGGER_SERVICE = /* @__PURE__ */ Symbol("OBSERVABLE_LOGGER_SERVIC
|
|
|
9000
9000
|
|
|
9001
9001
|
// src/service/app-logger.service.ts
|
|
9002
9002
|
import { RequestContextService, ObservableService } from "@lark-apaas/nestjs-common";
|
|
9003
|
+
|
|
9004
|
+
// src/utils/safeStringify.ts
|
|
9005
|
+
function safeStringify(obj) {
|
|
9006
|
+
const seen = /* @__PURE__ */ new Set();
|
|
9007
|
+
try {
|
|
9008
|
+
return JSON.stringify(obj, (key, value) => {
|
|
9009
|
+
if (typeof value === "object" && value !== null) {
|
|
9010
|
+
if (seen.has(value)) {
|
|
9011
|
+
return "[Circular]";
|
|
9012
|
+
}
|
|
9013
|
+
seen.add(value);
|
|
9014
|
+
}
|
|
9015
|
+
if (typeof value === "bigint") {
|
|
9016
|
+
return value.toString();
|
|
9017
|
+
}
|
|
9018
|
+
if (value instanceof Date) {
|
|
9019
|
+
return value.toISOString();
|
|
9020
|
+
}
|
|
9021
|
+
if (value instanceof Map) {
|
|
9022
|
+
return Object.fromEntries(value);
|
|
9023
|
+
}
|
|
9024
|
+
if (value instanceof Set) {
|
|
9025
|
+
return Array.from(value);
|
|
9026
|
+
}
|
|
9027
|
+
if (typeof value === "undefined") {
|
|
9028
|
+
return "undefined";
|
|
9029
|
+
}
|
|
9030
|
+
if (typeof value === "symbol") {
|
|
9031
|
+
return value.toString();
|
|
9032
|
+
}
|
|
9033
|
+
return value;
|
|
9034
|
+
});
|
|
9035
|
+
} catch {
|
|
9036
|
+
return "";
|
|
9037
|
+
} finally {
|
|
9038
|
+
seen.clear();
|
|
9039
|
+
}
|
|
9040
|
+
}
|
|
9041
|
+
__name(safeStringify, "safeStringify");
|
|
9042
|
+
|
|
9043
|
+
// src/service/app-logger.service.ts
|
|
9003
9044
|
function _ts_decorate(decorators, target, key, desc) {
|
|
9004
9045
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
9005
9046
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -9183,6 +9224,7 @@ var BasePinoLogger = class _BasePinoLogger {
|
|
|
9183
9224
|
const { context, stack, extras } = this.extractOptionalParams(optionalParams, treatStack);
|
|
9184
9225
|
const requestState = this.contextStore.getContext();
|
|
9185
9226
|
const traceId = requestState?.requestId ?? null;
|
|
9227
|
+
const firstParam = optionalParams[0];
|
|
9186
9228
|
const flatObject = /* @__PURE__ */ __name((extra) => {
|
|
9187
9229
|
let res = {};
|
|
9188
9230
|
try {
|
|
@@ -9227,16 +9269,50 @@ var BasePinoLogger = class _BasePinoLogger {
|
|
|
9227
9269
|
}
|
|
9228
9270
|
const pinoLevel = mapLogLevelToPino(level);
|
|
9229
9271
|
const sanitizedPayload = sanitizeValue(payload);
|
|
9272
|
+
const isTriggerLog = firstParam?.module === "trigger";
|
|
9273
|
+
const platformModule = isTriggerLog ? "trigger" : firstParam?.paas_attributes_module;
|
|
9230
9274
|
if (process.env.NODE_ENV === "development") {
|
|
9275
|
+
if (platformModule) {
|
|
9276
|
+
return;
|
|
9277
|
+
}
|
|
9231
9278
|
if (messageText) {
|
|
9232
9279
|
this.logger[pinoLevel](sanitizedPayload, messageText);
|
|
9233
9280
|
} else {
|
|
9234
9281
|
this.logger[pinoLevel](sanitizedPayload);
|
|
9235
9282
|
}
|
|
9236
9283
|
} else {
|
|
9237
|
-
|
|
9284
|
+
let finalMessage = messageText;
|
|
9285
|
+
if (platformModule) {
|
|
9286
|
+
if (typeof message !== "string") {
|
|
9287
|
+
finalMessage = safeStringify(message);
|
|
9288
|
+
}
|
|
9289
|
+
this.observableService.log(level, finalMessage ?? "", {
|
|
9290
|
+
...payload,
|
|
9291
|
+
module: platformModule
|
|
9292
|
+
});
|
|
9293
|
+
} else {
|
|
9294
|
+
const reportMessage = this.processLogParams([
|
|
9295
|
+
message,
|
|
9296
|
+
...optionalParams
|
|
9297
|
+
]);
|
|
9298
|
+
this.observableService.log(level, reportMessage, {});
|
|
9299
|
+
}
|
|
9238
9300
|
}
|
|
9239
9301
|
}
|
|
9302
|
+
processLogParams(args) {
|
|
9303
|
+
if (args.length === 1) {
|
|
9304
|
+
const firstParam = args[0];
|
|
9305
|
+
if (typeof firstParam !== "string") {
|
|
9306
|
+
return safeStringify(firstParam);
|
|
9307
|
+
}
|
|
9308
|
+
return firstParam;
|
|
9309
|
+
}
|
|
9310
|
+
const obj = {};
|
|
9311
|
+
for (let i = 0; i < args.length; i++) {
|
|
9312
|
+
obj[i.toString()] = args[i];
|
|
9313
|
+
}
|
|
9314
|
+
return safeStringify(obj);
|
|
9315
|
+
}
|
|
9240
9316
|
extractOptionalParams(optionalParams, treatStack) {
|
|
9241
9317
|
const params = [
|
|
9242
9318
|
...optionalParams
|
|
@@ -9434,9 +9510,14 @@ var LoggingInterceptor = class {
|
|
|
9434
9510
|
if (context.getType() !== "http") {
|
|
9435
9511
|
return next.handle();
|
|
9436
9512
|
}
|
|
9513
|
+
const isDev = process.env.NODE_ENV === "development";
|
|
9437
9514
|
const httpContext = context.switchToHttp();
|
|
9438
9515
|
const req = httpContext.getRequest();
|
|
9439
9516
|
const res = httpContext.getResponse();
|
|
9517
|
+
const protocol = req.protocol;
|
|
9518
|
+
const host = req.get("Host");
|
|
9519
|
+
const originalUrl = req.originalUrl;
|
|
9520
|
+
const fullUrl = `${protocol}://${host}${originalUrl}`;
|
|
9440
9521
|
const method = req.method;
|
|
9441
9522
|
const path = req.path;
|
|
9442
9523
|
this.requestContext.setContext({
|
|
@@ -9473,8 +9554,10 @@ var LoggingInterceptor = class {
|
|
|
9473
9554
|
if (this.config.logRequestBody && Object.keys(req.query || {}).length > 0) {
|
|
9474
9555
|
requestMeta["query_params"] = this.sanitizeAndTruncate(req.query);
|
|
9475
9556
|
}
|
|
9476
|
-
|
|
9557
|
+
if (isDev) {
|
|
9558
|
+
this.appLogger.log(`HTTP request started
|
|
9477
9559
|
url=${req.url}`, "HTTPTraceInterceptor");
|
|
9560
|
+
}
|
|
9478
9561
|
this.traceLogger.logStructured("verbose", "HTTP request started", requestMeta, "HTTPTraceInterceptor");
|
|
9479
9562
|
let logged = false;
|
|
9480
9563
|
res.on("finish", () => {
|
|
@@ -9512,7 +9595,7 @@ url=${req.url}
|
|
|
9512
9595
|
status_code=${statusCode}
|
|
9513
9596
|
duration_ms=${durationMs}`, "HTTPTraceInterceptor");
|
|
9514
9597
|
}
|
|
9515
|
-
} else {
|
|
9598
|
+
} else if (isDev) {
|
|
9516
9599
|
if (shouldLogBody) {
|
|
9517
9600
|
appLog(`HTTP request completed
|
|
9518
9601
|
url=${req.url}
|
|
@@ -9528,8 +9611,33 @@ duration_ms=${durationMs}`, "HTTPTraceInterceptor");
|
|
|
9528
9611
|
}
|
|
9529
9612
|
this.traceLogger.logStructured(traceLevel, isError ? "HTTP request failed" : "HTTP request completed", responseMeta, "HTTPTraceInterceptor");
|
|
9530
9613
|
});
|
|
9531
|
-
return next.handle().pipe((0, import_operators.
|
|
9532
|
-
|
|
9614
|
+
return next.handle().pipe((0, import_operators.tap)({
|
|
9615
|
+
next: /* @__PURE__ */ __name((data) => {
|
|
9616
|
+
const statusCode = res.statusCode;
|
|
9617
|
+
const durationMs = Date.now() - startedAt;
|
|
9618
|
+
const isError = statusCode > 400;
|
|
9619
|
+
const responseData = {
|
|
9620
|
+
method,
|
|
9621
|
+
path,
|
|
9622
|
+
url: fullUrl,
|
|
9623
|
+
duration_ms: durationMs,
|
|
9624
|
+
error_message: isError ? data?.message ?? "" : ""
|
|
9625
|
+
};
|
|
9626
|
+
if (req.body) responseData.request_body = req.body;
|
|
9627
|
+
if (data) responseData.response = data;
|
|
9628
|
+
const reportData = JSON.stringify(responseData);
|
|
9629
|
+
if (!isDev) {
|
|
9630
|
+
this.appLogger.log(reportData, {
|
|
9631
|
+
paas_attributes_module: "app_server",
|
|
9632
|
+
source_type: "platform"
|
|
9633
|
+
}, "HTTPTraceInterceptor");
|
|
9634
|
+
}
|
|
9635
|
+
}, "next")
|
|
9636
|
+
}), (0, import_operators.catchError)((error) => {
|
|
9637
|
+
this.appLogger.error(format("HTTP request exception\nmethod=%s url=%s\nerror=%o", req.method, req.url, error), {
|
|
9638
|
+
paas_attributes_module: "app_server",
|
|
9639
|
+
source_type: "platform"
|
|
9640
|
+
}, "HTTPTraceInterceptor");
|
|
9533
9641
|
throw error;
|
|
9534
9642
|
}));
|
|
9535
9643
|
}
|