This repository has been archived by the owner on Oct 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
84 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |