The logging levels are pre-defined.
Navigate to the above created directory.
Run Yeoman SharePoint Generator to create the solution.
Yeoman generator will present you with the wizard by asking questions about the solution to be created.
Solution Name
Hit Enter to have a default name (spfx-logging in this case) or type in any other name for your solution.
Selected choice - Hit Enter.
Target for component
Here, we can select the target environment where we are planning to deploy the client webpart, i.e., SharePoint Online or SharePoint OnPremise (SharePoint 2016 onwards).
Selected choice - SharePoint Online only (latest)
Place of files
We may choose to use the same folder or create a subfolder for our solution.
Selected choice - Same folder
Deployment option
Selecting Y will allow the app to be deployed instantly to all sites and will be accessible everywhere.
Selected choice - N (install on each site explicitly)
Type of client-side component to create
We can choose to create client side webpart or an extension. Choose the webpart option.
Selected choice - WebPart
Web part name
Hit Enter to select the default name or type in any other name.
Selected choice - SPFxLogger
Web part description
Hit enter to select the default description or type in any other value.
Selected choice - Logging with SPFx
Framework to use
Select any JavaScript framework to develop the component. Available choices are - No JavaScript Framework, React, and Knockout.
Selected choice - No JavaScript Framework
Yeoman generator will perform scaffolding process to generate the solution. The scaffolding process will take a significant amount of time. Once the scaffolding process is completed, lock down the version of project dependencies by running the below command.
In the command prompt, type the below command to open the solution in code editor of your choice.
Working with Logging API
SharePoint Framework supports logging out of the box. Use the below import for logs.
- import { Log } from '@microsoft/sp-core-library';
Define our log source.
- const LOG_SOURCE: string = 'SPFxLogger';
Log the messages from web part.
- @override
- public onInit(): Promise<void> {
- Log.info(LOG_SOURCE, 'Hello world from SPFx Logger');
- Log.info(LOG_SOURCE, JSON.stringify(this.properties, undefined, 2));
- Log.info(LOG_SOURCE, `Access the strings as "${strings.BasicGroupName}"`);
- return Promise.resolve<void>();
- }
The Log class offers 4 methods to log the information, each of which represents corresponding log level – verbose, info, warn, and error.
The first parameter is logging location. By default, it is the name of the web part but can be overridden. The next parameter is the message to be logged. Optionally, we can specify the service scope.
While debugging the SPFx web part, the log message should appear in the console window of the browser.
Summary
SharePoint Framework out of the box provides an API to log information in your solutions. Use the logs effectively to get the benefits out of it at the time of investigating the cause of the error.