github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/examples/logging/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/hashgraph/hedera-sdk-go/v2"
     6  	"os"
     7  )
     8  
     9  func main() {
    10  	var client *hedera.Client
    11  	var err error
    12  
    13  	// Retrieving network type from environment variable HEDERA_NETWORK
    14  	client, err = hedera.ClientForName(os.Getenv("HEDERA_NETWORK"))
    15  	if err != nil {
    16  		panic(fmt.Sprintf("%v : error creating client", err))
    17  	}
    18  
    19  	// The client comes with default logger.
    20  	// We can also set a custom logger for this client.
    21  	// The logger must implement the hedera.Logger interface.
    22  	logger := hedera.NewLogger("Hedera sdk", hedera.LoggerLevelDebug)
    23  	client.SetLogger(logger)
    24  
    25  	// Set the logging level fot this client, to be used as default.
    26  	// Individual log levels can be set for the Query or Transaction object by
    27  	// chaining the SetLogLevel() function on the given Transaction or Query object.
    28  	client.SetLogLevel(hedera.LoggerLevelTrace)
    29  
    30  	// Retrieving operator ID from environment variable OPERATOR_ID
    31  	operatorAccountID, err := hedera.AccountIDFromString(os.Getenv("OPERATOR_ID"))
    32  	if err != nil {
    33  		panic(fmt.Sprintf("%v : error converting string to AccountID", err))
    34  	}
    35  
    36  	// Retrieving operator key from environment variable OPERATOR_KEY
    37  	operatorKey, err := hedera.PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
    38  	if err != nil {
    39  		panic(fmt.Sprintf("%v : error converting string to PrivateKey", err))
    40  	}
    41  
    42  	// Setting the client operator ID and key
    43  	client.SetOperator(operatorAccountID, operatorKey)
    44  
    45  	// Generate new key to use with new account
    46  	newKey, err := hedera.GeneratePrivateKey()
    47  	if err != nil {
    48  		panic(fmt.Sprintf("%v : error generating PrivateKey}", err))
    49  	}
    50  
    51  	fmt.Printf("private = %v\n", newKey)
    52  	fmt.Printf("public = %v\n", newKey.PublicKey())
    53  
    54  	// Transaction used to show default logging functionality from client
    55  	_, err = hedera.NewAccountCreateTransaction().
    56  		SetKey(newKey.PublicKey()).
    57  		Execute(client)
    58  	if err != nil {
    59  		panic(fmt.Sprintf("%v : error executing account create transaction}", err))
    60  	}
    61  
    62  	// Disable default logging on client, to show logging functionality from transaction
    63  	client.SetLogLevel(hedera.LoggerLevelDisabled)
    64  
    65  	// Create account transaction used to show logging functionality
    66  	_, err = hedera.NewAccountCreateTransaction().
    67  		SetKey(newKey.PublicKey()).
    68  		// Set logging level for the specific transaction
    69  		SetLogLevel(hedera.LoggerLevelTrace).
    70  		Execute(client)
    71  	if err != nil {
    72  		panic(fmt.Sprintf("%v : error executing account create transaction}", err))
    73  	}
    74  }