Skip to content

Not printing any debug statements #696

Open

Description

@toymachiner62

I'm sure i'm doing something wrong, but I have this code and it's not printing any debug statement's

#!/usr/bin/env node

const axios = require('axios')
const program = require('commander')
const debug = require('debug')
const config = require('./config.json')
const getJwt = require('./getJwt.js')
const package = require('./package.json')
const { envs } = require('./util')

program
  .usage('[options]')
  .description('Delete files/data for a specific account')
  .option('-v --version', package.version)
  .option('-e --env <env>', 'Environment to create account for. Options are "dev", "qa", stage", or "prod"')
  .option('-m, --email <required>', 'email')
  .option('-p, --password <required>', 'password')
  .option('-a --all', 'Remove all files and documents')
  .option('-b --debug', 'Enable debug mode')
  .parse(process.argv)

if (program.debug) {
  console.log('in program.debug')
  debug.enable('debug')
}
console.log('debug is enabled =', debug.enabled('debug'))
debug('here') // WHY ISN'T THIS PRINTING?

And here's the output from my command:

$ ./deleteUserData -e dev -m foo@example.com -p myPassword -a -b
in program.debug
debug is enabled = true

I'm using version 4.1.1 of debug

Activity

  1. Qix- commented on Mar 19, 2019

    @Qix-
    Member

    Debug is a function that returns a function. The first invocation to debug takes the namespace and returns the actual function to make logging messages

    Your call to debug just creates namespaces but doesn't use them. You need to store the result somewhere and use it. Most people do this:

    const debug = require('debug')('some:namespace');
  2. toymachiner62 commented on Mar 19, 2019

    @toymachiner62
    Author

    I don't want to do that b/c i want to enable it ONLY if a flag is passed in. From what i understand from the docs, doesn't that namespace automatically enable the debug flag?

  3. Qix- commented on Mar 19, 2019

    @Qix-
    Member

    No.

    A namespace is just a way to group logical sets of logs together. debug gives you the ability to filter out particular namespaces.

    How you should be using debug is via the environment variable, not via a flag. This is what it's for - it's not necessarily meant to be a logging mechanism.

    /* auth.js */
    const debug = require('debug')('my-app:auth');
    debug('this has something to do with authentication');
    
    /* db.js */
    const debug = require('debug')('my-app:db');
    debug('this has something to do with the database');
    
    /* index.js */
    require('./db');
    require('./auth');
    $ DEBUG='my-app:*' node ./index.js
    my-app:auth    this has something to do with authentication   +0ms
    my-app:db      this has something to do with authentication   +0ms
    
    $ DEBUG='my-app:auth' node ./index.js
    my-app:auth    this has something to do with authentication   +0ms

    etc.

  4. toymachiner62 commented on Mar 19, 2019

    @toymachiner62
    Author

    If i have to use an env var i will set it in the script, but this is a script that I ideally users don't have to know to pass in env vars + flags + stand on their head and chug beer through their eyeball sockets. Ideally they just pass in flags.

    I'll tinker with your example above and see if i can get wrangle up what i'm trying to do. should be doable.

  5. TooTallNate commented on Mar 19, 2019

    @TooTallNate
    Contributor

    To create a debug instance that is always enabled, do this:

    const debug = require('debug')('my-namespace');
    
    debug.enabled = true;
    
    debug('some stuff');
  6. toymachiner62 commented on Mar 19, 2019

    @toymachiner62
    Author

    If i have to use an env var i will set it in the script, but this is a script that I ideally users don't have to know to pass in env vars + flags + stand on their head and chug beer through their eyeball sockets. Ideally they just pass in flags.

    I'll tinker with your example above and see if i can get wrangle up what i'm trying to do. should be doable.

    Actually setting the env var in the script doesn't work. Should it?

    #!/usr/bin/env node
    
    const axios = require('axios')
    const program = require('commander')
    const debug = require('debug')('delete')
    
    console.log('process.env.DEBUG = ', process.env.DEBUG)
    
    program
      .usage('[options]')
      .description('Delete files/data for a specific account')
      .option('-v --version', package.version)
      .option('-e --env <env>', 'Environment to create account for. Options are "dev", "qa", stage", or "prod"')
      .option('-m, --email <required>', 'email')
      .option('-p, --password <required>', 'password')
      .option('-a --all', 'Remove all files and documents')
      .option('-b --debug', 'Enable debug mode')
      .parse(process.argv)
    
    if (program.debug) {
      process.env.DEBUG = 'delete'
      console.log('process.env.DEBUG = ', process.env.DEBUG)
    }
    
    debug('here') // THIS WONT PRINT WHEN SETTING THE ENV VAR IN THE SCRIPT

    Without setting the flag when calling the script, it doesn't work and this is my output:

    $ node ./deleteUserData -e dev -m foo@example.com -p myPassword -a -b 
    process.env.DEBUG =  undefined
    in program.debug
    process.env.DEBUG =  delete

    When i set the env var when calling the script it does work. Curious if this is expected behavior.

    $ DEBUG='delete' node ./deleteUserData -e dev -m foo@example.com -p myPassword -a -b
    process.env.DEBUG =  delete
    in program.debug
    process.env.DEBUG =  delete
      delete here +0ms
  7. Qix- commented on Apr 2, 2019

    @Qix-
    Member

    You have to set the environment variable before the initial call to debug. It's a known inconvenience that we have a few issues open to address.

  8. ndastur commented on Mar 14, 2020

    @ndastur

    Just to point out the documentation shows debug.enable as a function. But doesn't seem to exist. Setting .enabled works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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