[VOL-2694] Use package specific logger instance in all log statements
Change-Id: Ib33d89bb04750d3f95901f3d80f9d37d336a3e2c
diff --git a/pkg/probe/common.go b/pkg/probe/common.go
new file mode 100644
index 0000000..211419d
--- /dev/null
+++ b/pkg/probe/common.go
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package probe
+
+import (
+ "github.com/opencord/voltha-lib-go/v3/pkg/log"
+)
+
+var logger log.Logger
+
+func init() {
+ // Setup this package so that it's log level can be modified at run time
+ var err error
+ logger, err = log.AddPackage(log.JSON, log.ErrorLevel, log.Fields{"pkg": "probe"})
+ if err != nil {
+ panic(err)
+ }
+}
diff --git a/pkg/probe/probe.go b/pkg/probe/probe.go
index 932c287..e89d5bc 100644
--- a/pkg/probe/probe.go
+++ b/pkg/probe/probe.go
@@ -118,7 +118,7 @@
for _, name := range names {
if _, ok := p.status[name]; !ok {
p.status[name] = ServiceStatusUnknown
- log.Debugw("probe-service-registered", log.Fields{"service-name": name})
+ logger.Debugw("probe-service-registered", log.Fields{"service-name": name})
}
}
@@ -161,7 +161,7 @@
} else {
p.isHealthy = defaultHealthFunc(p.status)
}
- log.Debugw("probe-service-status-updated",
+ logger.Debugw("probe-service-status-updated",
log.Fields{
"service-name": name,
"status": status.String(),
@@ -232,21 +232,21 @@
defer p.mutex.RUnlock()
w.Header().Set("Content-Type", "application/json")
if _, err := w.Write([]byte("{")); err != nil {
- log.Errorw("write-response", log.Fields{"error": err})
+ logger.Errorw("write-response", log.Fields{"error": err})
w.WriteHeader(http.StatusInternalServerError)
return
}
comma := ""
for c, s := range p.status {
if _, err := w.Write([]byte(fmt.Sprintf("%s\"%s\": \"%s\"", comma, c, s.String()))); err != nil {
- log.Errorw("write-response", log.Fields{"error": err})
+ logger.Errorw("write-response", log.Fields{"error": err})
w.WriteHeader(http.StatusInternalServerError)
return
}
comma = ", "
}
if _, err := w.Write([]byte("}")); err != nil {
- log.Errorw("write-response", log.Fields{"error": err})
+ logger.Errorw("write-response", log.Fields{"error": err})
w.WriteHeader(http.StatusInternalServerError)
return
}
@@ -269,7 +269,7 @@
Addr: address,
Handler: mux,
}
- log.Fatal(s.ListenAndServe())
+ logger.Fatal(s.ListenAndServe())
}
func (p *Probe) IsReady() bool {
diff --git a/pkg/probe/probe_test.go b/pkg/probe/probe_test.go
index 93f9a03..77cd6a8 100644
--- a/pkg/probe/probe_test.go
+++ b/pkg/probe/probe_test.go
@@ -18,22 +18,14 @@
import (
"context"
"encoding/json"
+ "github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"
-
- "github.com/opencord/voltha-lib-go/v3/pkg/log"
- "github.com/stretchr/testify/assert"
)
-func init() {
- if _, err := log.AddPackage(log.JSON, log.WarnLevel, nil); err != nil {
- log.Fatalw("adding-log-package", log.Fields{"error": err})
- }
-}
-
func TestServiceStatusString(t *testing.T) {
assert.Equal(t, "Unknown", ServiceStatusUnknown.String(), "ServiceStatusUnknown")
assert.Equal(t, "Preparing", ServiceStatusPreparing.String(), "ServiceStatusPreparing")