Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

473
Views
NestJS - avoid setContext() in constructor on logger injection

In NestJS I have custom logger:

import { Injectable, Logger, Scope } from '@nestjs/common';

@Injectable({ scope: Scope.TRANSIENT })
export class LoggerService extends Logger {
  log(message: any) {
    super.log(message);
  }

  error(message: any) {
    super.log(message);
  }

  warn(message: any) {
    super.log(message);
  }

  debug(message: any) {
    super.log(message);
  }

  verbose(message: any) {
    super.log(message);
  }

  setContext(context: string) {
    super.context = context;
  }
}

It is registered globally:

import { Global, Module } from '@nestjs/common';
import { LoggerService } from './logger.service';

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

Is there any way to somehow pass context on injection in service constructor and avoid execution of logger.setContext(context) in every service - instead just set it in LoggerService constructor?

Example usage now:

constructor(private logger: LoggerService) {
    this.logger.setContext(ClassName.name);
}

Expected usage:

constructor(private logger: LoggerService) {}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

When you do private logger: LoggerService, there's no chance to make that .setContext(Service.name) call.

What you could do is something like:
@Logger(Service.name) private logger: LoggerService

How? Read this article: Advanced NestJS: Dynamic Providers

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!