Skip to content

Working with the Logging API

Vesa Juvonen edited this page Nov 28, 2019 · 3 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://docs.microsoft.com/en-us/javascript/api/sp-core-library/log?view=sp-typescript-latest. 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