Updated on 2023-11-09 GMT+08:00

Log Analysis

Log Configuration

OBS BrowserJS SDK provides the logging function. You can call ObsClient.initLog to enable and configure logging. Sample code is as follows:

// Create an ObsClient instance.
var obsClient = new ObsClient({
    // Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables AccessKeyID and SecretAccessKey.
    // The front-end code does not have the process environment variable, so you need to use a module bundler like webpack to define the process variable.
    // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.    access_key_id: process.env.AccessKeyID,
    secret_access_key: process.env.SecretAccessKey,
    //CN-Hong Kong region is used here as an example. Replace it with the one in your actual situation.
    server: 'https://obs.ap-southeast-1.myhuaweicloud.com'
    });

obsClient.initLog({
       level:'warn', // Set the log level.
});
  • Logs printed by the SDK will be displayed in the Console of the developer tool provided by the browser.
  • The logging function is disabled by default. You need to enable it if needed.

Log Format

The SDK log format is: Log time|log level|log content. The following are example logs:

2018/2/11 PM 9:22:45|info|ListObjects|enter ListObjects...
2018/2/11 PM 9:22:45|info|ListObjects|prepare request parameters ok,then Send request to service start
2018/2/11 PM 9:22:45|info|ListObjects|http cost 19 ms
2018/2/11 PM 9:22:45|info|ListObjects|get response start, statusCode:200

Log Level

When current logs cannot be used to troubleshoot system faults, you can change the log level to obtain more information. You can obtain the most information in debug logs and the least information in error logs.

Log level description:

  • debug: Debugging level. If this level is set, all logs will be printed.
  • info: Information level. If this level is set, information about logs of the warn level and time consumed for each HTTP/HTTPS request will be printed.
  • warn: Warning level. If this level is set, information about logs at the error level and information about partial critical events will be printed.
  • error: Error level. If this level is set, only error information will be printed.