Working with the Logging API

GrahaMc edited this page Aug 29, 2017 · 2 revisions

The SharePoint Framework contains a logging API that can be used to log messages during the lifecycle of your webpart. The API documentation is here - https://sharepoint.github.io/classes/_sp_client_base_.log.html . The logging levels are fairly standard, with Verbose being the least important, followed by Info, Warning and Error.

Here is how you go about using it.

1st - reference the Log class.

import { Log } from '@microsoft/sp-core-library';

2nd - Log your message from your WebPart

Log.verbose("HelloWorld", "Here is a verbose log", this.context.serviceScope);

Log.info("HelloWorld", "Here is an informational message.", this.context.serviceScope);

Log.warn("HelloWorld", "Oh Oh, this might be bad", this.context.serviceScope);

Log.error("HelloWorld", new Error("Oh No!  Error!  Ahhhhhh!!!!"), this.context.serviceScope);

This will result in the following in the debug console Logging Sample

The only real interesting point here is passing in the service scope. If you have access to one (and all WebParts do), you should pass it in to the logger so that it can associate the logging to the component that logged it.

We'll be adding features around logging in the future, particularly with a view that lets you view and filter them nicely.

Clone this wiki locally
You can’t perform that action at this time.
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.
Press h to open a hovercard with more details.