Skip to content

Commit

Permalink
Added log levels
Browse files Browse the repository at this point in the history
  • Loading branch information
Carsten Patzke committed Mar 4, 2020
1 parent 8711561 commit 80714c9
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 26 deletions.
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
# InfiniBand Radar Client & API Server

If you have any questions please [open an issue](https://github.com/infiniband-radar/infiniband-radar-daemon/issues)

This repository contains the web interface and API server.

The API and WebClient **can be run on a server that is not connected to the InfiniBand**.
If you have any questions please [open an issue](https://github.com/infiniband-radar/infiniband-radar-daemon/issues).

This repository contains the web interface and API server.
The API and WebClient **can be run on a server that is not connected to the InfiniBand**.
To run a complete setup you need to install the [daemon](https://github.com/infiniband-radar/infiniband-radar-daemon) on a server that is connected to a InfiniBand fabric.

![InfiniBand-Radar](./screenshot1.png)

## Requirements:
## Requirements
- A folder with SSL files called `cert.pem` and `key.pem`
- docker >= 17.05 and [docker-compose](https://github.com/docker/compose/releases)
- Ports 80 and 443 available

## Start a fresh installation:
## Start a fresh installation
1. Clone this repo
2. Copy the `*.template.json` to `*.json` from `./config` and edit them to your needs
3. Share the server keys with your daemon config
Expand Down Expand Up @@ -44,7 +42,7 @@ GATEWAY_CERTS="$PWD/ssl_certs" docker-compose up --build -d

`/config/apiServer.json`

```json
```json5
{
"server": { // API webserver configuration, if you are using docker-compose leave it as it is
"host": "0.0.0.0",
Expand Down Expand Up @@ -86,7 +84,8 @@ GATEWAY_CERTS="$PWD/ssl_certs" docker-compose up --build -d
"mongoDb": {
"host": "mongodb://mongodb:27017/",
"database": "infiniband_radar"
}
},
"logLevel": "Debug" // Available: Debug, Info, Warning, Error
}
```

Expand Down
2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "infiniband-radar-client",
"version": "2.4.4",
"version": "2.4.5",
"private": true,
"scripts": {
"dev": "npm run serve",
Expand Down
2 changes: 1 addition & 1 deletion client/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import 'flot/jquery.flot.time';
import 'flot/jquery.flot.crosshair';
import 'flot/jquery.flot.selection';

import './3dparty/flot/jquery.flot.byte';
import 'flot/jquery.flot.byte';

// Add and use a precise time format
// Otherwise it would show: 'a few seconds' instead of '3 seconds'
Expand Down
3 changes: 2 additions & 1 deletion config/apiServer.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@
"mongoDb": {
"host": "mongodb://mongodb:27017/",
"database": "infiniband_radar"
}
},
"logLevel": "Debug"
}
3 changes: 2 additions & 1 deletion config/apiServer.template.ldap.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@
"mongoDb": {
"host": "mongodb://mongodb:27017/",
"database": "infiniband_radar"
}
},
"logLevel": "Debug"
}
6 changes: 3 additions & 3 deletions server/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "infiniband-radar-api-server",
"version": "2.4.4",
"version": "2.4.5",
"description": "",
"main": "src/Server.ts",
"private": true,
"scripts": {
"start": "ts-node src/Server.ts",
"dev": "cross-env NODE_ENV=development ts-node-dev --inspect --respawn --ignore-watch node_modules --type-check src/Server.ts",
"start": "ts-node src/Main.ts",
"dev": "cross-env NODE_ENV=development ts-node-dev --inspect --respawn --ignore-watch node_modules --type-check src/Main.ts",
"tslint": "tslint -p .",
"build": "tsoa swagger && pretty-swag -i dist/swagger.json -o dist/documentation.html && tsoa routes",
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
11 changes: 11 additions & 0 deletions server/src/Main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Logger } from './lib/Logger';
import { Container } from 'typescript-ioc';
import { ConfigService } from './services/ConfigService';

const config = Container.get(ConfigService) as ConfigService;
Logger.setGlobalLogLevelFromString(config.getLogLevelAsString());

// Start server

import { Server } from './Server';
const server = new Server();
6 changes: 2 additions & 4 deletions server/src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { Logger } from './lib/Logger';
import { MetricDatabase } from './services/MetricDatabase';
import { TopologyDatabase } from './services/TopologyDatabase';
import { UserDatabase } from './services/UserDatabase';
import {AuthenticationService} from './services/AuthenticationService';
import { AuthenticationService } from './services/AuthenticationService';

class Server {

export class Server {
private static readonly log = Logger.getLogger(Server);

@Inject
Expand Down Expand Up @@ -45,5 +45,3 @@ class Server {
);
}
}

new Server();
50 changes: 44 additions & 6 deletions server/src/lib/Logger.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
enum LogLevel {
Error = '\x1b[31mERROR\x1b[0m',
Warning = '\x1b[33mWARN\x1b[0m',
Info = '\x1b[36mINFO\x1b[0m',
Debug = '\x1b[37mDebug\x1b[0m',
export enum LogLevel {
Unknown,
Error,
Warning,
Info,
Debug,
}

export class Logger {
private static globalLogLevel: LogLevel = LogLevel.Debug;
private static logLevelText: Readonly<{[level: number]: string}> = {
[LogLevel.Error]: '\x1b[31mERROR\x1b[0m',
[LogLevel.Warning]: '\x1b[33mWARN\x1b[0m',
[LogLevel.Info]: '\x1b[36mINFO\x1b[0m',
[LogLevel.Debug]: '\x1b[37mDebug\x1b[0m',
};

private readonly name: string;

private constructor(name: string) {
Expand Down Expand Up @@ -53,8 +62,37 @@ export class Logger {
return new Logger(type.name);
}

/**
* Sets the global log level of the application
* @param minLogLevel Minimum log level that is printed
*/
public static setGlobalLogLevel(minLogLevel: LogLevel) {
this.globalLogLevel = minLogLevel;
const lvlText = Logger.logLevelText[minLogLevel];
console.log(`Log level is: [${lvlText}]`);
}

public static setGlobalLogLevelFromString(minLogLevel: string) {
const lvl: LogLevel = LogLevel[minLogLevel];
if (!lvl) {
const error = new Error(`Invalid log level '${minLogLevel}'`);
error.stack = error.stack.split('\n', 3).slice(0, 2).join('\n');
throw error;
}
this.setGlobalLogLevel(lvl);
}

private shouldBeLogged(logLevel: LogLevel): boolean {
return logLevel <= Logger.globalLogLevel;
}

private log(logLevel: LogLevel, ...args) {
console.log.apply(console, [`[${new Date().toUTCString()}][${logLevel}][${this.name}]`, ...args]);
if (this.shouldBeLogged(logLevel)) {
const timestamp = new Date().toUTCString();
const lvlText = Logger.logLevelText[logLevel];

console.log.apply(console, [`[${timestamp}][${lvlText}][${this.name}]`, ...args]);
}
}

}
21 changes: 21 additions & 0 deletions server/src/services/ConfigService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AutoWired, Singleton } from 'typescript-ioc';
import * as fs from 'fs';
import * as path from 'path';
import { FabricId } from '../../../common/models/AliasTypes';
import { LogLevel } from '../lib/Logger';

export interface ServerConfig {
host: string;
Expand Down Expand Up @@ -54,6 +55,7 @@ export interface IConfig {
authentication: AuthenticationConfig;
influxDb: InfluxDbConfig;
mongoDb: MongoDbConfig;
logLevel: string;
}

function niceStringifyJson(data: any): string {
Expand Down Expand Up @@ -94,13 +96,32 @@ export class ConfigService {
return this.config.mongoDb;
}

public getLogLevelAsString(): string {
return this.config.logLevel;
}

private loadAndResaveConfig() {
const absPath = path.resolve(__dirname, ConfigService.configPath);
this.config = JSON.parse(fs.readFileSync(absPath, 'utf8'));
this.applyPatches();
try {
fs.writeFileSync(absPath, niceStringifyJson(this.config), 'utf8');
} catch (e) {
// Ignored. Maybe the file is write protected
}
}

/**
* This function is used to apply patches to the config.
* For example when the log levels were introduced, but not all configs had it set.
*/
private applyPatches() {
this.applyPatchLogLevel();
}

private applyPatchLogLevel() {
if (!this.config.logLevel) {
this.config.logLevel = LogLevel[LogLevel.Info];
}
}
}

0 comments on commit 80714c9

Please sign in to comment.