百度智能云

All Product Document

          Object Storage

          sdk Log

          SDK Log

          BOS GO SDK realizes the supporting of log module of 6 levels, 3 outputs (standard output, standard error and file) and basic format setting, with import path github.com/baidubce/bce-sdk-go/util/log. When the output is file, setting of 5 log rolling modes (not rolling, by day, by hour, by minute and by size) is supported, and in this case, directory where the log file is output also needs to be set. For details, please see the sample code.

          Default Log

          BOS GO SDK contains the global log object of package level, which does not record log by default, and if it is needed to output SDK related log, users need to specify the output mode and level, for details, please see the example below:

          // import "github.com/baidubce/bce-sdk-go/util/log" 
          
          // Specify output to standard error, and output level of INFO and above 
          log.SetLogHandler(log.STDERR) 
          log.SetLogLevel(log.INFO) 
          
          // Specify output to standard error and file, DEBUG and above level, and roll by 1GB file size 
          log.SetLogHandler(log.STDERR | log.FILE) 
          log.SetLogDir("/tmp/gosdk-log") 
          log.SetRotateType(log.ROTATE_SIZE) 
          log.SetRotateSize(1 << 30) 
          
          // Output to standard output, and only output level and log message 
          log.SetLogHandler(log.STDOUT) 
          log.SetLogFormat([]string{log.FMT_LEVEL, log.FMT_MSG}) 

          Note: 1. The default output level of log is DEBUG 2. If the output is set to file, the default log output directory is /tmp, and rolled by hour by default. 3. If the output is set to file and rolled by size, the rolling size is 1GB by default. 4. The default log output format is: FMT_LEVEL, FMT_LTIME, FMT_LOCATION, FMT_MSG

          Use of Project

          The log module does not have any external reliance, and users can directly refer to that log module in the project when using GO SDK developing project; users can continue to use the log object of package level used by GO SDK, and also can create a log object, see the example below:

          // Use global log object of package level directly (output together with GO SDK own log) 
          log.SetLogHandler(log.STDERR) 
          log.Debugf("%s", "logging message using the log package in the BOS go sdk") 
          
          // Create a log object (set output log based on customization, separated with GO SDK log output) 
          myLogger := log.NewLogger() 
          myLogger.SetLogHandler(log.FILE) 
          myLogger.SetLogDir("/home/log") 
          myLogger.SetRotateType(log.ROTATE_SIZE) 
          myLogger.Info("this is my own logger from the BOS go sdk") 
          Previous
          Error Processing
          Next
          C++ SDK