From 4c534e9d5dff10417591c7794898c4e9525307e3 Mon Sep 17 00:00:00 2001 From: Ruben Taelman Date: Wed, 23 Sep 2020 09:18:16 +0200 Subject: [PATCH] change: add global setGlobalLoggerFactory function --- src/init/Setup.ts | 5 ++--- src/logging/LogUtil.ts | 13 +++++++++++-- test/unit/logging/LogUtil.test.ts | 17 +++++++++++++++-- test/unit/server/ExpressHttpServer.test.ts | 4 ++-- 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/init/Setup.ts b/src/init/Setup.ts index 0c531a145..91b5e26f1 100644 --- a/src/init/Setup.ts +++ b/src/init/Setup.ts @@ -1,9 +1,8 @@ import streamifyArray from 'streamify-array'; import type { AclManager } from '../authorization/AclManager'; import { RepresentationMetadata } from '../ldp/representation/RepresentationMetadata'; -import { LazyLoggerFactory } from '../logging/LazyLoggerFactory'; import type { LoggerFactory } from '../logging/LoggerFactory'; -import { getLoggerFor } from '../logging/LogUtil'; +import { getLoggerFor, setGlobalLoggerFactory } from '../logging/LogUtil'; import type { ExpressHttpServer } from '../server/ExpressHttpServer'; import type { ResourceStore } from '../storage/ResourceStore'; import { TEXT_TURTLE } from '../util/ContentTypes'; @@ -42,7 +41,7 @@ export class Setup { */ public async setup(): Promise { // Configure the logger factory so that others can statically call it. - LazyLoggerFactory.getInstance().setLoggerFactory(this.loggerFactory); + setGlobalLoggerFactory(this.loggerFactory); // Set up acl so everything can still be done by default // Note that this will need to be adapted to go through all the correct channels later on diff --git a/src/logging/LogUtil.ts b/src/logging/LogUtil.ts index 75f374f51..d990387c9 100644 --- a/src/logging/LogUtil.ts +++ b/src/logging/LogUtil.ts @@ -1,5 +1,6 @@ import { LazyLoggerFactory } from './LazyLoggerFactory'; import type { Logger } from './Logger'; +import type { LoggerFactory } from './LoggerFactory'; /** * Gets a logger instance for the given class instance. @@ -19,8 +20,16 @@ import type { Logger } from './Logger'; * * @param loggable - A class instance or a class string name. */ -export const getLoggerFor = (loggable: string | Instance): Logger => LazyLoggerFactory.getInstance() - .createLogger(typeof loggable === 'string' ? loggable : loggable.constructor.name); +export const getLoggerFor = (loggable: string | Instance): Logger => LazyLoggerFactory + .getInstance().createLogger(typeof loggable === 'string' ? loggable : loggable.constructor.name); + +/** + * Sets the global logger factory. + * This will cause all loggers created by {@link getLoggerFor} to be delegated to a logger from the given factory. + * @param loggerFactory - A logger factory, of undefined for unsetting it. + */ +export const setGlobalLoggerFactory = (loggerFactory: LoggerFactory | undefined): void => LazyLoggerFactory + .getInstance().setLoggerFactory(loggerFactory); /** * Helper interface to identify class instances. diff --git a/test/unit/logging/LogUtil.test.ts b/test/unit/logging/LogUtil.test.ts index 5cdfe0192..6322a0d83 100644 --- a/test/unit/logging/LogUtil.test.ts +++ b/test/unit/logging/LogUtil.test.ts @@ -1,11 +1,12 @@ import { LazyLogger } from '../../../src/logging/LazyLogger'; import { LazyLoggerFactory } from '../../../src/logging/LazyLoggerFactory'; -import { getLoggerFor } from '../../../src/logging/LogUtil'; +import { getLoggerFor, setGlobalLoggerFactory } from '../../../src/logging/LogUtil'; import { VoidLogger } from '../../../src/logging/VoidLogger'; +import { VoidLoggerFactory } from '../../../src/logging/VoidLoggerFactory'; describe('LogUtil', (): void => { beforeEach(async(): Promise => { - LazyLoggerFactory.getInstance().setLoggerFactory(undefined); + setGlobalLoggerFactory(undefined); }); it('allows creating a lazy logger for a string label.', async(): Promise => { @@ -17,4 +18,16 @@ describe('LogUtil', (): void => { expect(getLoggerFor(new VoidLogger())).toBeInstanceOf(LazyLogger); expect((getLoggerFor(new VoidLogger()) as any).label).toEqual('VoidLogger'); }); + + it('allows setting the global logger factory.', async(): Promise => { + expect(setGlobalLoggerFactory(new VoidLoggerFactory())); + expect(LazyLoggerFactory.getInstance().getLoggerFactoryOrThrow()).toBeInstanceOf(VoidLoggerFactory); + }); + + it('allows unsetting the global logger factory.', async(): Promise => { + expect(setGlobalLoggerFactory(new VoidLoggerFactory())); + expect(setGlobalLoggerFactory(undefined)); + expect((): any => LazyLoggerFactory.getInstance().getLoggerFactoryOrThrow()) + .toThrow(new Error('Illegal logging during initialization')); + }); }); diff --git a/test/unit/server/ExpressHttpServer.test.ts b/test/unit/server/ExpressHttpServer.test.ts index 019406ba7..8334b6d68 100644 --- a/test/unit/server/ExpressHttpServer.test.ts +++ b/test/unit/server/ExpressHttpServer.test.ts @@ -1,6 +1,6 @@ import type { Server } from 'http'; import request from 'supertest'; -import { LazyLoggerFactory } from '../../../src/logging/LazyLoggerFactory'; +import { setGlobalLoggerFactory } from '../../../src/logging/LogUtil'; import { VoidLoggerFactory } from '../../../src/logging/VoidLoggerFactory'; import { ExpressHttpServer } from '../../../src/server/ExpressHttpServer'; import { HttpHandler } from '../../../src/server/HttpHandler'; @@ -33,7 +33,7 @@ describe('ExpressHttpServer', (): void => { beforeAll(async(): Promise => { // Prevent test from writing to stderr mock = jest.spyOn(process.stderr, 'write').mockImplementation((): boolean => true); - LazyLoggerFactory.getInstance().setLoggerFactory(new VoidLoggerFactory()); + setGlobalLoggerFactory(new VoidLoggerFactory()); }); beforeEach(async(): Promise => {