domain-driven-hexagon icon indicating copy to clipboard operation
domain-driven-hexagon copied to clipboard

Nest removed Logger.setContext()

Open lp-belliot opened this issue 2 years ago • 3 comments

Thanks so much for effort writing all this, this repo has been my go to for helping me grok DDD books and their concepts in the context of something tangible.

I'm working through your example implementation and noticed that at some point Nest upgraded their Logger implementation, so the Logger.setContext() method is no longer available. Instead you're expected to pass the context as an optional second parameter in the log call.

All I can think is to add the log method overloads to the Logger port interface and then add a private class attribute for the logContext wherever you've previously called setContext().

Is there a cleaner way to deal with this that I'm missing?

lp-belliot avatar Jan 14 '22 04:01 lp-belliot

Hey. Thanks for reporting the issue, I will check that out when I have time.

Sairyss avatar Jan 14 '22 10:01 Sairyss

@Sairyss Thanks so much for writing such a repository. I use the following code to create a custom logger.

// @libs/ddd/domain/ports/logger.port import { Logger as NestLogger } from '@nestjs/common';

export class Logger extends NestLogger { setContext(context: string): void { this.context = context; } }

vahidzafari avatar Apr 17 '22 18:04 vahidzafari

Hey. Thanks to @Sairyss for sharing this very nice repository. I'm learning Nestjs, DDD and programming and it is a very helpful and valuable resource :)

@vahidzafari , I'm not sure it is the right way to proceed according to ddd principles. In my understanding, the port is part of the domain and should only define an interface without any dependency with the real implementation. Therefore, I guess it should be better to keep the port definition as it is and just define an adapter to implement it.

Could be something like this (not tested yet) :

// @libs/ddd/infrastructure/logger/logger.adapter.ts import { ConsoleLogger, Injectable } from '@nestjs/common'; import { LoggerPort } from '@libs/ddd/domain/ports/logger.port';

@Injectable({ scope: Scope.TRANSIENT }) export class LoggerAdapter extends ConsoleLogger implements LoggerPort {}

// @libs/ddd/infrastructure/logger/logger.module.ts import { Module, Global } from '@nestjs/common'; import { LoggerAdapter } from './logger.adapter';

@Global() @Module({ providers: [LoggerAdapter], exports: [LoggerAdapter], }) export class LoggerModule {}

Indeed, the setContext method was removed from the Logger class but kept in the ConsoleLogger one. The Nestjs team encourage to use the latter now.

pepsi92 avatar Apr 24 '22 14:04 pepsi92