From e94fad7d6d592f20489f30a013fb2f487261a58c Mon Sep 17 00:00:00 2001 From: Georg Schwarz Date: Tue, 14 Nov 2023 15:48:21 +0100 Subject: [PATCH] Return stream of logs instead of separate streams in CachedLogger --- .../src/lib/logging/cached-logger.ts | 23 +++--- libs/execution/src/lib/logging/log-cache.ts | 34 +-------- libs/execution/src/lib/logging/logger.ts | 5 ++ libs/interpreter-lib/src/parsing-util.spec.ts | 72 +++++++++---------- 4 files changed, 54 insertions(+), 80 deletions(-) diff --git a/libs/execution/src/lib/logging/cached-logger.ts b/libs/execution/src/lib/logging/cached-logger.ts index 7fc4633d8..bcdb7c555 100644 --- a/libs/execution/src/lib/logging/cached-logger.ts +++ b/libs/execution/src/lib/logging/cached-logger.ts @@ -8,14 +8,14 @@ import { Range } from 'vscode-languageserver'; import { DefaultLogger } from './default-logger'; import { LogCache } from './log-cache'; -import { DiagnosticSeverity } from './logger'; +import { DiagnosticSeverity, LogEntry } from './logger'; export class CachedLogger extends DefaultLogger { - private logCache: LogCache; + protected logCache: LogCache; constructor( enableDebugLogging: boolean, loggingContext?: string, - private printLogs: boolean = true, + protected printLogs: boolean = true, depth = 0, cacheSize = 200, ) { @@ -23,13 +23,16 @@ export class CachedLogger extends DefaultLogger { this.logCache = new LogCache(cacheSize); } - public getLogs() { - return this.logCache.getLogsFilteredBySeverity([ - DiagnosticSeverity.ERROR, - DiagnosticSeverity.HINT, - DiagnosticSeverity.INFO, - DiagnosticSeverity.WARNING, - ]); + /** + * Gets all log entries with the specified log levels. + * If no log level given, returns logs on all log levels. + */ + public getLogs(...logLevel: DiagnosticSeverity[]): ReadonlyArray { + const filterLevels = + logLevel.length === 0 ? Object.values(DiagnosticSeverity) : logLevel; + return this.logCache + .getLogs() + .filter((log) => filterLevels.includes(log.severity)); } public clearLogs(): void { diff --git a/libs/execution/src/lib/logging/log-cache.ts b/libs/execution/src/lib/logging/log-cache.ts index 079a57718..2586c2e38 100644 --- a/libs/execution/src/lib/logging/log-cache.ts +++ b/libs/execution/src/lib/logging/log-cache.ts @@ -2,12 +2,7 @@ // // SPDX-License-Identifier: AGPL-3.0-only -import { DiagnosticSeverity } from './logger'; - -interface LogEntry { - severity: DiagnosticSeverity; - message: string; -} +import { DiagnosticSeverity, LogEntry } from './logger'; export class LogCache { private logCacheMaxSize: number; @@ -35,31 +30,8 @@ export class LogCache { return this.logs.map((e) => e.message); } - public getLogsFilteredBySeverity( - severity: DiagnosticSeverity | DiagnosticSeverity[], - ): { - [key in DiagnosticSeverity]: string[]; - } { - const ret = { - [DiagnosticSeverity.ERROR]: [] as string[], - [DiagnosticSeverity.HINT]: [] as string[], - [DiagnosticSeverity.INFO]: [] as string[], - [DiagnosticSeverity.WARNING]: [] as string[], - [DiagnosticSeverity.DEBUG]: [] as string[], - }; - const severities: DiagnosticSeverity[] = []; - if (Array.isArray(severity)) { - severities.push(...severity); - } else { - severities.push(severity); - } - for (const s of severities) { - ret[s] = this.logs - .filter((log) => log.severity === s) - .map((s) => s.message); - } - - return ret; + public getLogs(): ReadonlyArray { + return this.logs; } public clearLogs(): void { diff --git a/libs/execution/src/lib/logging/logger.ts b/libs/execution/src/lib/logging/logger.ts index 4bf4ff65f..5377e4a41 100644 --- a/libs/execution/src/lib/logging/logger.ts +++ b/libs/execution/src/lib/logging/logger.ts @@ -21,6 +21,11 @@ export enum DiagnosticSeverity { DEBUG = 'debug', } +export interface LogEntry { + severity: DiagnosticSeverity; + message: string; +} + export abstract class Logger { abstract setLoggingContext(loggingContext: string | undefined): void; abstract setLoggingDepth(depth: number): void; diff --git a/libs/interpreter-lib/src/parsing-util.spec.ts b/libs/interpreter-lib/src/parsing-util.spec.ts index 6bd157b0c..afe586574 100644 --- a/libs/interpreter-lib/src/parsing-util.spec.ts +++ b/libs/interpreter-lib/src/parsing-util.spec.ts @@ -4,7 +4,7 @@ import * as path from 'path'; -import { CachedLogger } from '@jvalue/jayvee-execution'; +import { CachedLogger, DiagnosticSeverity } from '@jvalue/jayvee-execution'; import { JayveeServices, createJayveeServices, @@ -68,12 +68,11 @@ describe('Validation of parsing-util', () => { await parseAndValidateDocument(text); - expect(logger.getLogs().error.length).toEqual(0); - expect(logger.getLogs().info).toHaveLength(0); - expect(logger.getLogs().error).toHaveLength(0); - expect(logger.getLogs().debug).toHaveLength(0); - expect(logger.getLogs().hint).toHaveLength(0); - expect(logger.getLogs().warning).toHaveLength(0); + expect(logger.getLogs(DiagnosticSeverity.ERROR)).toHaveLength(0); + expect(logger.getLogs(DiagnosticSeverity.INFO)).toHaveLength(0); + expect(logger.getLogs(DiagnosticSeverity.DEBUG)).toHaveLength(0); + expect(logger.getLogs(DiagnosticSeverity.HINT)).toHaveLength(0); + expect(logger.getLogs(DiagnosticSeverity.WARNING)).toHaveLength(0); }); it('should diagnose error on wrong loader type', async () => { @@ -84,12 +83,11 @@ describe('Validation of parsing-util', () => { try { await parseAndValidateDocument(text); } catch (e) { - expect(logger.getLogs().error.length).toBeGreaterThanOrEqual(1); - expect(logger.getLogs().info).toHaveLength(0); - expect(logger.getLogs().error).toHaveLength(2 * 5); // 2 calls that get formated to 5 lines each - expect(logger.getLogs().debug).toHaveLength(0); - expect(logger.getLogs().hint).toHaveLength(0); - expect(logger.getLogs().warning).toHaveLength(0); + expect(logger.getLogs(DiagnosticSeverity.INFO)).toHaveLength(0); + expect(logger.getLogs(DiagnosticSeverity.ERROR)).toHaveLength(2 * 5); // 2 calls that get formated to 5 lines each + expect(logger.getLogs(DiagnosticSeverity.DEBUG)).toHaveLength(0); + expect(logger.getLogs(DiagnosticSeverity.HINT)).toHaveLength(0); + expect(logger.getLogs(DiagnosticSeverity.WARNING)).toHaveLength(0); } }); @@ -101,12 +99,11 @@ describe('Validation of parsing-util', () => { try { await parseAndValidateDocument(text); } catch (e) { - expect(logger.getLogs().error.length).toEqual(1); - expect(logger.getLogs().info).toHaveLength(0); - expect(logger.getLogs().error).toHaveLength(1); - expect(logger.getLogs().debug).toHaveLength(0); - expect(logger.getLogs().hint).toHaveLength(0); - expect(logger.getLogs().warning).toHaveLength(0); + expect(logger.getLogs(DiagnosticSeverity.INFO)).toHaveLength(0); + expect(logger.getLogs(DiagnosticSeverity.ERROR)).toHaveLength(1); + expect(logger.getLogs(DiagnosticSeverity.DEBUG)).toHaveLength(0); + expect(logger.getLogs(DiagnosticSeverity.HINT)).toHaveLength(0); + expect(logger.getLogs(DiagnosticSeverity.WARNING)).toHaveLength(0); } }); }); @@ -123,12 +120,11 @@ describe('Validation of parsing-util', () => { logger, ); - expect(logger.getLogs().error.length).toEqual(0); - expect(logger.getLogs().info).toHaveLength(0); - expect(logger.getLogs().error).toHaveLength(0); - expect(logger.getLogs().debug).toHaveLength(0); - expect(logger.getLogs().hint).toHaveLength(0); - expect(logger.getLogs().warning).toHaveLength(0); + expect(logger.getLogs(DiagnosticSeverity.INFO)).toHaveLength(0); + expect(logger.getLogs(DiagnosticSeverity.ERROR)).toHaveLength(0); + expect(logger.getLogs(DiagnosticSeverity.DEBUG)).toHaveLength(0); + expect(logger.getLogs(DiagnosticSeverity.HINT)).toHaveLength(0); + expect(logger.getLogs(DiagnosticSeverity.WARNING)).toHaveLength(0); }); it('should diagnose error on invalid extension', async () => { @@ -143,17 +139,16 @@ describe('Validation of parsing-util', () => { logger, ); } catch (e) { - expect(logger.getLogs().error.length).toEqual(1); - expect(logger.getLogs().info).toHaveLength(0); - expect(logger.getLogs().error).toHaveLength(1); - expect(logger.getLogs().error[0]).toEqual( + expect(logger.getLogs(DiagnosticSeverity.INFO)).toHaveLength(0); + expect(logger.getLogs(DiagnosticSeverity.ERROR)).toHaveLength(1); + expect(logger.getLogs(DiagnosticSeverity.ERROR)[0]?.message).toEqual( expect.stringContaining( 'Please choose a file with this extension: ".jv"', ), ); - expect(logger.getLogs().debug).toHaveLength(0); - expect(logger.getLogs().hint).toHaveLength(0); - expect(logger.getLogs().warning).toHaveLength(0); + expect(logger.getLogs(DiagnosticSeverity.DEBUG)).toHaveLength(0); + expect(logger.getLogs(DiagnosticSeverity.HINT)).toHaveLength(0); + expect(logger.getLogs(DiagnosticSeverity.WARNING)).toHaveLength(0); } }); @@ -169,16 +164,15 @@ describe('Validation of parsing-util', () => { logger, ); } catch (e) { - expect(logger.getLogs().error.length).toEqual(1); - expect(logger.getLogs().info).toHaveLength(0); - expect(logger.getLogs().error).toHaveLength(1); - expect(logger.getLogs().error[0]).toMatch( + expect(logger.getLogs(DiagnosticSeverity.INFO)).toHaveLength(0); + expect(logger.getLogs(DiagnosticSeverity.ERROR)).toHaveLength(1); + expect(logger.getLogs(DiagnosticSeverity.ERROR)[0]?.message).toMatch( // eslint-disable-next-line no-useless-escape /File [\w\-\/]*\/libs\/interpreter-lib\/test\/assets\/parsing-util\/extractDocumentFromFile\/invalid-missing-file\.jv does not exist\./, ); - expect(logger.getLogs().debug).toHaveLength(0); - expect(logger.getLogs().hint).toHaveLength(0); - expect(logger.getLogs().warning).toHaveLength(0); + expect(logger.getLogs(DiagnosticSeverity.DEBUG)).toHaveLength(0); + expect(logger.getLogs(DiagnosticSeverity.HINT)).toHaveLength(0); + expect(logger.getLogs(DiagnosticSeverity.WARNING)).toHaveLength(0); } }); });