Back to all reviewers

Use constructor.name context

novuhq/novu
Based on 2 comments
TypeScript

Always set logger context using `this.constructor.name` and configure it via the `setContext()` method rather than hardcoding context strings or passing context as parameters to individual log calls.

Logging TypeScript

Reviewer Prompt

Always set logger context using this.constructor.name and configure it via the setContext() method rather than hardcoding context strings or passing context as parameters to individual log calls.

This approach ensures consistency across the codebase and proper context display in logs. Hardcoded context strings can become outdated when classes are renamed, while this.constructor.name automatically reflects the current class name.

Correct approach:

@Injectable()
export class SendWebhookMessage {
  constructor(private logger: PinoLogger) {
    this.logger.setContext(this.constructor.name);
  }

  async execute() {
    // Context is already set, no need to pass it
    this.logger.error({ err: error }, 'Failed to create execution details');
  }
}

Avoid:

// Don't use hardcoded context strings
const LOG_CONTEXT = 'SendWebhookMessageUseCase';
this.logger.setContext(LOG_CONTEXT);

// Don't pass context as parameter - it won't be used properly
this.logger.error({ err: error }, 'Failed to create execution details', LOG_CONTEXT);

Setting context once in the constructor ensures all log messages from that class instance include the proper context automatically, making logs more traceable and maintainable.

2
Comments Analyzed
TypeScript
Primary Language
Logging
Category

Source Discussions