feat(logging): add opt-in debug logging mode
- Add validated LOG_MODE configuration and route trace-aware diagnostics through debug. - Select debug mode in shared test setup while preserving DEBUG namespace control. - Add logger/config regression coverage and document runtime configuration.
This commit is contained in:
1 parent
ff9a700c86
commit
ceed27ad9e
10 files changed
+214
-4
No files matched your search
@@ -1,9 +1,108 @@
|
||||
import { ConsoleLogger, ConsoleLoggerOptions, LogLevel } from "@nestjs/common";
|
||||
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
||||
import createDebug = require("debug");
|
||||
import { getTraceId } from "./trace-context";
|
||||
import { LogMode } from "../config/app.config";
|
||||
|
||||
export class TraceLogger extends ConsoleLogger {
|
||||
private readonly loggerContext: string;
|
||||
|
||||
constructor(context?: string, options: ConsoleLoggerOptions = {}) {
|
||||
super(context as string, options);
|
||||
this.loggerContext = context ?? "Application";
|
||||
}
|
||||
|
||||
override log(...args: Parameters<ConsoleLogger["log"]>): void {
|
||||
if (this.useDebugOutput()) {
|
||||
const [message, ...optionalParams] = args;
|
||||
this.writeDebug("log", message, optionalParams);
|
||||
return;
|
||||
}
|
||||
super.log(...args);
|
||||
}
|
||||
|
||||
override error(...args: Parameters<ConsoleLogger["error"]>): void {
|
||||
if (this.useDebugOutput()) {
|
||||
const [message, ...optionalParams] = args;
|
||||
this.writeDebug("error", message, optionalParams);
|
||||
return;
|
||||
}
|
||||
super.error(...args);
|
||||
}
|
||||
|
||||
override warn(...args: Parameters<ConsoleLogger["warn"]>): void {
|
||||
if (this.useDebugOutput()) {
|
||||
const [message, ...optionalParams] = args;
|
||||
this.writeDebug("warn", message, optionalParams);
|
||||
return;
|
||||
}
|
||||
super.warn(...args);
|
||||
}
|
||||
|
||||
override debug(...args: Parameters<ConsoleLogger["debug"]>): void {
|
||||
if (this.useDebugOutput()) {
|
||||
const [message, ...optionalParams] = args;
|
||||
this.writeDebug("debug", message, optionalParams);
|
||||
return;
|
||||
}
|
||||
super.debug(...args);
|
||||
}
|
||||
|
||||
override verbose(...args: Parameters<ConsoleLogger["verbose"]>): void {
|
||||
if (this.useDebugOutput()) {
|
||||
const [message, ...optionalParams] = args;
|
||||
this.writeDebug("verbose", message, optionalParams);
|
||||
return;
|
||||
}
|
||||
super.verbose(...args);
|
||||
}
|
||||
|
||||
override fatal(...args: Parameters<ConsoleLogger["fatal"]>): void {
|
||||
if (this.useDebugOutput()) {
|
||||
const [message, ...optionalParams] = args;
|
||||
this.writeDebug("fatal", message, optionalParams);
|
||||
return;
|
||||
}
|
||||
super.fatal(...args);
|
||||
}
|
||||
|
||||
private useDebugOutput(): boolean {
|
||||
return process.env.LOG_MODE === LogMode.Debug;
|
||||
}
|
||||
|
||||
private writeDebug(
|
||||
level: LogLevel,
|
||||
message: unknown,
|
||||
optionalParams: unknown[],
|
||||
): void {
|
||||
const last = optionalParams[optionalParams.length - 1];
|
||||
const lastIsErrorStack =
|
||||
level === "error" &&
|
||||
optionalParams.length === 1 &&
|
||||
typeof last === "string" &&
|
||||
/(?:\n|^\s*at\s)/.test(last);
|
||||
const context =
|
||||
typeof last === "string" && !lastIsErrorStack
|
||||
? last
|
||||
: this.loggerContext;
|
||||
const details =
|
||||
typeof last === "string" && !lastIsErrorStack
|
||||
? optionalParams.slice(0, -1)
|
||||
: optionalParams;
|
||||
const traceId = getTraceId();
|
||||
const tracedContext = traceId ? `${context}:${traceId}` : context;
|
||||
const namespace =
|
||||
process.env.NODE_ENV === "test"
|
||||
? "test"
|
||||
: `liqa:${context.toLowerCase()}`;
|
||||
const messageText = this.stringifyMessage(message, level);
|
||||
const detailText = details
|
||||
.map((detail) => this.stringifyMessage(detail, level))
|
||||
.filter(Boolean)
|
||||
.join(" ");
|
||||
const output = `[${level.toUpperCase()}] [${tracedContext}] ${messageText}${detailText ? ` ${detailText}` : ""}`;
|
||||
|
||||
createDebug(namespace)("%s", output);
|
||||
}
|
||||
|
||||
protected override getTimestamp(): string {
|
||||
|
||||
@@ -20,6 +20,11 @@ export enum TraceMode {
|
||||
Off = "off",
|
||||
}
|
||||
|
||||
export enum LogMode {
|
||||
Console = "console",
|
||||
Debug = "debug",
|
||||
}
|
||||
|
||||
export class AppConfig {
|
||||
/** `production` in the Docker image; also selects the default CORS origins. */
|
||||
@IsString()
|
||||
@@ -40,6 +45,10 @@ export class AppConfig {
|
||||
@IsString()
|
||||
APP_VERSION: string = appPackage().version;
|
||||
|
||||
/** Route application diagnostics through `debug` instead of the console. */
|
||||
@IsEnum(LogMode)
|
||||
LOG_MODE: LogMode = LogMode.Console;
|
||||
|
||||
@IsString()
|
||||
DB_PATH: string = "data/liqa.db";
|
||||
|
||||
|
||||
Reference in new issue
Block a user