Skip to content

Commit

Permalink
Feature: Add zap logger to support more logging function
Browse files Browse the repository at this point in the history
  • Loading branch information
eynzhang committed May 11, 2020
1 parent 527b5f9 commit 7966bd7
Show file tree
Hide file tree
Showing 24 changed files with 491 additions and 431 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ This is the folder and package structure of SDK source code and the description
- **gzip**: it provide the gzip decompress functionality that unzip the websocket binary data
- **model**: The internal data model
- **requestbuilder**: Responsible to build the request with the signature
- **config**: it stores the common configuration, such as host, access key.
- **logging**: It provides the logging function
- **config**: It stores the common configuration, such as host, access key.
- **cmd**: The main package is defined here, it provides the examples how to use **client** package and **response** package to access API and read response.

As the example indicates, there are two important namespaces: **client** and **response**, this section will introduce both of them below.
Expand Down Expand Up @@ -201,6 +202,16 @@ for _, kline := range resp {

Golang is not a pure object oriented programming language, and there is no native constructor. In this SDK, every struct has an ***Init*** function for each struct, you must call Init function first, otherwise the member variables may not be initialized expected.

###Logging

This SDK uses the high performance logging library [zap](https://github.com/uber-go/zap), which provide different kind of loggers. To better support format message, this SDK uses the SugaredLogger, and wrapped a few interfaces in package *logging/applogger*. It has below features:

1. Logging target is console (In the future we will support output to file)
2. Support multiple levels (Fatal, Error, Panic, Warn, Info and Debug) and minimum log level
3. Support colorful text (by default)

You can customize your own logging by updating *applogger.go* file.

## Request Examples

### Common data
Expand Down
33 changes: 11 additions & 22 deletions cmd/accountwebsocketclientexample/accountwebsocketclientexample.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package accountwebsocketclientexample
import (
"fmt"
"github.com/huobirdcenter/huobi_golang/config"
"github.com/huobirdcenter/huobi_golang/logging/applogger"
"github.com/huobirdcenter/huobi_golang/pkg/client/accountwebsocketclient"
"github.com/huobirdcenter/huobi_golang/pkg/response/account"
"github.com/huobirdcenter/huobi_golang/pkg/response/auth"
Expand Down Expand Up @@ -136,14 +137,9 @@ func subAccountUpdateV2() {
// Authentication response handler
func(resp *auth.WebSocketV2AuthenticationResponse) {
if resp.IsSuccess() {
err := client.Subscribe("1", "1149")
if err != nil {
fmt.Printf("Subscribe error: %s\n", err)
} else {
fmt.Println("Sent subscription")
}
client.Subscribe("1", "1149")
} else {
fmt.Printf("Authentication error, code: %d, message:%s\n", resp.Code, resp.Message)
applogger.Error("Authentication error, code: %d, message:%s", resp.Code, resp.Message)
}
},
// Response handler
Expand All @@ -152,42 +148,35 @@ func subAccountUpdateV2() {
if ok {
if subResponse.Action == "sub" {
if subResponse.IsSuccess() {
fmt.Printf("Subscription topic %s successfully\n", subResponse.Ch)
applogger.Info("Subscription topic %s successfully", subResponse.Ch)
} else {
fmt.Printf("Subscription topic %s error, code: %d, message: %s\n", subResponse.Ch, subResponse.Code, subResponse.Message)
applogger.Error("Subscription topic %s error, code: %d, message: %s", subResponse.Ch, subResponse.Code, subResponse.Message)
}
} else if subResponse.Action == "push" {
if subResponse.Data != nil {
b := subResponse.Data
if b.ChangeTime == 0 {
fmt.Printf("Account overview, id: %d, currency: %s, balance: %s\n", b.AccountId, b.Currency, b.Balance)
applogger.Info("Account overview, id: %d, currency: %s, balance: %s", b.AccountId, b.Currency, b.Balance)
} else {
fmt.Printf("Account update, id: %d, currency: %s, balance: %s, time: %d\n", b.AccountId, b.Currency, b.Balance, b.ChangeTime)
applogger.Info("Account update, id: %d, currency: %s, balance: %s, time: %d", b.AccountId, b.Currency, b.Balance, b.ChangeTime)
}
}
}
} else {
fmt.Printf("Received unknown response: %v\n", resp)
applogger.Error("Received unknown response: %v", resp)
}
})

// Connect to the server and wait for the handler to handle the response
err := client.Connect(true)
if err != nil {
fmt.Printf("Client Connect error: %s\n", err)
return
}
client.Connect(true)

fmt.Println("Press ENTER to unsubscribe and stop...")
fmt.Scanln()

// Unsubscribe the topic
err = client.UnSubscribe("1", "1250")
if err != nil {
fmt.Printf("UnSubscribe error: %s\n", err)
}
client.UnSubscribe("1", "1250")

// Close the connection
client.Close()
fmt.Println("Client closed")
applogger.Info("Client closed")
}
6 changes: 3 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/huobirdcenter/huobi_golang/cmd/orderclientexample"
"github.com/huobirdcenter/huobi_golang/cmd/orderwebsocketclientexample"
"github.com/huobirdcenter/huobi_golang/cmd/walletclientexample"
"github.com/huobirdcenter/huobi_golang/logging"
"github.com/huobirdcenter/huobi_golang/logging/perflogger"
)

func main() {
Expand All @@ -35,8 +35,8 @@ func runAll() {
}

// Run performance test
func perfTest() {
logging.EnablePerformanceLog(true)
func runPerfTest() {
perflogger.Enable(true)

commonclientexample.RunAllExamples()
accountclientexample.RunAllExamples()
Expand Down
Loading

0 comments on commit 7966bd7

Please sign in to comment.