Updated on 2024-06-21 GMT+08:00

Log Initialization (SDK for Go)

Function

You can enable the SDK log function to record log information generated during API calling into log files for subsequent data analysis or fault location. You can use InitLog to enable logging, CloseLog to disable logging, and SyncLog to synchronize log information from the cache to log files.

Initialization Method

1
func InitLog(logFullPath string, maxLogSize int64, backups int, level Level, logToConsole bool) error

Parameters

Parameter

Type

Mandatory (Yes/No)

Description

logFullPath

string

Yes

Full path to the log file

maxLogSize

int64

Yes

Log file size in bytes

backups

int

Yes

Maximum number of log files that can be retained

level

Level

Yes

Log level

logToConsole

bool

Yes

Whether to print logs to the console

Value options: true or false

true: Logs are printed to the console.

false: Logs are not printed to the console.

Default value: false

Table 1 Level

Constant

Default Value

Description

LEVEL_OFF

500

Close level. If this level is set, logging will be disabled.

LEVEL_ERROR

400

Error level. If this level is set, only error information will be printed.

LEVEL_WARN

300

Warning level. If this level is set, information about logs at the error level and information about partial critical events will be printed.

LEVEL_INFO

200

Information level. If this level is set, information about logs of the warning level, time consumed for each HTTP/HTTPS request, and time consumed for calling the ObsClient API will be printed.

LEVEL_DEBUG

100

Debugging level. If this level is set, information about logs at the information level, HTTP/HTTPS request and response headers, and stringToSign information calculated by authentication algorithm will be printed.

Code Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Import the dependency package.
import (
    obs "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
)

func main() {
       // Set a path for saving log files.
       var logFullPath string = "./logs/OBS-SDK.log"
       // Set a size (in bytes) for each log file.
       var maxLogSize int64 = 1024 * 1024 * 10
       // Set the number of retained log files.
       var backups int = 10
       // Set a log level.
       var level = obs.LEVEL_INFO
       // Specify whether to print logs to OBS Console.
       var logToConsole bool = false
       // Enable logging.
       obs.InitLog(logFullPath, maxLogSize, backups, level, logToConsole)
       // Disable logging and synchronize cached data to log files.
       obs.CloseLog()

}
  • The logging function is disabled by default. You need to enable it manually.
  • For details about SDK logs, see Log Analysis.
  • By default, logs are written to the cache (then written to log files after logs are accumulated to a certain amount). You can call obs.CloseLog() to forcibly synchronize the log information from the cache to log files.