Skip to content

Commit

Permalink
#92 implement tslog version 4 (#93)
Browse files Browse the repository at this point in the history
* Updated all dependencies
* Added type to logger for tslog 4
* Implemented LogBuilder
  • Loading branch information
basmasking authored Dec 16, 2022
1 parent 33d35ab commit 92d8464
Show file tree
Hide file tree
Showing 12 changed files with 166 additions and 125 deletions.
179 changes: 89 additions & 90 deletions packages/jitar-nodejs-server/package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions packages/jitar-nodejs-server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jitar-nodejs-server",
"version": "0.2.1",
"version": "0.2.2",
"description": "NodeJS server implementation for Jitar.",
"author": "Masking Technology <[email protected]> (https://jitar.dev)",
"license": "MIT",
Expand All @@ -22,14 +22,14 @@
"dependencies": {
"@overnightjs/core": "^1.7.6",
"class-transformer": "^0.5.1",
"class-validator": "^0.13.2",
"class-validator": "^0.14.0",
"express": "^4.18.1",
"express-http-proxy": "^1.6.3",
"fs-extra": "^11.1.0",
"glob": "^8.0.3",
"glob-promise": "^5.0.0",
"mime-types": "^2.1.35",
"tslog": "^3.3.3",
"tslog": "^4.4.4",
"yargs": "^17.6.0",
"jitar": "^0.2.1"
},
Expand Down
26 changes: 9 additions & 17 deletions packages/jitar-nodejs-server/src/JitarServer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import { Server as OvernightServer } from '@overnightjs/core';
import bodyParser from 'body-parser';
import { Logger, TLogLevelName } from 'tslog';
import { Logger } from 'tslog';

import { HealthCheck, LocalGateway, LocalNode, LocalRepository, Proxy, Runtime } from 'jitar';

Expand All @@ -22,6 +22,7 @@ import RuntimeConfiguration from './configuration/RuntimeConfiguration.js';
import RuntimeDefaults from './definitions/RuntimeDefaults.js';

import RuntimeNotAvaiable from './errors/RuntimeNotAvaiable.js';
import LogBuilder from './utils/LogBuilder.js';

const STARTUP_MESSAGE = `
██ ██ ████████ █████ ██████
Expand Down Expand Up @@ -53,7 +54,7 @@ export default class JitarServer extends OvernightServer
const configuration = await RuntimeConfigurationLoader.load(options.config);
const runtime = await RuntimeConfigurator.configure(configuration);

const logger = this.#createLogger(options.loglevel);
const logger = LogBuilder.build(options.loglevel);

this.#addControllers(configuration, runtime, logger);

Expand All @@ -76,16 +77,7 @@ export default class JitarServer extends OvernightServer
this.#runtime.addHealthCheck(name, healthCheck);
}

#createLogger(level: string): Logger
{
return new Logger(
{
displayFilePath: 'hidden',
minLevel: level as TLogLevelName
});
}

#addControllers(configuration: RuntimeConfiguration, runtime: Runtime, logger: Logger): void
#addControllers(configuration: RuntimeConfiguration, runtime: Runtime, logger: Logger<unknown>): void
{
if (configuration.standalone !== undefined && runtime instanceof Proxy)
{
Expand Down Expand Up @@ -113,7 +105,7 @@ export default class JitarServer extends OvernightServer
}
}

#addStandAloneControllers(proxy: Proxy, logger: Logger, index: string): void
#addStandAloneControllers(proxy: Proxy, logger: Logger<unknown>, index: string): void
{
super.addControllers(new HealthController(proxy, logger));
super.addControllers(new JitarController(this.app));
Expand All @@ -123,28 +115,28 @@ export default class JitarServer extends OvernightServer
super.addControllers(new AssetsController(this.app, proxy, index, logger));
}

#addRepositoryControllers(repository: LocalRepository, logger: Logger, index: string): void
#addRepositoryControllers(repository: LocalRepository, logger: Logger<unknown>, index: string): void
{
super.addControllers(new JitarController(this.app));
super.addControllers(new ModulesController(repository, logger));
super.addControllers(new AssetsController(this.app, repository, index, logger));
}

#addGatewayControllers(gateway: LocalGateway, logger: Logger): void
#addGatewayControllers(gateway: LocalGateway, logger: Logger<unknown>): void
{
super.addControllers(new NodesController(gateway, logger));
super.addControllers(new ProceduresController(gateway, logger));
super.addControllers(new RPCController(gateway, logger, false));
}

#addNodeControllers(node: LocalNode, logger: Logger): void
#addNodeControllers(node: LocalNode, logger: Logger<unknown>): void
{
super.addControllers(new HealthController(node, logger));
super.addControllers(new ProceduresController(node, logger));
super.addControllers(new RPCController(node, logger, true));
}

#addProxyControllers(proxy: Proxy, logger: Logger): void
#addProxyControllers(proxy: Proxy, logger: Logger<unknown>): void
{
super.addControllers(new ProxyController(this.app, proxy, logger));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@

import { Contains, IsOptional, IsString } from 'class-validator';
import { Contains, IsEnum, IsOptional, IsString } from 'class-validator';
import { LogLevel } from '../utils/LogBuilder.js';

export default class ServerOptions
{
@IsString()
@IsOptional()
@IsEnum(LogLevel)
loglevel = 'info';

@IsString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export default class AssetsController
{
#repository: LocalRepository | Proxy;
#indexFile: string;
#logger: Logger;
#logger: Logger<unknown>;

constructor(app: express.Application, repository: LocalRepository | Proxy, indexFile: string, logger: Logger)
constructor(app: express.Application, repository: LocalRepository | Proxy, indexFile: string, logger: Logger<unknown>)
{
this.#repository = repository;
this.#indexFile = indexFile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { LocalNode, Proxy } from 'jitar';
export default class HealthController
{
#node: LocalNode | Proxy;
#logger: Logger;
#logger: Logger<unknown>;

constructor(node: LocalNode | Proxy, logger: Logger)
constructor(node: LocalNode | Proxy, logger: Logger<unknown>)
{
this.#node = node;
this.#logger = logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { ClientId, LocalRepository, Proxy, ValueSerializer } from 'jitar';
export default class ModulesController
{
#repository: LocalRepository | Proxy;
#logger: Logger;
#logger: Logger<unknown>;

constructor(repository: LocalRepository | Proxy, logger: Logger)
constructor(repository: LocalRepository | Proxy, logger: Logger<unknown>)
{
this.#repository = repository;
this.#logger = logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import DataConverter from '../utils/DataConverter.js';
export default class NodesController
{
#gateway: LocalGateway;
#logger: Logger;
#logger: Logger<unknown>;

constructor(gateway: LocalGateway, logger: Logger)
constructor(gateway: LocalGateway, logger: Logger<unknown>)
{
this.#gateway = gateway;
this.#logger = logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { LocalGateway, LocalNode, Proxy } from 'jitar';
export default class ProceduresController
{
#runtime: LocalGateway | LocalNode | Proxy;
#logger: Logger;
#logger: Logger<unknown>;

constructor(runtime: LocalGateway | LocalNode | Proxy, logger: Logger)
constructor(runtime: LocalGateway | LocalNode | Proxy, logger: Logger<unknown>)
{
this.#runtime = runtime;
this.#logger = logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { Proxy } from 'jitar';
@Controller('')
export default class ProxyController
{
#logger: Logger;
#logger: Logger<unknown>;

#repositoryUrl: string;
#runnerUrl: string;

constructor(app: express.Application, proxy: Proxy, logger: Logger)
constructor(app: express.Application, proxy: Proxy, logger: Logger<unknown>)
{
this.#logger = logger;

Expand Down
4 changes: 2 additions & 2 deletions packages/jitar-nodejs-server/src/controllers/RPCController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ const NOT_FOUND_ERRORS = [ImplementationNotFound, ProcedureNotFound];
export default class RPCController
{
#runtime: ProcedureContainer;
#logger: Logger;
#logger: Logger<unknown>;
#useSerializer: boolean;

constructor(runtime: ProcedureContainer, logger: Logger, useSerializer: boolean)
constructor(runtime: ProcedureContainer, logger: Logger<unknown>, useSerializer: boolean)
{
this.#runtime = runtime;
this.#logger = logger;
Expand Down
48 changes: 48 additions & 0 deletions packages/jitar-nodejs-server/src/utils/LogBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

import { Logger } from 'tslog';

export enum LogLevel
{
DEBUG = 'debug',
INFO = 'info',
WARN = 'warn',
ERROR = 'error',
FATAL = 'fatal'
}

export default class LogBuilder
{
static build(level: string): Logger<unknown>
{
const logConfiguration = this.#getLogConfiguration(level);

return new Logger(logConfiguration);
}

static #getLogConfiguration(level: string): object
{
const logLevel = this.#getLogLevel(level);

return {
prettyLogTemplate: "{{dateIsoStr}}\t{{logLevelName}}\t",
minLevel: logLevel
};
}

static #getLogLevel(level: string): number
{
switch (level)
{
case LogLevel.FATAL:
return 6;
case LogLevel.ERROR:
return 5;
case LogLevel.WARN:
return 4;
case LogLevel.INFO:
return 3;
default:
return 2;
}
}
}

0 comments on commit 92d8464

Please sign in to comment.