From a30dd252cc82481807dfb52741e73fbe8d95289c Mon Sep 17 00:00:00 2001 From: Andreas Weber Date: Tue, 27 Feb 2024 19:48:02 +0100 Subject: [PATCH] fix: order of log output in cli (#644) --- .vscode/launch.json | 4 ++- CHANGELOG.md | 3 +- package-lock.json | 4 +-- package.json | 2 +- src/cli/send/send.ts | 6 +++- src/io/logger.ts | 63 ++++++++++++++++++++++++---------------- src/models/logHandler.ts | 1 + 7 files changed, 52 insertions(+), 31 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 9a161889..114da044 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -14,7 +14,9 @@ "args": [ "send", "${file}", - "--all" + "--all", + "-o", + "short" ], "outFiles": [ "${workspaceFolder}/dist/**/*.js" diff --git a/CHANGELOG.md b/CHANGELOG.md index ad85ea9b..7b9c2d55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,8 @@ -## [unreleased] +## [6.11.5] (2023-02-27) ### Fix - replace grpc-reflection-js with grpc-js-reflection-client +- fix order of log output in cli (#644) ## [6.11.4] (2023-02-25) diff --git a/package-lock.json b/package-lock.json index ed65c06f..a24ec72b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "httpyac", - "version": "6.11.4", + "version": "6.11.5", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "httpyac", - "version": "6.11.4", + "version": "6.11.5", "license": "MIT", "dependencies": { "@cloudamqp/amqp-client": "^2.1.1", diff --git a/package.json b/package.json index 807a149d..556c0725 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "license": "MIT", "publisher": "AnWeber", "description": "HTTP/REST CLI Client for *.http files", - "version": "6.11.4", + "version": "6.11.5", "homepage": "https://github.com/AnWeber/httpyac", "repository": { "type": "git", diff --git a/src/cli/send/send.ts b/src/cli/send/send.ts index 672e2a12..5847220e 100644 --- a/src/cli/send/send.ts +++ b/src/cli/send/send.ts @@ -286,7 +286,11 @@ function getRequestLogger( ); if (requestLoggerOptions || requestFailedLoggerOptions) { - return utils.requestLoggerFactory(args => logger.info(args), requestLoggerOptions, requestFailedLoggerOptions); + return utils.requestLoggerFactory( + args => logger.logPriority(args), + requestLoggerOptions, + requestFailedLoggerOptions + ); } return undefined; } diff --git a/src/io/logger.ts b/src/io/logger.ts index a3179a8e..32f90f95 100644 --- a/src/io/logger.ts +++ b/src/io/logger.ts @@ -1,7 +1,10 @@ import { ConsoleLogHandler, LogHandler, LogLevel } from '../models'; +type LogCache = Array<() => void> | undefined; + export class Logger implements ConsoleLogHandler { - private collectCache: Array<() => void> | undefined; + private collectCache: LogCache; + private priorityCache: LogCache; constructor( readonly options: { level?: LogLevel; @@ -11,59 +14,69 @@ export class Logger implements ConsoleLogHandler { } ) {} - collectMessages(): void { + public collectMessages(): void { this.collectCache = []; + this.priorityCache = []; + } + + public flush(): void { + this.priorityCache = this.flushCache(this.priorityCache); + this.collectCache = this.flushCache(this.collectCache); } - flush(): void { - if (this.collectCache) { - for (const action of this.collectCache) { + private flushCache(cache: LogCache) { + if (cache) { + for (const action of cache) { action(); } - this.collectCache = []; + return []; } + return undefined; } - private writeLog(logLevel: LogLevel, action: (...params: unknown[]) => void, params: unknown[]) { + private writeLog(logLevel: LogLevel, cache: LogCache, action: (...params: unknown[]) => void, params: unknown[]) { if (!this.options?.level || logLevel >= this.options.level) { let log = () => action(...params); if (this.options?.logMethod) { log = () => this.options?.logMethod?.(logLevel, ...params); } - if (this.collectCache) { - this.collectCache.push(log); + if (cache) { + cache.push(log); } else { log(); } } } - info(...params: unknown[]): void { - this.writeLog(LogLevel.info, console.info, params); + public info(...params: unknown[]): void { + this.writeLog(LogLevel.info, this.collectCache, console.info, params); } - log(...params: unknown[]): void { - this.writeLog(LogLevel.info, console.log, params); + public log(...params: unknown[]): void { + this.writeLog(LogLevel.info, this.collectCache, console.log, params); } - trace(...params: unknown[]): void { - this.writeLog(LogLevel.trace, this.options.noTrace ? console.debug : console.trace, params); + public trace(...params: unknown[]): void { + this.writeLog(LogLevel.trace, this.collectCache, this.options.noTrace ? console.debug : console.trace, params); } - debug(...params: unknown[]): void { - this.writeLog(LogLevel.debug, console.debug, params); + public debug(...params: unknown[]): void { + this.writeLog(LogLevel.debug, this.collectCache, console.debug, params); } - error(...params: unknown[]): void { - this.writeLog(LogLevel.error, console.error, params); + public error(...params: unknown[]): void { + this.writeLog(LogLevel.error, this.collectCache, console.error, params); } - warn(...params: unknown[]): void { - this.writeLog(LogLevel.warn, console.warn, params); + public warn(...params: unknown[]): void { + this.writeLog(LogLevel.warn, this.collectCache, console.warn, params); } - logTest(result: boolean, message: string): void { + public logTest(result: boolean, message: string): void { if (!this.options?.onlyFailedTests && result) { - this.writeLog(LogLevel.info, console.info, [message]); + this.writeLog(LogLevel.info, this.collectCache, console.info, [message]); } else if (!result) { - this.writeLog(LogLevel.info, console.error, [message]); + this.writeLog(LogLevel.info, this.collectCache, console.error, [message]); } } - clear(): void { + public logPriority(...msg: Array) { + this.writeLog(LogLevel.info, this.priorityCache, console.info, msg); + } + public clear(): void { console.clear(); } } diff --git a/src/models/logHandler.ts b/src/models/logHandler.ts index 8d2f4c55..d62b31fe 100644 --- a/src/models/logHandler.ts +++ b/src/models/logHandler.ts @@ -25,6 +25,7 @@ export interface LogHandler { export interface ConsoleLogHandler extends LogHandler { logTest(result: boolean, message: string): void; + logPriority(message: string): void; collectMessages(): void; flush(): void; }