Overview
SharePoint web parts follow their own lifecycle while executing the user requests. Everything looks normal when requests go well and web part performs the user interactions (without any error). The awkward moment comes when any of the user requests fail and the web part starts displaying an error message.
SharePoint Framework (SPFx) also has no exception to it and supports logging APIs which can be used during the lifecycle of the web part.
Logging Overview
The logging levels are pre-defined.
| Logging Level | Definition |
| Verbose | Corresponds to lengthy events. Logs almost everything. |
| Information | Does not require any attention. However, they provide valuable data for monitoring the state of solution. |
| Warning | Indicates potential problem or issue that might need attention. We should monitor the pattern over time. If ignored, warning may result in sever error. |
| Error | Requires urgent attention. All error events should be investigated. |
| Critical | Indicates serious error that has caused major failure in the solution |
| None | No logging occurs |
Verbose is least important followed by information, warning and error.
Create SPFx Solution
Open the command prompt. Create a directory for SPFx solution.
- md spfx-logging
- cd spfx-logging
- yo @microsoft/sharepoint

Hit Enter to have a default name (spfx-logging in this case) or type in any other name for your solution.
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).
We may choose to use the same folder or create a subfolder for our solution.
Selecting Y will allow the app to be deployed instantly to all sites and will be accessible everywhere.
We can choose to create client side webpart or an extension. Choose the webpart option.
Hit Enter to select the default name or type in any other name.
Hit enter to select the default description or type in any other value.
Select any JavaScript framework to develop the component. Available choices are - No JavaScript Framework, React, and Knockout.
- npm shrinkwrap
- code .
Working with Logging API
- import { Log } from '@microsoft/sp-core-library';
- const LOG_SOURCE: string = 'SPFxLogger';
- @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 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.

Rayees KuniyilPosted Sep 26, 2018, 5:38 AM
Thanks for sharing...
Prasham SabadraPosted Sep 7, 2018, 11:08 PM
Also we can have retention policy or flow to list to archive /delete old errors
Prasham SabadraPosted Sep 7, 2018, 11:07 PM
Thanks for sharing Nanddeep. One good option I also look is having SharePoint list and logging important message in it. Filtering in a list very easy.
Hadshana KamalanathanPosted Sep 7, 2018, 10:53 AM
Thanks for sharing...