@@ -7,9 +7,11 @@ import (
77 "encoding/json"
88 "fmt"
99 "io/ioutil"
10- "log"
1110 "net/http"
11+ "os"
1212
13+ "github.com/apex/log"
14+ "github.com/apex/log/handlers/logfmt"
1315 "github.com/pavel-z1/phpipam-sdk-go/phpipam/session"
1416)
1517
@@ -116,7 +118,7 @@ func newRequestResponse(r *http.Response) *requestResponse {
116118 }
117119 defer r .Body .Close ()
118120 body , err := ioutil .ReadAll (r .Body )
119- log .Printf ("Response Body Debug ................... %s" , body )
121+ log .Debugf ("Response Body Debug ................... %s" , body )
120122 if err != nil {
121123 panic (err )
122124 }
@@ -146,12 +148,12 @@ func (r *Request) Send() error {
146148 switch r .Method {
147149 case "OPTIONS" , "GET" , "POST" , "PUT" , "PATCH" , "DELETE" :
148150 bs , err := json .Marshal (r .Input )
149- log .Printf ("Request Body Debug ................... %s" , bs )
151+ log .Debugf ("Request Body Debug ................... %s" , bs )
150152 if err != nil {
151153 return fmt .Errorf ("Error preparing request data: %s" , err )
152154 }
153155 buf := bytes .NewBuffer (bs )
154- log .Printf ("Request URL Debug ...................Method: %s, UR: %s/%s%s" , r .Method , r .Session .Config .Endpoint , r .Session .Config .AppID , r .URI )
156+ log .Debugf ("Request URL Debug ...................Method: %s, UR: %s/%s%s" , r .Method , r .Session .Config .Endpoint , r .Session .Config .AppID , r .URI )
155157 req , err = http .NewRequest (r .Method , fmt .Sprintf ("%s/%s%s" , r .Session .Config .Endpoint , r .Session .Config .AppID , r .URI ), buf )
156158 req .Header .Add ("Content-Type" , "application/json" )
157159 default :
@@ -196,8 +198,26 @@ func (r *Request) Send() error {
196198
197199// NewRequest creates a new request instance with configuration set.
198200func NewRequest (s * session.Session ) * Request {
201+ log .SetLevel (log .InfoLevel )
202+ log .SetHandler (logfmt .New (os .Stderr ))
203+
204+ env_loglevel := os .Getenv ("PHPIPAMSDK_LOGLEVEL" )
205+ if env_loglevel != "" {
206+ loglevel , err := log .ParseLevel (env_loglevel )
207+ if err == nil {
208+ log .SetLevel (loglevel )
209+ } else {
210+ log .Warnf ("Invalid log level, defaulting to info: %s" , err )
211+ }
212+ }
213+
199214 r := & Request {
200215 Session : s ,
201216 }
202217 return r
203218}
219+
220+ // change logger level, default is info
221+ func SetLevel (level log.Level ) {
222+ log .SetLevel (level )
223+ }
0 commit comments