From 61c515a7c8556482946901b264433c088c433fee Mon Sep 17 00:00:00 2001 From: Maksadbek Date: Sat, 23 Nov 2024 23:41:05 +0100 Subject: [PATCH] feat(logger): add TRACE log level --- src/RequestExecutor/HttpRequestExecutor.ts | 14 ++++++++++++++ src/Utils/Logger.ts | 12 +++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/RequestExecutor/HttpRequestExecutor.ts b/src/RequestExecutor/HttpRequestExecutor.ts index b4534fd7..b20287b7 100644 --- a/src/RequestExecutor/HttpRequestExecutor.ts +++ b/src/RequestExecutor/HttpRequestExecutor.ts @@ -103,6 +103,20 @@ export class HttpRequestExecutor implements RequestExecutor { const { res, body } = await this.request(options); + logger.trace( + 'received following response for request %j: headers: %j body: %s', + { + url: options.url, + protocol: this.protocol, + method: options.method + }, + { + statusCode: res.statusCode, + headers: res.headers + }, + body.slice(0, 500).concat(body.length > 500 ? '...' : '') + ); + return new Response({ body, protocol: this.protocol, diff --git a/src/Utils/Logger.ts b/src/Utils/Logger.ts index 1620f075..feee0c7f 100644 --- a/src/Utils/Logger.ts +++ b/src/Utils/Logger.ts @@ -6,7 +6,8 @@ export enum LogLevel { ERROR, WARN, NOTICE, - VERBOSE + VERBOSE, + TRACE } export class Logger { @@ -79,6 +80,14 @@ export class Logger { this.write(message, LogLevel.VERBOSE, ...args); } + public trace(message: string, ...args: any[]): void { + if (this.logLevel < LogLevel.TRACE) { + return; + } + + this.write(message, LogLevel.TRACE, ...args); + } + private write(message: string, level: LogLevel, ...args: any[]): void { const logMessage = `${this.formatHeader(level)} - ${message}`; @@ -105,6 +114,7 @@ export class Logger { case LogLevel.NOTICE: return chalk.green(header); case LogLevel.VERBOSE: + case LogLevel.TRACE: return chalk.cyan(header); } }