-
Notifications
You must be signed in to change notification settings - Fork 25
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
feat: adding debug logger #64
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to work out how/whether we continue to support NODE_DEBUG=axios
.
lib/helper.ts
Outdated
@@ -129,6 +130,7 @@ export function getMissingParams( | |||
* @returns {boolean} true if 'text' has html tags | |||
*/ | |||
export function isHTML(text: string): boolean { | |||
logger.debug(`Determining if the text ${text} is HTLML.`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo here. "HTLML" -> "HTML"
To see the output from all of the debugging scopes you can use: | ||
|
||
```DEBUG=node-sdk-core*``` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should add a note here about how DEBUG can be set for more than one library, using comma-separated debug strings. E.g.:
DEBUG=node-sdk-core:debug,other-lib:debug
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the way I set the logger up is for all the logger instances to be under node-sdk-core
. So the user can only enable/disable loggers under one library DEBUG=node-sdk-core:debug,node-sdk-core:error
. I originally did have a logger for each library but I moved towards the simpler approach of just having one for each log scope. Do we want to have my original approach instead? One for baseservice, helper, etc.. ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mkistler - please see the changes I made to the README. Let me know if this doesn't work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the difference between verbose
and debug
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debug level includes all the logs, and verbose does as well excluding logs that are logged debug. This mimics winston's log levels based on the standard https://tools.ietf.org/html/rfc5424
} catch { | ||
console.debug(config) | ||
logger.error(config) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's something still not quite right here. If someone sets NODE_DEBUG=axios
but does not set DEBUG=node-sdk-core:debug
, then they will get no debug output (if I'm reading this correctly).
It appears that we don't actually document the behavior of NODE_DEBUG=axios
, so I guess we need to decide how we want this to work. We could just drop support for NODE_DEBUG=axios
now that we have this new logging support.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes you're right. Was that flag initially added just to log the request and response being intercepted?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mkistler - I removed the DEBUG flag from here and I just enabled all the loggers if the axios flag is set. Please take a look at logger.ts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mkistler - I removed the DEBUG flag from here and I just enabled all the loggers if the axios flag is set. Please take a look at I just realized removing logger.ts
DEBUG
flag was a mistake. Since removing made that block dependent on the axios flag being set now. Re-adding it back in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay I see what @mkistler was saying about removing this "if" statement but I actually think it is fine to leave it there. Only because if this code executes, it will add these interceptors that will get run every time a request is made. This isn't a big performance hit or anything but since it's easily avoidable, I don't think we need to force the user through this pattern every time. Just a thought
598789c
to
035392a
Compare
This is looking good @jorge-ibm Buen trabajo |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leaving a few comments, mostly just to make sure we are consistent about logging strings rather than error objects
const err = new Error('`requestToken` MUST be overridden by a subclass of JwtTokenManagerV1.'); | ||
const errMsg = '`requestToken` MUST be overridden by a subclass of JwtTokenManagerV1.'; | ||
const err = new Error(errMsg); | ||
logger.error(err); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other error logs in this file log the message but this is logging the entire Error object, which includes the stack trace, etc. This should probably be changed to
logger.error(err); | |
logger.error(errMsg); |
lib/base-service.ts
Outdated
@@ -85,6 +87,7 @@ export class BaseService { | |||
// check serviceUrl for common user errors | |||
const credentialProblems = checkCredentials(options, ['serviceUrl']); | |||
if (credentialProblems) { | |||
logger.error(credentialProblems); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to my other comment, if we don't want to include the stack trace in these logs, we don't need to log the whole error (checkCredentials
returns an error object). This can be:
logger.error(credentialProblems); | |
logger.error(credentialProblems.message); |
} catch { | ||
console.debug(config) | ||
logger.error(config) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay I see what @mkistler was saying about removing this "if" statement but I actually think it is fine to leave it there. Only because if this code executes, it will add these interceptors that will get run every time a request is made. This isn't a big performance hit or anything but since it's easily avoidable, I don't think we need to force the user through this pattern every time. Just a thought
README.md
Outdated
- ```DEBUG=ibm-cloud-sdk-core:info``` enables info logs | ||
- ```DEBUG=ibm-cloud-sdk-core:debug``` enables debug logs | ||
- ```DEBUG=ibm-cloud-sdk-core:verbose``` enables verbose logs | ||
- ```DEBUG=ibm-cloud-sdk-core:warning``` enables warning logs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose this is fine but I feel like the typical idea in loggers is that you set one level and then you get the logs from that level and also everything "under" that level in terms of decreasing verbosity. For example, if I set the level "verbose", I would also expect to get error and warnings and info, etc.
This seems like a limitation of debug
and not your implementation so I'm not sure there's anything we can do here but I hope we aren't creating an experience that's less than ideal
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This wasn't specified in the documentation, and yes it's a limitation of using debug. We can manually force this ourselves by enabling each log level through some conditions, but something to keep in mind is that I noticed debug was being used by other packages in our code. So that might make our version of log levels different than what the other packages do if they did not manually enforce that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed the same thing and would like to put in my vote for making the levels "additive", with a clear indication of the order of inclusion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. I've gone ahead and implemented log levels
035392a
to
425e419
Compare
try { | ||
console.debug(JSON.stringify(error, null, 2)); | ||
logger.error(JSON.stringify(error, null, 2)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the error logged here does not include the original request, it would be really nice if we could find a way to log the request here as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I ran a test and it looks like the error object returned does in fact contain the original request (under error.config
), so as long as the stringify has been doing its job then that should have been logged as well
7ecfd3e
to
f40401a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! 👍
Left one minor comment -- does not affect functionality, just code simplification
lib/logger.ts
Outdated
verbose.enabled = true; | ||
info.enabled = true; | ||
warn.enabled = true; | ||
error.enabled = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need these four lines. The logic below handles this.
auth/utils/read-credentials-file.ts
Outdated
|
||
const filename: string = 'ibm-credentials.env'; | ||
|
||
export function readCredentialsFile() { | ||
if (!fs.existsSync) { | ||
logger.warn('Unable to find the library fs.existsSync'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This usually happen when running in a browser and it's not a warning. I can't come up with a better message but I see this from a browser developer perspective and that person will be getting a warning that is not a problem
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add a +1 here, we can probably just remove that log
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left 2 comments but this looks good
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good after addressing Mike and German's comments!
lib/base-service.ts
Outdated
@@ -65,6 +66,7 @@ export class BaseService { | |||
*/ | |||
constructor(userOptions: UserOptions) { | |||
if (!(this instanceof BaseService)) { | |||
logger.error('Error creating an instance of BaseService: the "new" keyword is required to create service instances'); | |||
throw new Error( | |||
'the "new" keyword is required to create service instances' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is picky but the string the "new" keyword is required to create service instances
could be assigned to a variable since it is used twice here. Not a required change
f40401a
to
9259c95
Compare
9259c95
to
0b09654
Compare
# [1.1.0](v1.0.0...v1.1.0) (2019-10-14) ### Features * adding debug logger ([#64](#64)) ([6079ca0](6079ca0))
🎉 This PR is included in version 1.1.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Based on feedback I have added logging through the debug module. Log messages are suppressed by default unless the specified environment variables in the README are set. I've gone ahead and updated some unit tests that broke after this change, added unit tests for the logger itself, and replaced all
console.log
references throughout the code.Checklist
npm test
passes (tip:npm run lint-fix
can correct most style issues)