Skip to content

Commit

Permalink
fix: order of log output in cli (#644)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnWeber committed Feb 27, 2024
1 parent 8379fb8 commit a30dd25
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 31 deletions.
4 changes: 3 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"args": [
"send",
"${file}",
"--all"
"--all",
"-o",
"short"
],
"outFiles": [
"${workspaceFolder}/dist/**/*.js"
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 5 additions & 1 deletion src/cli/send/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
63 changes: 38 additions & 25 deletions src/io/logger.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<string>) {
this.writeLog(LogLevel.info, this.priorityCache, console.info, msg);
}
public clear(): void {
console.clear();
}
}
Expand Down
1 change: 1 addition & 0 deletions src/models/logHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit a30dd25

Please sign in to comment.