Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
Dev/logging configuration (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
OG-NI authored Apr 28, 2024
1 parent d2ca39d commit 1a2ea1c
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 5 deletions.
54 changes: 54 additions & 0 deletions backend/docs/logging.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Logging

Die Logging-Funktionen werden durch das Interface `LoggerService` abstrahiert. Dadurch kann später einfacher zu einem anderen Logger gewechselt werden. Aktuell wird der von NestJS bereitgestellte `ConsoleLogger` (<https://docs.nestjs.com/techniques/logger>) verwendet. Dieser erfüllt das `LoggerService` Interface.

## Logging Level

Es werden vier Logging Level unterstützt, um unterschiedliche Dringlichkeitsstufen zu repräsentieren:

- **log**: allgemeine Informationen
- **error**: Fehler
- **warn**: Warnungen
- **debug**: Informationen zur Fehlerbehebung

## Beispiel

Im Modul wird der Logger Service als Provider registriert. Dabei kann mit dem Parameter `useClass` die konkrete Implementierung gewählt werden.

```ts
import { ConsoleLogger, Module } from '@nestjs/common';
import { ExampleController } from './example.controller';
import { LOGGER_SERVICE } from './logger/logger.service';

@Module({
controllers: [ExampleController],
providers: [
{
useClass: ConsoleLogger,
provide: LOGGER_SERVICE,
},
],
})
export class ExampleModule {}
```

In z.B. einem Controller kann dann durch Dependency Injection auf den Logger zugegriffen werden. Dafür wird dieser als Parameter im Konstruktor übergeben und mit dem `@Inject`-Decorator versehen.

```ts
import { Controller, Get, Inject } from '@nestjs/common';
import { LOGGER_SERVICE, LoggerService } from './logger/logger.service';

@Controller()
export class ExampleController {
constructor(
@Inject(LOGGER_SERVICE)
private readonly loggerService: LoggerService,
) {}

@Get('/example')
getExample(): string {
this.loggerService.log('example log message');
return 'example';
}
}
```
6 changes: 5 additions & 1 deletion backend/src/app.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { ConsoleLogger } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { LoggerService } from './logger/logger.service';

describe('AppController', () => {
let appController: AppController;
let loggerService: LoggerService;
let appService: AppService;

beforeEach(async () => {
appService = new AppService();
appController = new AppController(appService);
loggerService = new ConsoleLogger();
appController = new AppController(appService, loggerService);
});

describe('getPing', () => {
Expand Down
10 changes: 8 additions & 2 deletions backend/src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { Controller, Get } from '@nestjs/common';
import { Controller, Get, Inject } from '@nestjs/common';
import { AppService } from './app.service';
import { LOGGER_SERVICE, LoggerService } from './logger/logger.service';

@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
constructor(
private readonly appService: AppService,
@Inject(LOGGER_SERVICE)
private readonly loggerService: LoggerService,
) {}

/**
* Ping
* @returns {string} "pong"
*/
@Get('/ping')
getPing(): string {
this.loggerService.log('ping');
return this.appService.getPing();
}
}
11 changes: 9 additions & 2 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { Module } from '@nestjs/common';
import { ConsoleLogger, Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AuthModule } from './auth/auth.module';
import { UserController } from './api/user/user.controller';
import { LOGGER_SERVICE } from './logger/logger.service';

@Module({
imports: [AuthModule],
controllers: [AppController, UserController],
providers: [AppService],
providers: [
AppService,
{
useClass: ConsoleLogger,
provide: LOGGER_SERVICE,
},
],
})
export class AppModule {}
8 changes: 8 additions & 0 deletions backend/src/logger/logger.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const LOGGER_SERVICE = 'LOGGER_SERVICE';

export interface LoggerService {
log(message: string): void;
error(message: string): void;
warn(message: string): void;
debug(message: string): void;
}

0 comments on commit 1a2ea1c

Please sign in to comment.