You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
constaxios=require('axios')constprogram=require('commander')constdebug=require('debug')constconfig=require('./config.json')constgetJwt=require('./getJwt.js')constpackage=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
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:
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?
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 */constdebug=require('debug')('my-app:auth');debug('this has something to do with authentication');/* db.js */constdebug=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.jsmy-app:auth this has something to do with authentication +0msmy-app:db this has something to do with authentication +0ms
$ DEBUG='my-app:auth' node ./index.jsmy-app:auth this has something to do with authentication +0ms
etc.
Reacted by tomislav13, Tomas Vancoillie and Fabio Martins
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.
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
constaxios=require('axios')constprogram=require('commander')constdebug=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
Activity
Qix- commentedon 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:
toymachiner62 commentedon Mar 19, 2019
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- commentedon Mar 19, 2019
No.
A namespace is just a way to group logical sets of logs together.
debuggives you the ability to filter out particular namespaces.How you should be using
debugis via the environment variable, not via a flag. This is what it's for - it's not necessarily meant to be a logging mechanism.etc.
toymachiner62 commentedon 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.
TooTallNate commentedon Mar 19, 2019
To create a
debuginstance that is always enabled, do this:toymachiner62 commentedon Mar 19, 2019
Actually setting the env var in the script doesn't work. Should it?
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 = deleteWhen i set the env var when calling the script it does work. Curious if this is expected behavior.
Qix- commentedon 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 commentedon Mar 14, 2020
Just to point out the documentation shows debug.enable as a function. But doesn't seem to exist. Setting .enabled works