Skip to content

Commit

Permalink
Merge pull request #470 from jvalue/refactor-logger
Browse files Browse the repository at this point in the history
Refactor CachedLogger
  • Loading branch information
georg-schwarz authored Nov 15, 2023
2 parents f91d37e + e94fad7 commit e6bad7c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 80 deletions.
23 changes: 13 additions & 10 deletions libs/execution/src/lib/logging/cached-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,31 @@ 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,
) {
super(enableDebugLogging, loggingContext, depth);
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<LogEntry> {
const filterLevels =
logLevel.length === 0 ? Object.values(DiagnosticSeverity) : logLevel;
return this.logCache
.getLogs()
.filter((log) => filterLevels.includes(log.severity));
}

public clearLogs(): void {
Expand Down
34 changes: 3 additions & 31 deletions libs/execution/src/lib/logging/log-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<LogEntry> {
return this.logs;
}

public clearLogs(): void {
Expand Down
5 changes: 5 additions & 0 deletions libs/execution/src/lib/logging/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
72 changes: 33 additions & 39 deletions libs/interpreter-lib/src/parsing-util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 () => {
Expand All @@ -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);
}
});

Expand All @@ -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);
}
});
});
Expand All @@ -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 () => {
Expand All @@ -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);
}
});

Expand All @@ -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);
}
});
});
Expand Down

0 comments on commit e6bad7c

Please sign in to comment.