Enable MSAL logging


Photo by Tadas Sar on Unsplash

Today I wanted to share how to enable logging in MSAL JS library. It’s very easy to miss this because it’s not part a basic documentation and it’s hidden in the advanced topics with configurations for .NET and iOS.

But I really encourage you to enable verbose logging in MSAL at least for lower environments because out of the box you receive a lot of useful information:

  • Error’s logging. Everything goes wrong reported directly to your console which is very handy;
  • Information about token requests, cache hits and cache misses. Very useful to track how often you request tokens, when you request several tokens simultaneously (in some cases may lead to cache misses and redundant requests);
  • Information about obsolete API calls;
Verbose logging

As you can see this log contains a lot of details and simplifies debugging a lot. Now let’s jump right to the code snippet. Basic configuration I’ve described in the previous blog post (I’m trying to keep it up to date), so now let’s extend this configuration to enable logging:

Extending configuration to enable logging

The first line with the condition is a pseudo code but I don’t want to remove it from the sample to highlight that I don’t recommend to enable intensive logging on production because it affects the end users. For production I’d rather go with the approach to send only errors to the servers (e.g. Google Analytics) and that’s it.

Conclusion

Enabling logging is very important especially for the cases where you are working with multiple tokens on the same page. It helped me a lot to debug the behaviour of the MSAL library. And I hope it’ll help you, too!


Thanks a lot for reading! I hope you’ve enjoyed. If you find this material useful, don’t forget to subscribe and share with your colleagues and friends! Thanks!

One thought on “Enable MSAL logging

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s