Logging API

The SHIM package provides APIs for the chaincode to create and manage logging objects. The logs generated by these objects are integrated with peer logs.

The chaincode can create and use an arbitrary number of logging objects. Each logging object must have a unique name, which is used to prefix log records in the output and to distinguish the records of different logging objects and the SHIM. (Remember that the logging object name SHIM API is reserved and should not be used in chaincode.) Each logging object has set a logging severity level at which the log records will be sent to the output. Log records with the severity level CRITICAL always appear in the output. The following snippet lists the API functions to create and manage logging objects in the chaincode.

// Creates a new logging object. 
func NewLogger(name string) *ChaincodeLogger 
 
// Converts a case-insensitive string representing a logging level into an element of LoggingLevel enumeration type. 
// This function is used to convert constants of standard GO logging levels (i.e. CRITICAL, ERROR, WARNING, NOTICE, INFO or DEBUG) into the shim's enumeration LoggingLevel type (i.e. LogDebug, LogInfo, LogNotice, LogWarning, LogError, LogCritical). 
func LogLevel(levelString string) (LoggingLevel, error) 
 
// Sets the logging level of the logging object. 
func (c *ChaincodeLogger) SetLevel(level LoggingLevel) 
 
// Returns true if the logging object will generate logs at the given level. 
func (c *ChaincodeLogger) IsEnabledFor(level LoggingLevel) bool 

The logging object ChaincodeLogger provides functions for logging records for each of the severity levels. The following shippet lists the functions of the ChaincodeLogger.

func (c *ChaincodeLogger) Debug(args ...interface{}) 
func (c *ChaincodeLogger) Debugf(format string, args ...interface{}) 
func (c *ChaincodeLogger) Info(args ...interface{}) 
func (c *ChaincodeLogger) Infof(format string, args ...interface{}) 
func (c *ChaincodeLogger) Notice(args ...interface{}) 
func (c *ChaincodeLogger) Noticef(format string, args ...interface{}) 
func (c *ChaincodeLogger) Warning(args ...interface{}) 
func (c *ChaincodeLogger) Warningf(format string, args ...interface{}) 
func (c *ChaincodeLogger) Error(args ...interface{}) 
func (c *ChaincodeLogger) Errorf(format string, args ...interface{}) 
func (c *ChaincodeLogger) Critical(args ...interface{}) 
func (c *ChaincodeLogger) Criticalf(format string, args ...interface{}) 

The default formatting of the records is defined by the configuration of SHIM, which places a space between the printed representations of the input arguments. For each severity level, the logging objects provide an additional function with the suffix f. These functions allow you to control the formatting of the output with the argument format.

The template of an output generated by the logging objects is as follows:

[timestamp] [logger name] [severity level] printed arguments 

The output of all logging objects and of SHIM is combined and sent into the standard error (stderr).

The following code block illustrates an example of creating and using a logging object:

var logger = shim.NewLogger("tradeWorkflow") 
logger.SetLevel(shim.LogDebug) 
 
_, args := stub.GetFunctionAndParameters() 
logger.Debugf("Function: %s(%s)", "requestTrade", strings.Join(args, ",")) 
 
if !authenticateImporterOrg(creatorOrg, creatorCertIssuer) { 
   logger.Info("Caller not a member of Importer Org. Access denied:", creatorOrg, creatorCertIssuer) 
}