Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not printing any debug statements #696

Open
toymachiner62 opened this issue Mar 19, 2019 · 8 comments
Open

Not printing any debug statements #696

toymachiner62 opened this issue Mar 19, 2019 · 8 comments

Comments

@toymachiner62
Copy link

toymachiner62 commented Mar 19, 2019

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

@Qix-
Copy link
Member

Qix- commented Mar 19, 2019

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');

@toymachiner62
Copy link
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?

@Qix-
Copy link
Member

Qix- commented Mar 19, 2019

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.

@toymachiner62
Copy link
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.

@TooTallNate
Copy link
Contributor

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

const debug = require('debug')('my-namespace');

debug.enabled = true;

debug('some stuff');

@toymachiner62
Copy link
Author

toymachiner62 commented Mar 19, 2019

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

@Qix-
Copy link
Member

Qix- commented Apr 2, 2019

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.

@ndastur
Copy link

ndastur commented Mar 14, 2020

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
Labels
None yet
Development

No branches or pull requests

4 participants