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

feat: adding debug logger #64

Merged
merged 1 commit into from
Oct 14, 2019
Merged

feat: adding debug logger #64

merged 1 commit into from
Oct 14, 2019

Conversation

jorge-ibm
Copy link
Contributor

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)
  • tests are included
  • documentation is changed or added

Copy link
Contributor

@mkistler mkistler left a 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.`);
Copy link
Contributor

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*```

Copy link
Contributor

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

Copy link
Contributor Author

@jorge-ibm jorge-ibm Oct 9, 2019

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.. ?

Copy link
Contributor Author

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.

Copy link
Contributor

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?

Copy link
Contributor Author

@jorge-ibm jorge-ibm Oct 14, 2019

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)
}
Copy link
Contributor

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.

Copy link
Contributor Author

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?

Copy link
Contributor Author

@jorge-ibm jorge-ibm Oct 9, 2019

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

Copy link
Contributor Author

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 I just realized removing DEBUG flag was a mistake. Since removing made that block dependent on the axios flag being set now. Re-adding it back in.

Copy link
Member

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

@jorge-ibm jorge-ibm force-pushed the add-debug-logger branch 3 times, most recently from 598789c to 035392a Compare October 9, 2019 20:16
@germanattanasio
Copy link
Contributor

This is looking good @jorge-ibm Buen trabajo

Copy link
Member

@dpopp07 dpopp07 left a 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);
Copy link
Member

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

Suggested change
logger.error(err);
logger.error(errMsg);

@@ -85,6 +87,7 @@ export class BaseService {
// check serviceUrl for common user errors
const credentialProblems = checkCredentials(options, ['serviceUrl']);
if (credentialProblems) {
logger.error(credentialProblems);
Copy link
Member

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:

Suggested change
logger.error(credentialProblems);
logger.error(credentialProblems.message);

} catch {
console.debug(config)
logger.error(config)
}
Copy link
Member

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
Copy link
Member

@dpopp07 dpopp07 Oct 10, 2019

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

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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

try {
console.debug(JSON.stringify(error, null, 2));
logger.error(JSON.stringify(error, null, 2));
Copy link
Contributor

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.

Copy link
Contributor Author

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

@jorge-ibm jorge-ibm force-pushed the add-debug-logger branch 2 times, most recently from 7ecfd3e to f40401a Compare October 11, 2019 22:30
Copy link
Contributor

@mkistler mkistler left a 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
Comment on lines 11 to 14
verbose.enabled = true;
info.enabled = true;
warn.enabled = true;
error.enabled = true;
Copy link
Contributor

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.


const filename: string = 'ibm-credentials.env';

export function readCredentialsFile() {
if (!fs.existsSync) {
logger.warn('Unable to find the library fs.existsSync');
Copy link
Contributor

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

Copy link
Member

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

Copy link
Contributor

@germanattanasio germanattanasio left a 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

Copy link
Member

@dpopp07 dpopp07 left a 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!

@@ -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'
Copy link
Member

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

@jorge-ibm jorge-ibm merged commit 6079ca0 into master Oct 14, 2019
ibm-devx-automation pushed a commit that referenced this pull request Oct 14, 2019
# [1.1.0](v1.0.0...v1.1.0) (2019-10-14)

### Features

* adding debug logger ([#64](#64)) ([6079ca0](6079ca0))
@ibm-devx-automation
Copy link

🎉 This PR is included in version 1.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@jorge-ibm jorge-ibm deleted the add-debug-logger branch October 20, 2019 17:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants