Fix logging: Config options for debugging and profiling had no effect.

This commit is contained in:
baldo 2022-07-28 15:07:35 +02:00
parent 13e4895b81
commit 111fac6e47
3 changed files with 84 additions and 12 deletions

View file

@ -1,13 +1,25 @@
import {isLogLevel, LogLevel, LogLevels} from "./types";
import {isLogLevel, isUndefined, LoggingConfig, LogLevel, LogLevels} from "./types";
import {ActivatableLoggerImpl} from "./logger";
function withDefault<T>(value: T | undefined, defaultValue: T): T {
return isUndefined(value) ? defaultValue : value;
}
class TestableLogger extends ActivatableLoggerImpl {
private logs: any[][] = [];
constructor(enabled?: boolean) {
constructor(
enabled?: boolean,
debug?: boolean,
profile?: boolean,
) {
super();
this.init(
enabled === false ? false : true, // default is true
new LoggingConfig(
withDefault(enabled, true),
withDefault(debug, true),
withDefault(profile, true),
),
(...args: any[]): void => this.doLog(...args)
);
}
@ -42,6 +54,7 @@ function parseLogEntry(logEntry: any[]): ParsedLogEntry {
);
}
// noinspection RegExpRedundantEscape
const regexp = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} ([A-Z]+) - (\[[^\]]*\])? *(.*)$/;
const groups = logMessage.match(regexp);
if (groups === null || groups.length < 4) {
@ -187,3 +200,56 @@ for (const level of LogLevels) {
});
}
test(`should not log debug message with disabled debugging`, () => {
// given
const logger = new TestableLogger(true, false, true);
// when
logger.tag("tag").debug("message");
// then
expect(parseLogs(logger.getLogs())).toEqual([]);
});
test(`should log profile message with disabled debugging`, () => {
// given
const logger = new TestableLogger(true, false, true);
// when
logger.tag("tag").profile("message");
// then
expect(parseLogs(logger.getLogs())).toEqual([{
level: "profile",
tags: ["tag"],
message: "message",
args: [],
}]);
});
test(`should not log profile message with disabled profiling`, () => {
// given
const logger = new TestableLogger(true, true, false);
// when
logger.tag("tag").profile("message");
// then
expect(parseLogs(logger.getLogs())).toEqual([]);
});
test(`should log debug message with disabled profiling`, () => {
// given
const logger = new TestableLogger(true, true, false);
// when
logger.tag("tag").debug("message");
// then
expect(parseLogs(logger.getLogs())).toEqual([{
level: "debug",
tags: ["tag"],
message: "message",
args: [],
}]);
});

View file

@ -1,4 +1,4 @@
import {isString, Logger, LogLevel, TaggedLogger} from './types';
import {isString, Logger, LoggingConfig, LogLevel, TaggedLogger} from './types';
import moment from 'moment';
export type LoggingFunction = (...args: any[]) => void;
@ -13,23 +13,25 @@ const noopTaggedLogger: TaggedLogger = {
};
export interface ActivatableLogger extends Logger {
init(enabled: boolean, loggingFunction?: LoggingFunction): void;
init(config: LoggingConfig, loggingFunction?: LoggingFunction): void;
}
/**
* TODO: Check if LoggingConfig.debug and LoggingConfig.profile are handled.
*/
export class ActivatableLoggerImpl implements ActivatableLogger {
private enabled: boolean = false;
private config: LoggingConfig = new LoggingConfig(false, false, false);
private loggingFunction: LoggingFunction = console.info;
init(enabled: boolean, loggingFunction?: LoggingFunction): void {
this.enabled = enabled;
init(config: LoggingConfig, loggingFunction?: LoggingFunction): void {
this.config = config;
this.loggingFunction = loggingFunction || console.info;
}
tag(...tags: string[]): TaggedLogger {
if (this.enabled) {
if (this.config.enabled) {
const debug = this.config.debug;
const profile = this.config.profile;
const loggingFunction = this.loggingFunction;
return {
log(level: LogLevel, ...args: any[]): void {
@ -54,7 +56,9 @@ export class ActivatableLoggerImpl implements ActivatableLogger {
loggingFunction(logStr, ...args);
},
debug(...args: any[]): void {
this.log('debug', ...args);
if (debug) {
this.log('debug', ...args);
}
},
info(...args: any[]): void {
this.log('info', ...args);
@ -66,7 +70,9 @@ export class ActivatableLoggerImpl implements ActivatableLogger {
this.log('error', ...args);
},
profile(...args: any[]): void {
this.log('profile', ...args);
if (profile) {
this.log('profile', ...args);
}
},
}
} else {

View file

@ -8,7 +8,7 @@ import * as app from "./app"
import * as mail from "./mail";
app.init();
Logger.init(config.server.logging.enabled);
Logger.init(config.server.logging);
Logger.tag('main', 'startup').info('Server starting up...');
async function main() {