Add logger to RICDMS
Added logger to RICDMS for logging purpose.
Issue-ID: RIC-714
Signed-off-by: subhash kumar singh <subh.singh@samsung.com>
Change-Id: Id0b52146038958d4a4d0b4a403931726f61db4c6
diff --git a/config/config-test.yaml b/config/config-test.yaml
new file mode 100644
index 0000000..de9d2ad
--- /dev/null
+++ b/config/config-test.yaml
@@ -0,0 +1,15 @@
+# Copyright (c) 2022 Samsung
+#
+# 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.
+#
+log-level: debug
\ No newline at end of file
diff --git a/config/config.yaml b/config/config.yaml
new file mode 100644
index 0000000..de9d2ad
--- /dev/null
+++ b/config/config.yaml
@@ -0,0 +1,15 @@
+# Copyright (c) 2022 Samsung
+#
+# 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.
+#
+log-level: debug
\ No newline at end of file
diff --git a/go.mod b/go.mod
index f8b1aad..2f87777 100644
--- a/go.mod
+++ b/go.mod
@@ -23,6 +23,7 @@
go 1.17
require (
+ gerrit.o-ran-sc.org/r/com/golog v0.0.0-00010101000000-000000000000
github.com/go-openapi/errors v0.20.2
github.com/go-openapi/loads v0.21.1
github.com/go-openapi/runtime v0.24.1
@@ -31,11 +32,13 @@
github.com/go-openapi/swag v0.21.1
github.com/go-openapi/validate v0.22.0
github.com/jessevdk/go-flags v1.5.0
+ github.com/stretchr/testify v1.7.0
golang.org/x/net v0.0.0-20220630215102-69896b714898
)
require (
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
+ github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/go-openapi/analysis v0.21.2 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
@@ -45,7 +48,12 @@
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/oklog/ulid v1.3.1 // indirect
+ github.com/pmezard/go-difflib v1.0.0 // indirect
go.mongodb.org/mongo-driver v1.8.3 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
+ gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
+ k8s.io/utils v0.0.0-20220812165043-ad590609e2e5 // indirect
)
+
+replace gerrit.o-ran-sc.org/r/com/golog => gerrit.o-ran-sc.org/r/com/golog.git v0.0.2
diff --git a/go.sum b/go.sum
index c3e3a6f..b2801ab 100644
--- a/go.sum
+++ b/go.sum
@@ -1,3 +1,5 @@
+gerrit.o-ran-sc.org/r/com/golog.git v0.0.2 h1:Ix6SgFuzd6yW6Ur6+qDlGhDO65UYs8PiIkeAL1VaQ2o=
+gerrit.o-ran-sc.org/r/com/golog.git v0.0.2/go.mod h1:A7hUL52YQSO4dFIZNcj76XQ09C9PftAe3LyL7kqBnok=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
@@ -10,6 +12,7 @@
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw=
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
+github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas=
github.com/go-openapi/analysis v0.21.2 h1:hXFrOYFHUAMQdu6zwAiKKJHJQ8kqZs1ux/ru1P1wLJU=
github.com/go-openapi/analysis v0.21.2/go.mod h1:HZwRk4RRisyG8vx2Oe6aqeSQcoxRp47Xkp3+K6q+LdY=
github.com/go-openapi/errors v0.19.8/go.mod h1:cM//ZKUKyO06HSwqAelJ5NsEMMcpa6VpXe8DOa1Mi1M=
@@ -117,12 +120,14 @@
github.com/sirupsen/logrus v1.4.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
+github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
+github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
@@ -193,3 +198,6 @@
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+k8s.io/klog/v2 v2.0.0/go.mod h1:PBfzABfn139FHAV07az/IF9Wp1bkk3vpT2XSJ76fSDE=
+k8s.io/utils v0.0.0-20220812165043-ad590609e2e5 h1:XmRqFcQlCy/lKRZ39j+RVpokYNroHPqV3mcBRfnhT5o=
+k8s.io/utils v0.0.0-20220812165043-ad590609e2e5/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
diff --git a/pkg/restful/restful.go b/pkg/restful/restful.go
index 4693f56..2b8dc96 100644
--- a/pkg/restful/restful.go
+++ b/pkg/restful/restful.go
@@ -25,6 +25,7 @@
"gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi"
"gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations"
+ "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/ricdms"
"github.com/go-openapi/loads"
)
@@ -49,6 +50,7 @@
defer server.Shutdown()
server.Port = 8000
server.Host = "0.0.0.0"
+ ricdms.Logger.Info("Starting server at : %s:%d", server.Host, server.Port)
if err := server.Serve(); err != nil {
log.Fatal(err.Error())
}
diff --git a/pkg/ricdms/ricdms.go b/pkg/ricdms/ricdms.go
index d9a3cf5..40c2f9d 100644
--- a/pkg/ricdms/ricdms.go
+++ b/pkg/ricdms/ricdms.go
@@ -20,8 +20,33 @@
package ricdms
+import (
+ "fmt"
+ "os"
+
+ mdclog "gerrit.o-ran-sc.org/r/com/golog"
+)
+
type ricdms struct {
}
+var Logger *mdclog.MdcLogger
+
func Init() {
+ var err error
+ Logger, err = mdclog.InitLogger("ricdms")
+ if err != nil {
+ fmt.Println("Logger not initialized !!")
+ return
+ }
+
+ configFile := os.Getenv("RIC_DMS_CONFIG_FILE")
+
+ if configFile != "" {
+ Logger.ParseFileContent(configFile)
+ Logger.Info("Logger is initialized with config file : %s", configFile)
+ } else {
+ Logger.LevelSet(mdclog.INFO)
+ Logger.Info("Logger is initialized without config file.")
+ }
}
diff --git a/pkg/ricdms/ricdms_test.go b/pkg/ricdms/ricdms_test.go
new file mode 100644
index 0000000..3de790a
--- /dev/null
+++ b/pkg/ricdms/ricdms_test.go
@@ -0,0 +1,43 @@
+//==================================================================================
+// Copyright (c) 2022 Samsung
+//
+// 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.
+//
+// This source code is part of the near-RT RIC (RAN Intelligent Controller)
+// platform project (RICP).
+//==================================================================================
+//
+package ricdms
+
+import (
+ "os"
+ "path"
+ "testing"
+
+ "gerrit.o-ran-sc.org/r/com/golog"
+ "github.com/stretchr/testify/assert"
+)
+
+func TestLoggerWithConfigFile(t *testing.T) {
+ p, _ := os.Getwd()
+ p = path.Join(p, "../../config/config-test.yaml")
+ os.Setenv("RIC_DMS_CONFIG_FILE", p)
+ Init()
+ assert.Equal(t, Logger.LevelGet(), golog.Level(4))
+}
+
+func TestLoggerWithoutConfigFile(t *testing.T) {
+ os.Unsetenv("RIC_DMS_CONFIG_FILE")
+ Init()
+ assert.Equal(t, Logger.LevelGet(), golog.Level(3))
+}