feat: configure logger during setup

This commit is contained in:
Ruben Taelman
2020-09-22 11:31:22 +02:00
committed by Ruben Taelman
parent 09ac83caa5
commit aaa49219dc
10 changed files with 100 additions and 2 deletions

View File

@@ -3,8 +3,11 @@ import type { ReadStream, WriteStream } from 'tty';
import type { LoaderProperties } from 'componentsjs';
import { Loader } from 'componentsjs';
import yargs from 'yargs';
import { getLoggerFor } from '../logging/LogUtil';
import type { Setup } from './Setup';
const logger = getLoggerFor('CliRunner');
/**
* Generic run function for starting the server from a given config
* @param args - Command line arguments.
@@ -25,6 +28,7 @@ export const runCustom = function(
.options({
port: { type: 'number', alias: 'p', default: 3000 },
config: { type: 'string', alias: 'c' },
level: { type: 'string', alias: 'l', default: 'info' },
})
.help();
@@ -43,12 +47,14 @@ export const runCustom = function(
'urn:solid-server:default:variable:port': argv.port,
'urn:solid-server:default:variable:base': `http://localhost:${argv.port}/`,
'urn:solid-server:default:variable:rootFilePath': process.cwd(),
'urn:solid-server:default:variable:loggingLevel': argv.level,
},
}) as Setup;
return await setup.setup();
})().then((base: string): void => {
stdout.write(`Running at ${base}\n`);
logger.log('info', `Running at ${base}`);
}).catch((error): void => {
// This is the only time we can *not* use the logger to print error messages, as dependency injection has failed.
stderr.write(`${error}\n`);
});
};

View File

@@ -1,6 +1,9 @@
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 type { ExpressHttpServer } from '../server/ExpressHttpServer';
import type { ResourceStore } from '../storage/ResourceStore';
import { TEXT_TURTLE } from '../util/ContentTypes';
@@ -10,9 +13,11 @@ import { CONTENT_TYPE } from '../util/UriConstants';
* Invokes all logic to setup a server.
*/
export class Setup {
protected readonly logger = getLoggerFor(this);
private readonly httpServer: ExpressHttpServer;
private readonly store: ResourceStore;
private readonly aclManager: AclManager;
private readonly loggerFactory: LoggerFactory;
private readonly base: string;
private readonly port: number;
@@ -20,12 +25,14 @@ export class Setup {
httpServer: ExpressHttpServer,
store: ResourceStore,
aclManager: AclManager,
loggerFactory: LoggerFactory,
base: string,
port: number,
) {
this.httpServer = httpServer;
this.store = store;
this.aclManager = aclManager;
this.loggerFactory = loggerFactory;
this.base = base;
this.port = port;
}
@@ -34,6 +41,9 @@ export class Setup {
* Set up a server.
*/
public async setup(): Promise<string> {
// Configure the logger factory so that others can statically call it.
LazyLoggerFactory.getInstance().setLoggerFactory(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
const aclSetup = async(): Promise<void> => {
@@ -61,6 +71,7 @@ export class Setup {
},
);
};
this.logger.log('debug', 'Setup default ACL settings');
await aclSetup();
this.httpServer.listen(this.port);

30
src/logging/LogUtil.ts Normal file
View File

@@ -0,0 +1,30 @@
import { LazyLoggerFactory } from './LazyLoggerFactory';
import type { Logger } from './Logger';
/**
* Gets a logger instance for the given class instance.
*
* The returned type of logger depends on the configured {@link LoggerFactory} in {@link Setup}.
*
* The following shows a typical pattern on how to create loggers:
* ```
* class MyClass {
* protected readonly logger = getLoggerFor(this);
* }
* ```
* If no class is applicable, a logger can also be created as follows:
* ```
* const logger = getLoggerFor('MyFunction');
* ```
*
* @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);
/**
* Helper interface to identify class instances.
*/
interface Instance {
constructor: { name: string };
}