Keep test output quiet with opt-in diagnostic logs (#25)
Test runs stay quiet by default. Diagnostic logs can be enabled explicitly when troubleshooting, while normal application logging and saved scenario output remain available. Resolves #22. --------- Co-authored-by: ars9 <ars9@pm.me> Co-authored-by: agent <agent@halstation.halstation.local> Reviewed-on: #25 Co-authored-by: agent-codex <agent-codex@ars9.space>
This commit was merged in pull request #25.
This commit is contained in:
1 parent
8b48d4812d
commit
c0fe15dc01
24 files changed
+250
-36
No files matched your search
@@ -8,6 +8,9 @@
|
|||||||
# NODE_ENV=development
|
# NODE_ENV=development
|
||||||
# PORT=3000
|
# PORT=3000
|
||||||
|
|
||||||
|
## Application log output: console (default) or debug (controlled by DEBUG namespaces)
|
||||||
|
# LOG_MODE=console
|
||||||
|
|
||||||
## Optional account authentication (disabled unless explicitly enabled)
|
## Optional account authentication (disabled unless explicitly enabled)
|
||||||
# AUTH_ENABLED=true
|
# AUTH_ENABLED=true
|
||||||
## Required when AUTH_ENABLED=true. Set a private, high-entropy value outside source control.
|
## Required when AUTH_ENABLED=true. Set a private, high-entropy value outside source control.
|
||||||
|
|||||||
+2
-1
@@ -30,6 +30,7 @@ All settings are validated at startup by `server/src/config/app.config.ts`.
|
|||||||
| `NODE_ENV` | `development` | `production` in the Docker image. Shown in the startup log, and selects the default CORS origins |
|
| `NODE_ENV` | `development` | `production` in the Docker image. Shown in the startup log, and selects the default CORS origins |
|
||||||
| `CORS_ORIGINS` | *(empty)* | Comma-separated origins allowed to call the API cross-origin. When empty: the Vite dev server (`http://localhost:5173`, `http://127.0.0.1:5173`) unless `NODE_ENV=production`, then the compose port (`http://localhost:13000`, `http://127.0.0.1:13000`) |
|
| `CORS_ORIGINS` | *(empty)* | Comma-separated origins allowed to call the API cross-origin. When empty: the Vite dev server (`http://localhost:5173`, `http://127.0.0.1:5173`) unless `NODE_ENV=production`, then the compose port (`http://localhost:13000`, `http://127.0.0.1:13000`) |
|
||||||
| `PORT` | `3000` | HTTP port |
|
| `PORT` | `3000` | HTTP port |
|
||||||
|
| `LOG_MODE` | `console` | `console` preserves the default server output; `debug` routes application diagnostics through the `debug` package and only emits namespaces enabled by `DEBUG` (for example, `DEBUG=liqa:*`). |
|
||||||
| `DB_PATH` | `data/liqa.db` | SQLite database file |
|
| `DB_PATH` | `data/liqa.db` | SQLite database file |
|
||||||
| `FILES_DIR` | `data/files` | Where scenario files and run artifacts are stored |
|
| `FILES_DIR` | `data/files` | Where scenario files and run artifacts are stored |
|
||||||
| `FILE_RUN_EXPIRATION_DAYS` | `7` | How long run artifacts (downloads, failure dumps, traces) are kept |
|
| `FILE_RUN_EXPIRATION_DAYS` | `7` | How long run artifacts (downloads, failure dumps, traces) are kept |
|
||||||
@@ -191,7 +192,7 @@ Watch mode:
|
|||||||
npm -w server run test:watch
|
npm -w server run test:watch
|
||||||
```
|
```
|
||||||
|
|
||||||
Print the NestJS logs of the test app (they're silenced by default; `DEBUG=test`):
|
Server application logs use debug output during tests. They remain quiet unless the `test` namespace is enabled. To see NestJS and application logs:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
npm -w server run test:debug
|
npm -w server run test:debug
|
||||||
|
|||||||
Generated
+1
-1
@@ -17464,6 +17464,7 @@
|
|||||||
"better-sqlite3": "^12.8.0",
|
"better-sqlite3": "^12.8.0",
|
||||||
"class-transformer": "^0.5.1",
|
"class-transformer": "^0.5.1",
|
||||||
"class-validator": "^0.14.4",
|
"class-validator": "^0.14.4",
|
||||||
|
"debug": "^4.4.3",
|
||||||
"effect": "^3.21.0",
|
"effect": "^3.21.0",
|
||||||
"jsdom": "^29.0.2",
|
"jsdom": "^29.0.2",
|
||||||
"playwright": "^1.59.1",
|
"playwright": "^1.59.1",
|
||||||
@@ -17487,7 +17488,6 @@
|
|||||||
"@types/mozilla__readability": "^0.4.2",
|
"@types/mozilla__readability": "^0.4.2",
|
||||||
"@types/node": "^25.5.2",
|
"@types/node": "^25.5.2",
|
||||||
"@types/supertest": "^7.2.0",
|
"@types/supertest": "^7.2.0",
|
||||||
"debug": "^4.4.3",
|
|
||||||
"eslint": "^10.2.0",
|
"eslint": "^10.2.0",
|
||||||
"eslint-config-prettier": "^10.1.8",
|
"eslint-config-prettier": "^10.1.8",
|
||||||
"jest": "^30.3.0",
|
"jest": "^30.3.0",
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ module.exports = {
|
|||||||
transform: {
|
transform: {
|
||||||
'^.+\\.ts$': ['ts-jest', { tsconfig: 'test/tsconfig.json' }],
|
'^.+\\.ts$': ['ts-jest', { tsconfig: 'test/tsconfig.json' }],
|
||||||
},
|
},
|
||||||
|
setupFiles: ['<rootDir>/test/logging.setup.js'],
|
||||||
moduleNameMapper: {
|
moduleNameMapper: {
|
||||||
'^jsdom$': '<rootDir>/test/__mocks__/jsdom.ts',
|
'^jsdom$': '<rootDir>/test/__mocks__/jsdom.ts',
|
||||||
'^playwright$': '<rootDir>/test/__mocks__/playwright.ts',
|
'^playwright$': '<rootDir>/test/__mocks__/playwright.ts',
|
||||||
|
|||||||
+1
-1
@@ -38,6 +38,7 @@
|
|||||||
"better-sqlite3": "^12.8.0",
|
"better-sqlite3": "^12.8.0",
|
||||||
"class-transformer": "^0.5.1",
|
"class-transformer": "^0.5.1",
|
||||||
"class-validator": "^0.14.4",
|
"class-validator": "^0.14.4",
|
||||||
|
"debug": "^4.4.3",
|
||||||
"effect": "^3.21.0",
|
"effect": "^3.21.0",
|
||||||
"jsdom": "^29.0.2",
|
"jsdom": "^29.0.2",
|
||||||
"playwright": "^1.59.1",
|
"playwright": "^1.59.1",
|
||||||
@@ -61,7 +62,6 @@
|
|||||||
"@types/mozilla__readability": "^0.4.2",
|
"@types/mozilla__readability": "^0.4.2",
|
||||||
"@types/node": "^25.5.2",
|
"@types/node": "^25.5.2",
|
||||||
"@types/supertest": "^7.2.0",
|
"@types/supertest": "^7.2.0",
|
||||||
"debug": "^4.4.3",
|
|
||||||
"eslint": "^10.2.0",
|
"eslint": "^10.2.0",
|
||||||
"eslint-config-prettier": "^10.1.8",
|
"eslint-config-prettier": "^10.1.8",
|
||||||
"jest": "^30.3.0",
|
"jest": "^30.3.0",
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { SchedulerRegistry } from "@nestjs/schedule";
|
|||||||
import { InjectRepository } from "@nestjs/typeorm";
|
import { InjectRepository } from "@nestjs/typeorm";
|
||||||
import { CronJob } from "cron";
|
import { CronJob } from "cron";
|
||||||
import { LessThanOrEqual, Repository } from "typeorm";
|
import { LessThanOrEqual, Repository } from "typeorm";
|
||||||
import { TraceLogger } from "../common/trace-logger";
|
import { createAppLogger } from "../common/app-logger";
|
||||||
import { AppConfig } from "../config/app.config";
|
import { AppConfig } from "../config/app.config";
|
||||||
import { RevokedTokenEntity } from "./revoked-token.entity";
|
import { RevokedTokenEntity } from "./revoked-token.entity";
|
||||||
|
|
||||||
@@ -12,7 +12,7 @@ export const AUTH_REVOKED_TOKEN_CLEANUP_JOB = "auth-revoked-token-cleanup";
|
|||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class RevokedTokenCleanupService implements OnModuleInit {
|
export class RevokedTokenCleanupService implements OnModuleInit {
|
||||||
private readonly logger = new TraceLogger(RevokedTokenCleanupService.name);
|
private readonly logger = createAppLogger(RevokedTokenCleanupService.name);
|
||||||
private readonly enabled: boolean;
|
private readonly enabled: boolean;
|
||||||
private readonly cleanupCron: string;
|
private readonly cleanupCron: string;
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import type {
|
|||||||
ScriptLogger,
|
ScriptLogger,
|
||||||
} from "../code-executor/code-executor.service";
|
} from "../code-executor/code-executor.service";
|
||||||
import { CodeExecutorService } from "../code-executor/code-executor.service";
|
import { CodeExecutorService } from "../code-executor/code-executor.service";
|
||||||
import { TraceLogger } from "../common/trace-logger";
|
import { createAppLogger } from "../common/app-logger";
|
||||||
import type { EnvironmentData } from "../environment/environment.entity";
|
import type { EnvironmentData } from "../environment/environment.entity";
|
||||||
import { SessionContextService } from "../session/session-context.service";
|
import { SessionContextService } from "../session/session-context.service";
|
||||||
import { SessionService } from "../session/session.service";
|
import { SessionService } from "../session/session.service";
|
||||||
@@ -28,7 +28,7 @@ export interface OpenResult {
|
|||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class BrowserService {
|
export class BrowserService {
|
||||||
private readonly logger = new TraceLogger(BrowserService.name);
|
private readonly logger = createAppLogger(BrowserService.name);
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly sessionContextService: SessionContextService,
|
private readonly sessionContextService: SessionContextService,
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import { parse } from "acorn";
|
|||||||
import type { BrowserContext, Page } from "playwright";
|
import type { BrowserContext, Page } from "playwright";
|
||||||
import { URL } from "node:url";
|
import { URL } from "node:url";
|
||||||
import { downloadFile as downloadFileImpl } from "../common/file-downloader";
|
import { downloadFile as downloadFileImpl } from "../common/file-downloader";
|
||||||
import { TraceLogger } from "../common/trace-logger";
|
import { createAppLogger } from "../common/app-logger";
|
||||||
import type { EnvironmentData } from "../environment/environment.entity";
|
import type { EnvironmentData } from "../environment/environment.entity";
|
||||||
import type { FileStorageService } from "../file/file-storage.service";
|
import type { FileStorageService } from "../file/file-storage.service";
|
||||||
import type { DomNode } from "./dom-helpers";
|
import type { DomNode } from "./dom-helpers";
|
||||||
@@ -115,7 +115,7 @@ export interface ExecContext {
|
|||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class CodeExecutorService {
|
export class CodeExecutorService {
|
||||||
private readonly logger = new TraceLogger(CodeExecutorService.name);
|
private readonly logger = createAppLogger(CodeExecutorService.name);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validates `code` by wrapping it in an async function body and attempting
|
* Validates `code` by wrapping it in an async function body and attempting
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import { ConsoleLoggerOptions } from "@nestjs/common";
|
||||||
|
import { LogMode } from "../config/app.config";
|
||||||
|
import { DebugLogger } from "./debug-logger";
|
||||||
|
import { TraceLogger } from "./trace-logger";
|
||||||
|
|
||||||
|
/** Select the application's logger implementation at the configuration boundary. */
|
||||||
|
export function createAppLogger(
|
||||||
|
context?: string,
|
||||||
|
options?: ConsoleLoggerOptions,
|
||||||
|
) {
|
||||||
|
return process.env.LOG_MODE === LogMode.Debug
|
||||||
|
? new DebugLogger(context, options)
|
||||||
|
: new TraceLogger(context, options);
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
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";
|
||||||
|
|
||||||
|
/** Logger that routes all framework and application diagnostics through DEBUG. */
|
||||||
|
export class DebugLogger 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 {
|
||||||
|
this.write("log", args);
|
||||||
|
}
|
||||||
|
override error(...args: Parameters<ConsoleLogger["error"]>): void {
|
||||||
|
this.write("error", args);
|
||||||
|
}
|
||||||
|
override warn(...args: Parameters<ConsoleLogger["warn"]>): void {
|
||||||
|
this.write("warn", args);
|
||||||
|
}
|
||||||
|
override debug(...args: Parameters<ConsoleLogger["debug"]>): void {
|
||||||
|
this.write("debug", args);
|
||||||
|
}
|
||||||
|
override verbose(...args: Parameters<ConsoleLogger["verbose"]>): void {
|
||||||
|
this.write("verbose", args);
|
||||||
|
}
|
||||||
|
override fatal(...args: Parameters<ConsoleLogger["fatal"]>): void {
|
||||||
|
this.write("fatal", args);
|
||||||
|
}
|
||||||
|
|
||||||
|
private write(level: LogLevel, args: unknown[]): void {
|
||||||
|
const [message, ...optionalParams] = args;
|
||||||
|
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 stringify = (value: unknown): string => {
|
||||||
|
if (value instanceof Error) return value.stack ?? value.message;
|
||||||
|
if (typeof value === "string") return value;
|
||||||
|
try {
|
||||||
|
return JSON.stringify(value);
|
||||||
|
} catch {
|
||||||
|
return String(value);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const output = `[${level.toUpperCase()}] [${tracedContext}] ${stringify(message)}${details.length ? ` ${details.map(stringify).filter(Boolean).join(" ")}` : ""}`;
|
||||||
|
createDebug(namespace)("%s", output);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import { ConsoleLogger, ConsoleLoggerOptions, LogLevel } from "@nestjs/common";
|
import { ConsoleLogger, ConsoleLoggerOptions, LogLevel } from "@nestjs/common";
|
||||||
import { getTraceId } from "./trace-context";
|
import { getTraceId } from "./trace-context";
|
||||||
|
|
||||||
|
/** Runtime console logger. Output is always written using Nest's console behavior. */
|
||||||
export class TraceLogger extends ConsoleLogger {
|
export class TraceLogger extends ConsoleLogger {
|
||||||
constructor(context?: string, options: ConsoleLoggerOptions = {}) {
|
constructor(context?: string, options: ConsoleLoggerOptions = {}) {
|
||||||
super(context as string, options);
|
super(context as string, options);
|
||||||
@@ -10,6 +11,11 @@ export class TraceLogger extends ConsoleLogger {
|
|||||||
return new Date().toISOString();
|
return new Date().toISOString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override formatContext(context: string): string {
|
||||||
|
const traceId = getTraceId();
|
||||||
|
return super.formatContext(traceId ? `${context}:${traceId}` : context);
|
||||||
|
}
|
||||||
|
|
||||||
protected override formatMessage(
|
protected override formatMessage(
|
||||||
logLevel: LogLevel,
|
logLevel: LogLevel,
|
||||||
message: unknown,
|
message: unknown,
|
||||||
@@ -22,10 +28,4 @@ export class TraceLogger extends ConsoleLogger {
|
|||||||
const level = this.colorize(logLevel.toUpperCase(), logLevel);
|
const level = this.colorize(logLevel.toUpperCase(), logLevel);
|
||||||
return `${this.getTimestamp()} ${level} ${contextMessage}${output}${timestampDiff}\n`;
|
return `${this.getTimestamp()} ${level} ${contextMessage}${output}${timestampDiff}\n`;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override formatContext(context: string): string {
|
|
||||||
const traceId = getTraceId();
|
|
||||||
const traced = traceId ? `${context}:${traceId}` : context;
|
|
||||||
return super.formatContext(traced);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -20,6 +20,11 @@ export enum TraceMode {
|
|||||||
Off = "off",
|
Off = "off",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export enum LogMode {
|
||||||
|
Console = "console",
|
||||||
|
Debug = "debug",
|
||||||
|
}
|
||||||
|
|
||||||
export class AppConfig {
|
export class AppConfig {
|
||||||
/** `production` in the Docker image; also selects the default CORS origins. */
|
/** `production` in the Docker image; also selects the default CORS origins. */
|
||||||
@IsString()
|
@IsString()
|
||||||
@@ -40,6 +45,10 @@ export class AppConfig {
|
|||||||
@IsString()
|
@IsString()
|
||||||
APP_VERSION: string = appPackage().version;
|
APP_VERSION: string = appPackage().version;
|
||||||
|
|
||||||
|
/** Route application diagnostics through `debug` instead of the console. */
|
||||||
|
@IsEnum(LogMode)
|
||||||
|
LOG_MODE: LogMode = LogMode.Console;
|
||||||
|
|
||||||
@IsString()
|
@IsString()
|
||||||
DB_PATH: string = "data/liqa.db";
|
DB_PATH: string = "data/liqa.db";
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import * as fs from "node:fs/promises";
|
|||||||
import * as path from "node:path";
|
import * as path from "node:path";
|
||||||
import { CronJob } from "cron";
|
import { CronJob } from "cron";
|
||||||
import { Repository } from "typeorm";
|
import { Repository } from "typeorm";
|
||||||
import { TraceLogger } from "../common/trace-logger";
|
import { createAppLogger } from "../common/app-logger";
|
||||||
import { AppConfig } from "../config/app.config";
|
import { AppConfig } from "../config/app.config";
|
||||||
import { FileEntity } from "./file.entity";
|
import { FileEntity } from "./file.entity";
|
||||||
import { ScenarioFileEntity } from "./scenario-file.entity";
|
import { ScenarioFileEntity } from "./scenario-file.entity";
|
||||||
@@ -27,7 +27,7 @@ export interface FileMetadata {
|
|||||||
@Injectable()
|
@Injectable()
|
||||||
export class FileStorageService implements OnModuleInit {
|
export class FileStorageService implements OnModuleInit {
|
||||||
private static readonly DEFAULT_PAGE_SIZE = 20;
|
private static readonly DEFAULT_PAGE_SIZE = 20;
|
||||||
private readonly logger = new TraceLogger(FileStorageService.name);
|
private readonly logger = createAppLogger(FileStorageService.name);
|
||||||
private readonly filesDir: string;
|
private readonly filesDir: string;
|
||||||
private readonly fileRunExpirationDays: number;
|
private readonly fileRunExpirationDays: number;
|
||||||
private readonly fileCleanupCron: string;
|
private readonly fileCleanupCron: string;
|
||||||
|
|||||||
@@ -7,11 +7,11 @@ import {
|
|||||||
InternalServerErrorException,
|
InternalServerErrorException,
|
||||||
} from "@nestjs/common";
|
} from "@nestjs/common";
|
||||||
import type { Request, Response } from "express";
|
import type { Request, Response } from "express";
|
||||||
import { TraceLogger } from "../common/trace-logger";
|
import { createAppLogger } from "../common/app-logger";
|
||||||
|
|
||||||
@Catch(BadRequestException, InternalServerErrorException)
|
@Catch(BadRequestException, InternalServerErrorException)
|
||||||
export class HttpExceptionFilter implements ExceptionFilter {
|
export class HttpExceptionFilter implements ExceptionFilter {
|
||||||
private readonly logger = new TraceLogger(HttpExceptionFilter.name);
|
private readonly logger = createAppLogger(HttpExceptionFilter.name);
|
||||||
|
|
||||||
catch(exception: HttpException, host: ArgumentsHost): void {
|
catch(exception: HttpException, host: ArgumentsHost): void {
|
||||||
const ctx = host.switchToHttp();
|
const ctx = host.switchToHttp();
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import {
|
|||||||
} from "@nestjs/common";
|
} from "@nestjs/common";
|
||||||
import type { Request, Response } from "express";
|
import type { Request, Response } from "express";
|
||||||
import { Observable, tap } from "rxjs";
|
import { Observable, tap } from "rxjs";
|
||||||
import { TraceLogger } from "../common/trace-logger";
|
import { createAppLogger } from "../common/app-logger";
|
||||||
|
|
||||||
export function redactAuthRequestBody(path: string, body: unknown): unknown {
|
export function redactAuthRequestBody(path: string, body: unknown): unknown {
|
||||||
const normalizedPath = path.toLowerCase().replace(/\/+$/, "");
|
const normalizedPath = path.toLowerCase().replace(/\/+$/, "");
|
||||||
@@ -28,7 +28,7 @@ export function redactAuthRequestBody(path: string, body: unknown): unknown {
|
|||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class LoggingInterceptor implements NestInterceptor {
|
export class LoggingInterceptor implements NestInterceptor {
|
||||||
private readonly logger = new TraceLogger(LoggingInterceptor.name);
|
private readonly logger = createAppLogger(LoggingInterceptor.name);
|
||||||
|
|
||||||
intercept(context: ExecutionContext, next: CallHandler): Observable<unknown> {
|
intercept(context: ExecutionContext, next: CallHandler): Observable<unknown> {
|
||||||
const http = context.switchToHttp();
|
const http = context.switchToHttp();
|
||||||
|
|||||||
+4
-4
@@ -8,7 +8,7 @@ import { AppModule } from "./app.module";
|
|||||||
import { appPackage } from "./common/app-package";
|
import { appPackage } from "./common/app-package";
|
||||||
import { configureBodyParsers } from "./common/body-parsers";
|
import { configureBodyParsers } from "./common/body-parsers";
|
||||||
import { corsOrigins } from "./common/cors";
|
import { corsOrigins } from "./common/cors";
|
||||||
import { TraceLogger } from "./common/trace-logger";
|
import { createAppLogger } from "./common/app-logger";
|
||||||
import { AppConfig } from "./config/app.config";
|
import { AppConfig } from "./config/app.config";
|
||||||
import { HttpExceptionFilter } from "./filters/http-exception.filter";
|
import { HttpExceptionFilter } from "./filters/http-exception.filter";
|
||||||
import { LoggingInterceptor } from "./interceptors/logging.interceptor";
|
import { LoggingInterceptor } from "./interceptors/logging.interceptor";
|
||||||
@@ -17,7 +17,7 @@ import { TraceInterceptor } from "./interceptors/trace.interceptor";
|
|||||||
// Guard against uncaught exceptions thrown inside Playwright event listener
|
// Guard against uncaught exceptions thrown inside Playwright event listener
|
||||||
// callbacks (e.g. a bad waitForURL predicate). Without this, Node.js v15+
|
// callbacks (e.g. a bad waitForURL predicate). Without this, Node.js v15+
|
||||||
// crashes the process on any unhandled rejection or uncaught exception.
|
// crashes the process on any unhandled rejection or uncaught exception.
|
||||||
const processLogger = new TraceLogger("Process");
|
const processLogger = createAppLogger("Process");
|
||||||
process.on("uncaughtException", (err) => {
|
process.on("uncaughtException", (err) => {
|
||||||
processLogger.error(
|
processLogger.error(
|
||||||
`Uncaught exception (process kept alive): ${err.message}`,
|
`Uncaught exception (process kept alive): ${err.message}`,
|
||||||
@@ -35,9 +35,9 @@ process.on("unhandledRejection", (reason) => {
|
|||||||
async function bootstrap() {
|
async function bootstrap() {
|
||||||
const { name: pkgName, version: pkgVersion } = appPackage();
|
const { name: pkgName, version: pkgVersion } = appPackage();
|
||||||
const API_PREFIX = "api/v1";
|
const API_PREFIX = "api/v1";
|
||||||
const logger = new TraceLogger("Bootstrap");
|
const logger = createAppLogger("Bootstrap");
|
||||||
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
|
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
|
||||||
logger: new TraceLogger("Bootstrap", { timestamp: true }),
|
logger: createAppLogger("Bootstrap", { timestamp: true }),
|
||||||
});
|
});
|
||||||
|
|
||||||
// The schema is managed by migrations; refuse to run against an outdated one.
|
// The schema is managed by migrations; refuse to run against an outdated one.
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ import {
|
|||||||
} from "../code-executor/code-executor.service";
|
} from "../code-executor/code-executor.service";
|
||||||
import { ExecContextBuilder } from "../code-executor/exec-context.builder";
|
import { ExecContextBuilder } from "../code-executor/exec-context.builder";
|
||||||
import { traceStorage } from "../common/trace-context";
|
import { traceStorage } from "../common/trace-context";
|
||||||
import { TraceLogger } from "../common/trace-logger";
|
import { createAppLogger } from "../common/app-logger";
|
||||||
import {
|
import {
|
||||||
EnvironmentData,
|
EnvironmentData,
|
||||||
EnvironmentEntity,
|
EnvironmentEntity,
|
||||||
@@ -42,7 +42,7 @@ interface BrowserHandle {
|
|||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ScenarioSchedulerService implements OnModuleInit {
|
export class ScenarioSchedulerService implements OnModuleInit {
|
||||||
private readonly logger = new TraceLogger(ScenarioSchedulerService.name);
|
private readonly logger = createAppLogger(ScenarioSchedulerService.name);
|
||||||
private readonly activeRuns = new Set<string>();
|
private readonly activeRuns = new Set<string>();
|
||||||
private readonly runBrowsers = new Map<string, BrowserHandle>();
|
private readonly runBrowsers = new Map<string, BrowserHandle>();
|
||||||
// Cache credential maps per run (built once when a run starts)
|
// Cache credential maps per run (built once when a run starts)
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import {
|
|||||||
OnModuleDestroy,
|
OnModuleDestroy,
|
||||||
} from "@nestjs/common";
|
} from "@nestjs/common";
|
||||||
import type { Browser, BrowserContext, Page } from "playwright";
|
import type { Browser, BrowserContext, Page } from "playwright";
|
||||||
import { TraceLogger } from "../common/trace-logger";
|
import { createAppLogger } from "../common/app-logger";
|
||||||
import { SessionService } from "./session.service";
|
import { SessionService } from "./session.service";
|
||||||
|
|
||||||
export interface SessionHandle {
|
export interface SessionHandle {
|
||||||
@@ -21,7 +21,7 @@ export interface SessionHandle {
|
|||||||
*/
|
*/
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SessionContextService implements OnModuleDestroy {
|
export class SessionContextService implements OnModuleDestroy {
|
||||||
private readonly logger = new TraceLogger(SessionContextService.name);
|
private readonly logger = createAppLogger(SessionContextService.name);
|
||||||
private readonly handles = new Map<string, SessionHandle>();
|
private readonly handles = new Map<string, SessionHandle>();
|
||||||
|
|
||||||
constructor(private readonly sessionService: SessionService) {}
|
constructor(private readonly sessionService: SessionService) {}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { Injectable } from "@nestjs/common";
|
import { Injectable } from "@nestjs/common";
|
||||||
import { ConfigService } from "@nestjs/config";
|
import { ConfigService } from "@nestjs/config";
|
||||||
import { Interval } from "@nestjs/schedule";
|
import { Interval } from "@nestjs/schedule";
|
||||||
import { TraceLogger } from "../common/trace-logger";
|
import { createAppLogger } from "../common/app-logger";
|
||||||
import { AppConfig } from "../config/app.config";
|
import { AppConfig } from "../config/app.config";
|
||||||
import { SessionContextService } from "./session-context.service";
|
import { SessionContextService } from "./session-context.service";
|
||||||
import { SessionService } from "./session.service";
|
import { SessionService } from "./session.service";
|
||||||
@@ -13,7 +13,7 @@ import { SessionService } from "./session.service";
|
|||||||
*/
|
*/
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SessionSchedulerService {
|
export class SessionSchedulerService {
|
||||||
private readonly logger = new TraceLogger(SessionSchedulerService.name);
|
private readonly logger = createAppLogger(SessionSchedulerService.name);
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly sessionService: SessionService,
|
private readonly sessionService: SessionService,
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import {
|
|||||||
PaginatedResult,
|
PaginatedResult,
|
||||||
PaginationQueryDto,
|
PaginationQueryDto,
|
||||||
} from "../common/dto/pagination.dto";
|
} from "../common/dto/pagination.dto";
|
||||||
import { TraceLogger } from "../common/trace-logger";
|
import { createAppLogger } from "../common/app-logger";
|
||||||
import { SessionEntity } from "./session.entity";
|
import { SessionEntity } from "./session.entity";
|
||||||
|
|
||||||
export type SessionOrderBy =
|
export type SessionOrderBy =
|
||||||
@@ -19,7 +19,7 @@ export type SessionOrderBy =
|
|||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SessionService implements OnApplicationBootstrap {
|
export class SessionService implements OnApplicationBootstrap {
|
||||||
private readonly logger = new TraceLogger(SessionService.name);
|
private readonly logger = createAppLogger(SessionService.name);
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@InjectRepository(SessionEntity)
|
@InjectRepository(SessionEntity)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import "reflect-metadata";
|
import "reflect-metadata";
|
||||||
import { validateAppConfig } from "../src/config/app.config";
|
import { LogMode, validateAppConfig } from "../src/config/app.config";
|
||||||
import { durationSeconds } from "../src/common/interval";
|
import { durationSeconds } from "../src/common/interval";
|
||||||
|
|
||||||
describe("Authentication configuration", () => {
|
describe("Authentication configuration", () => {
|
||||||
@@ -12,6 +12,17 @@ describe("Authentication configuration", () => {
|
|||||||
expect(config.AUTH_REVOKED_TOKEN_CLEANUP_CRON).toBe("0 3 * * *");
|
expect(config.AUTH_REVOKED_TOKEN_CLEANUP_CRON).toBe("0 3 * * *");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("defaults to console logging and accepts debug logging", () => {
|
||||||
|
expect(validateAppConfig({}).LOG_MODE).toBe(LogMode.Console);
|
||||||
|
expect(validateAppConfig({ LOG_MODE: "debug" }).LOG_MODE).toBe(
|
||||||
|
LogMode.Debug,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects unsupported logging modes", () => {
|
||||||
|
expect(() => validateAppConfig({ LOG_MODE: "silent" })).toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
it("parses explicit true and false values without JavaScript truthiness", () => {
|
it("parses explicit true and false values without JavaScript truthiness", () => {
|
||||||
expect(validateAppConfig({ AUTH_ENABLED: "false" }).AUTH_ENABLED).toBe(
|
expect(validateAppConfig({ AUTH_ENABLED: "false" }).AUTH_ENABLED).toBe(
|
||||||
false,
|
false,
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { CallHandler, ExecutionContext } from "@nestjs/common";
|
import { CallHandler, ExecutionContext } from "@nestjs/common";
|
||||||
import { of } from "rxjs";
|
import { of } from "rxjs";
|
||||||
import { TraceLogger } from "../src/common/trace-logger";
|
import { DebugLogger } from "../src/common/debug-logger";
|
||||||
import {
|
import {
|
||||||
LoggingInterceptor,
|
LoggingInterceptor,
|
||||||
redactAuthRequestBody,
|
redactAuthRequestBody,
|
||||||
@@ -31,7 +31,7 @@ describe("redactAuthRequestBody", () => {
|
|||||||
it("never logs a password received on the accepted trailing-slash login path", () => {
|
it("never logs a password received on the accepted trailing-slash login path", () => {
|
||||||
const loggedMessages: unknown[][] = [];
|
const loggedMessages: unknown[][] = [];
|
||||||
const debug = jest
|
const debug = jest
|
||||||
.spyOn(TraceLogger.prototype, "debug")
|
.spyOn(DebugLogger.prototype, "debug")
|
||||||
.mockImplementation((...args: unknown[]) => {
|
.mockImplementation((...args: unknown[]) => {
|
||||||
loggedMessages.push(args);
|
loggedMessages.push(args);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
// Application logs should stay quiet during tests unless DEBUG enables them.
|
||||||
|
process.env.LOG_MODE = 'debug';
|
||||||
@@ -0,0 +1,107 @@
|
|||||||
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
||||||
|
import createDebug = require("debug");
|
||||||
|
import { createAppLogger } from "../src/common/app-logger";
|
||||||
|
import { DebugLogger } from "../src/common/debug-logger";
|
||||||
|
import { TraceLogger } from "../src/common/trace-logger";
|
||||||
|
import { traceStorage } from "../src/common/trace-context";
|
||||||
|
import { LogMode } from "../src/config/app.config";
|
||||||
|
|
||||||
|
describe("application logger selection and output", () => {
|
||||||
|
const originalLogMode = process.env.LOG_MODE;
|
||||||
|
const originalNodeEnv = process.env.NODE_ENV;
|
||||||
|
const originalDebug = createDebug.disable();
|
||||||
|
|
||||||
|
let stdout: jest.SpyInstance;
|
||||||
|
let stderr: jest.SpyInstance;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
stdout = jest.spyOn(process.stdout, "write").mockImplementation(() => true);
|
||||||
|
stderr = jest.spyOn(process.stderr, "write").mockImplementation(() => true);
|
||||||
|
createDebug.disable();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
stdout.mockRestore();
|
||||||
|
stderr.mockRestore();
|
||||||
|
createDebug.disable();
|
||||||
|
if (originalDebug) createDebug.enable(originalDebug);
|
||||||
|
if (originalLogMode === undefined) delete process.env.LOG_MODE;
|
||||||
|
else process.env.LOG_MODE = originalLogMode;
|
||||||
|
if (originalNodeEnv === undefined) delete process.env.NODE_ENV;
|
||||||
|
else process.env.NODE_ENV = originalNodeEnv;
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps console output as the default", () => {
|
||||||
|
delete process.env.LOG_MODE;
|
||||||
|
expect(createAppLogger("Runtime")).toBeInstanceOf(TraceLogger);
|
||||||
|
expect(
|
||||||
|
createAppLogger("Runtime").log("visible by default"),
|
||||||
|
).toBeUndefined();
|
||||||
|
|
||||||
|
expect(stdout).toHaveBeenCalled();
|
||||||
|
expect(stderr).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps TraceLogger on console regardless of LOG_MODE", () => {
|
||||||
|
process.env.LOG_MODE = LogMode.Debug;
|
||||||
|
new TraceLogger("Runtime").log("trace logger stays on console");
|
||||||
|
|
||||||
|
expect(stdout).toHaveBeenCalled();
|
||||||
|
expect(stderr).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("preserves timestamp, level and trace context in console output", () => {
|
||||||
|
const before = Date.now();
|
||||||
|
traceStorage.run({ traceId: "trace-console" }, () => {
|
||||||
|
new TraceLogger("Runtime", { colors: false }).log("legacy format");
|
||||||
|
});
|
||||||
|
const output = stdout.mock.calls.flat().join("");
|
||||||
|
const timestamp = output.match(/\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{3}Z/);
|
||||||
|
|
||||||
|
expect(timestamp).not.toBeNull();
|
||||||
|
expect(Date.parse(timestamp![0])).toBeGreaterThanOrEqual(before);
|
||||||
|
expect(output).toContain(" LOG [Runtime:trace-console] legacy format");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("writes debug-mode output only when its namespace is enabled", () => {
|
||||||
|
process.env.LOG_MODE = LogMode.Debug;
|
||||||
|
process.env.NODE_ENV = "production";
|
||||||
|
createDebug.enable("liqa:other");
|
||||||
|
|
||||||
|
expect(createAppLogger("Runtime")).toBeInstanceOf(DebugLogger);
|
||||||
|
createAppLogger("Runtime").warn("quiet without matching namespace");
|
||||||
|
|
||||||
|
expect(stdout).not.toHaveBeenCalled();
|
||||||
|
expect(stderr).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("routes matching debug output with level, context, trace id and error detail", () => {
|
||||||
|
process.env.LOG_MODE = LogMode.Debug;
|
||||||
|
process.env.NODE_ENV = "production";
|
||||||
|
createDebug.enable("liqa:runtime");
|
||||||
|
|
||||||
|
traceStorage.run({ traceId: "trace-123" }, () => {
|
||||||
|
createAppLogger("Runtime").error(
|
||||||
|
"request failed",
|
||||||
|
"synthetic stack\n at test()",
|
||||||
|
"Runtime",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
const output = stderr.mock.calls.flat().join("");
|
||||||
|
expect(stdout).not.toHaveBeenCalled();
|
||||||
|
expect(output).toContain("[ERROR] [Runtime:trace-123] request failed");
|
||||||
|
expect(output).toContain("synthetic stack");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("preserves DEBUG=test for test-process logging", () => {
|
||||||
|
process.env.LOG_MODE = LogMode.Debug;
|
||||||
|
process.env.NODE_ENV = "test";
|
||||||
|
createDebug.enable("test");
|
||||||
|
|
||||||
|
createAppLogger("Unit").debug("test diagnostic");
|
||||||
|
|
||||||
|
expect(stdout).not.toHaveBeenCalled();
|
||||||
|
expect(stderr.mock.calls.flat().join("")).toContain("test diagnostic");
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user