Skip to content

Debug in NestJs Application With DI and dotenv #750

@360disrupt

Description

@360disrupt

NestJs uses DI and dotenv for the configuration. I moved the initialization of the debug instance into the service constructor to make sure process.env.DEBUG is set. What confuses me is that this.debugSensorBoot.namespace === process.env.DEBUG is true but no log appears until it is manually enabled.

import debug from 'debug';
...
@Injectable()
export class SensorsService {
  debugSensorBoot: any;

  constructor() {
    console.log('process.env.DEBUG', process.env.DEBUG) // logs: process.env.DEBUG sensor:boot

    // (1) init debug
    this.debugSensorBoot = debug('sensor:boot');

    // (2) process.env.DEBUG is set - no logs appear
    console.log('this.debugSensorBoot', this.debugSensorBoot.namespace === process.env.DEBUG, this.debugSensorBoot.enabled) // logs: this.debugSensorBoot true false
    this.debugSensorBoot("Im not logging anything as I'm disabled") // does not log anything

    // (3) for example this works 
    this.debugSensorBoot.enabled = this.debugSensorBoot.namespace === process.env.DEBUG; // this is true as shown above
    this.debugSensorBoot("I log")
  }

I can add the .enable in the constructor as shown above but it seems quite hacky:

this.debugSensorBoot.enabled = this.debugSensorBoot.namespace === process.env.DEBUG; and it wouldn't work if it is equal.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions