aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastien Douheret <sebastien.douheret@iot.bzh>2017-09-29 10:39:34 +0200
committerSebastien Douheret <sebastien.douheret@iot.bzh>2017-09-29 10:39:34 +0200
commit0839b62390b61af49c9d191dea90b5679be66ffe (patch)
tree636c9de28de6bc48a73f064f8986adb6ae13cf23
parent515f86b3cebffe730285944a9515013d6866bac9 (diff)
Fixed Httpclient logging.v0.1.3
- Allowed to set logger output, level and prefix on creation (part of HTTPClientConfig) - Fixed log level decoding
-rw-r--r--golib/httpclient.go25
1 files changed, 20 insertions, 5 deletions
diff --git a/golib/httpclient.go b/golib/httpclient.go
index eaeb0f6..349aab9 100644
--- a/golib/httpclient.go
+++ b/golib/httpclient.go
@@ -37,8 +37,12 @@ type HTTPClientConfig struct {
Apikey string
HeaderClientKeyName string
CsrfDisable bool
+ LogOut io.Writer
+ LogLevel int
+ LogPrefix string
}
+// Logger levels constants
const (
HTTPLogLevelPanic = 0
HTTPLogLevelError = 1
@@ -62,10 +66,15 @@ func HTTPNewClient(baseURL string, cfg HTTPClientConfig) (*HTTPClient, error) {
},
},
}
+
+ lOut := cfg.LogOut
+ if cfg.LogOut == nil {
+ lOut = os.Stdout
+ }
client := HTTPClient{
- LoggerOut: os.Stdout,
- LoggerLevel: HTTPLogLevelError,
- LoggerPrefix: "",
+ LoggerOut: lOut,
+ LoggerLevel: cfg.LogLevel,
+ LoggerPrefix: cfg.LogPrefix,
httpClient: httpClient,
initDone: false,
@@ -82,13 +91,19 @@ func HTTPNewClient(baseURL string, cfg HTTPClientConfig) (*HTTPClient, error) {
return &client, err
}
+ client.log(HTTPLogLevelDebug, "HTTP client url %s init Done", client.endpoint)
client.initDone = true
return &client, nil
}
// GetLogLevel Get a readable string representing the log level
func (c *HTTPClient) GetLogLevel() string {
- switch c.LoggerLevel {
+ return c.logLevelToString(c.LoggerLevel)
+}
+
+// logLevelToString Convert an integer log level to string
+func (c *HTTPClient) logLevelToString(lvl int) string {
+ switch lvl {
case HTTPLogLevelPanic:
return "panic"
case HTTPLogLevelError:
@@ -126,7 +141,7 @@ func (c *HTTPClient) log(level int, format string, args ...interface{}) {
if level > c.LoggerLevel {
return
}
- sLvl := strings.ToUpper(c.GetLogLevel())
+ sLvl := strings.ToUpper(c.logLevelToString(level))
fmt.Fprintf(c.LoggerOut, sLvl+": "+c.LoggerPrefix+format+"\n", args...)
}