Skip to content
This repository has been archived by the owner on Apr 11, 2019. It is now read-only.

Commit

Permalink
Add logger.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekSi committed Mar 11, 2013
1 parent df96435 commit 019ce5a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
20 changes: 14 additions & 6 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"sync/atomic"
)
Expand Down Expand Up @@ -54,25 +55,32 @@ func (e *ExpectedMore) Error() string {
}

type API struct {
Auth string
url string
c http.Client
id int32
Auth string
Logger *log.Logger
url string
c http.Client
id int32
}

// URL like http://user:password@host/api_jsonrpc.php
func NewAPI(url string) (api *API) {
return &API{url: url, c: http.Client{}}
}

func (api *API) printf(format string, v ...interface{}) {
if api.Logger != nil {
api.Logger.Printf(format, v...)
}
}

func (api *API) callBytes(method string, params interface{}) (b []byte, err error) {
id := atomic.AddInt32(&api.id, 1)
jsonobj := request{"2.0", method, params, api.Auth, id}
b, err = json.Marshal(jsonobj)
if err != nil {
return
}
// log.Printf("Req: %s", b)
api.printf("Request : %s", b)

req, err := http.NewRequest("POST", api.url, bytes.NewReader(b))
if err != nil {
Expand All @@ -89,7 +97,7 @@ func (api *API) callBytes(method string, params interface{}) (b []byte, err erro
defer res.Body.Close()

b, err = ioutil.ReadAll(res.Body)
// log.Printf("Res: %s", b)
api.printf("Response: %s", b)
return
}

Expand Down
1 change: 1 addition & 0 deletions base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func getAPI(t *testing.T) *API {

url, user, password := os.Getenv("TEST_ZABBIX_URL"), os.Getenv("TEST_ZABBIX_USER"), os.Getenv("TEST_ZABBIX_PASSWORD")
_api = NewAPI(url)
_api.Logger = log.New(os.Stderr, "[zabbix] ", 0)

if user != "" {
auth, err := _api.Login(user, password)
Expand Down

0 comments on commit 019ce5a

Please sign in to comment.