Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace logrus with zap logger #148

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.4.4-dev
1.5.0
12 changes: 5 additions & 7 deletions consumer/nf_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ func BuildNFInstance(udmContext *udmContext.UDMContext) (profile models.NfProfil
return
}
profile.Ipv4Addresses = append(profile.Ipv4Addresses, udmContext.RegisterIPv4)

return
}

Expand All @@ -63,8 +62,7 @@ var SendRegisterNFInstance = func(nrfUri, nfInstanceId string, profile models.Nf
for {
prof, res, err = client.NFInstanceIDDocumentApi.RegisterNFInstance(context.TODO(), nfInstanceId, profile)
if err != nil || res == nil {
// TODO : add log
fmt.Println(fmt.Errorf("UDM register to NRF Error[%v]", err.Error()))
logger.ConsumerLog.Errorf("UDM register to NRF Error[%v]", err.Error())
time.Sleep(2 * time.Second)
continue
}
Expand Down Expand Up @@ -92,7 +90,7 @@ var SendRegisterNFInstance = func(nrfUri, nfInstanceId string, profile models.Nf
}

func SendDeregisterNFInstance() (problemDetails *models.ProblemDetails, err error) {
logger.ConsumerLog.Infof("Send Deregister NFInstance")
logger.ConsumerLog.Infoln("send Deregister NFInstance")

udmSelf := udmContext.UDM_Self()
// Set client and set url
Expand Down Expand Up @@ -124,7 +122,7 @@ func SendDeregisterNFInstance() (problemDetails *models.ProblemDetails, err erro
}

var SendUpdateNFInstance = func(patchItem []models.PatchItem) (nfProfile models.NfProfile, problemDetails *models.ProblemDetails, err error) {
logger.ConsumerLog.Debugf("Send Update NFInstance")
logger.ConsumerLog.Debugln("send Update NFInstance")

udmSelf := udmContext.UDM_Self()
configuration := Nnrf_NFManagement.NewConfiguration()
Expand Down Expand Up @@ -154,7 +152,7 @@ var SendUpdateNFInstance = func(patchItem []models.PatchItem) (nfProfile models.
}

func SendCreateSubscription(nrfUri string, nrfSubscriptionData models.NrfSubscriptionData) (nrfSubData models.NrfSubscriptionData, problemDetails *models.ProblemDetails, err error) {
logger.ConsumerLog.Debugf("Send Create Subscription")
logger.ConsumerLog.Debugln("send Create Subscription")

// Set client and set url
configuration := Nnrf_NFManagement.NewConfiguration()
Expand Down Expand Up @@ -184,7 +182,7 @@ func SendCreateSubscription(nrfUri string, nrfSubscriptionData models.NrfSubscri
}

func SendRemoveSubscription(subscriptionId string) (problemDetails *models.ProblemDetails, err error) {
logger.ConsumerLog.Infoln("Send Remove Subscription")
logger.ConsumerLog.Infoln("send Remove Subscription")

udmSelf := udmContext.UDM_Self()
// Set client and set url
Expand Down
4 changes: 2 additions & 2 deletions eventexposure/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/gin-gonic/gin"

"github.com/omec-project/udm/logger"
logger_util "github.com/omec-project/util/logger"
utilLogger "github.com/omec-project/util/logger"
)

// Route is the information for every URI.
Expand All @@ -41,7 +41,7 @@ type Routes []Route

// NewRouter returns a new router.
func NewRouter() *gin.Engine {
router := logger_util.NewGinWithLogrus(logger.GinLog)
router := utilLogger.NewGinWithZap(logger.GinLog)
AddService(router)
return router
}
Expand Down
5 changes: 2 additions & 3 deletions factory/udm_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package factory

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -16,7 +15,7 @@ import (
// Webui URL is not set then default Webui URL value is returned
func TestGetDefaultWebuiUrl(t *testing.T) {
if err := InitConfigFactory("udmcfg.yaml"); err != nil {
fmt.Printf("Error in InitConfigFactory: %v\n", err)
t.Logf("error in InitConfigFactory: %v", err)
}
got := UdmConfig.Configuration.WebuiUri
want := "webui:9876"
Expand All @@ -26,7 +25,7 @@ func TestGetDefaultWebuiUrl(t *testing.T) {
// Webui URL is set to a custom value then custom Webui URL is returned
func TestGetCustomWebuiUrl(t *testing.T) {
if err := InitConfigFactory("udmcfg_with_custom_webui_url.yaml"); err != nil {
fmt.Printf("Error in InitConfigFactory: %v\n", err)
t.Logf("error in InitConfigFactory: %v", err)
}
got := UdmConfig.Configuration.WebuiUri
want := "myspecialwebui:9872"
Expand Down
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ go 1.21

require (
github.com/antihax/optional v1.0.0
github.com/antonfisher/nested-logrus-formatter v1.3.1
github.com/gin-gonic/gin v1.10.0
github.com/google/uuid v1.6.0
github.com/omec-project/config5g v1.5.0
github.com/omec-project/openapi v1.3.1
github.com/omec-project/util v1.1.0
github.com/omec-project/util v1.2.1
github.com/prometheus/client_golang v1.20.4
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.9.0
github.com/urfave/cli v1.22.15
go.uber.org/zap v1.27.0
gopkg.in/yaml.v2 v2.4.0
)

Expand Down Expand Up @@ -54,7 +53,6 @@ require (
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/net v0.29.0 // indirect
Expand Down
9 changes: 2 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/antihax/optional v1.0.0 h1:xK2lYat7ZLaVVcIuj82J8kIro4V6kDe0AUDFboUCwcg=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/antonfisher/nested-logrus-formatter v1.3.1 h1:NFJIr+pzwv5QLHTPyKz9UMEoHck02Q9L0FP13b/xSbQ=
github.com/antonfisher/nested-logrus-formatter v1.3.1/go.mod h1:6WTfyWFkBc9+zyBaKIqRrg/KwMqBbodBjgbHjDz7zjA=
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d h1:Byv0BzEl3/e6D5CLfI0j/7hiIEtvGVFPCZ7Ei2oq8iQ=
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
Expand Down Expand Up @@ -80,8 +78,8 @@ github.com/omec-project/config5g v1.5.0 h1:VC7uHIdg4S4F5onYvpFfaEAFBttWwWh6OSnNn
github.com/omec-project/config5g v1.5.0/go.mod h1:yTjqsOjy8KQ8o4AlnPeZfP32CEvlm0kug3bMwZ3IDOQ=
github.com/omec-project/openapi v1.3.1 h1:NCteMRdMtWnMhf1CXYduuLgeu8fEhc/7XO1CiE7fN3Y=
github.com/omec-project/openapi v1.3.1/go.mod h1:cR6Iharp2TLOzEmskQ/EdCVFZnpKh0zTvUSSuyXAYLE=
github.com/omec-project/util v1.1.0 h1:TUuLmzqTLChIEXQlK9g5Ihgmw4FUm/UJnjfu0wT8Gz0=
github.com/omec-project/util v1.1.0/go.mod h1:BEv8nCokB4j0fgAQ6VVkKuQ2PSP3DJMEmz25pFMw5X8=
github.com/omec-project/util v1.2.1 h1:+o5kbnZzKBmbqT4oprVGPgL0jH5b8BvRMfLye5srGQ4=
github.com/omec-project/util v1.2.1/go.mod h1:kUF2LXhmtw+m7Bk5IGOMFLj7CA8VKtilLI/9QBJcfxE=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand All @@ -100,8 +98,6 @@ github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjR
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
Expand Down Expand Up @@ -135,7 +131,6 @@ golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
golang.org/x/oauth2 v0.23.0 h1:PbgcYx2W7i4LvjJWEbf0ngHV6qJYr86PkAV3bXdLEbs=
golang.org/x/oauth2 v0.23.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
Expand Down
8 changes: 4 additions & 4 deletions httpcallback/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (

"github.com/gin-gonic/gin"
"github.com/omec-project/udm/logger"
logger_util "github.com/omec-project/util/logger"
"github.com/sirupsen/logrus"
utilLogger "github.com/omec-project/util/logger"
"go.uber.org/zap"
)

var HttpLog *logrus.Entry
var HttpLog *zap.SugaredLogger

func init() {
HttpLog = logger.HttpLog
Expand All @@ -37,7 +37,7 @@ type Routes []Route

// NewRouter returns a new router
func NewRouter() *gin.Engine {
router := logger_util.NewGinWithLogrus(logger.GinLog)
router := utilLogger.NewGinWithZap(logger.GinLog)
AddService(router)
return router
}
Expand Down
120 changes: 68 additions & 52 deletions logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,74 +1,90 @@
// SPDX-FileCopyrightText: 2021 Open Networking Foundation <info@opennetworking.org>
// Copyright 2019 free5GC.org
// SPDX-FileCopyrightText: 2024 Canonical Ltd.
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2024 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0

package logger

import (
"time"

formatter "github.com/antonfisher/nested-logrus-formatter"
"github.com/sirupsen/logrus"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

var (
log *logrus.Logger
AppLog *logrus.Entry
InitLog *logrus.Entry
CfgLog *logrus.Entry
Handlelog *logrus.Entry
HttpLog *logrus.Entry
UeauLog *logrus.Entry
UecmLog *logrus.Entry
SdmLog *logrus.Entry
PpLog *logrus.Entry
EeLog *logrus.Entry
UtilLog *logrus.Entry
CallbackLog *logrus.Entry
ContextLog *logrus.Entry
ConsumerLog *logrus.Entry
GinLog *logrus.Entry
GrpcLog *logrus.Entry
ProducerLog *logrus.Entry
log *zap.Logger
AppLog *zap.SugaredLogger
InitLog *zap.SugaredLogger
CfgLog *zap.SugaredLogger
Handlelog *zap.SugaredLogger
HttpLog *zap.SugaredLogger
UeauLog *zap.SugaredLogger
UecmLog *zap.SugaredLogger
SdmLog *zap.SugaredLogger
PpLog *zap.SugaredLogger
EeLog *zap.SugaredLogger
UtilLog *zap.SugaredLogger
CallbackLog *zap.SugaredLogger
ContextLog *zap.SugaredLogger
ConsumerLog *zap.SugaredLogger
GinLog *zap.SugaredLogger
GrpcLog *zap.SugaredLogger
ProducerLog *zap.SugaredLogger
atomicLevel zap.AtomicLevel
)

func init() {
log = logrus.New()
log.SetReportCaller(false)
atomicLevel = zap.NewAtomicLevelAt(zap.InfoLevel)
config := zap.Config{
Level: atomicLevel,
Development: false,
Encoding: "console",
EncoderConfig: zap.NewProductionEncoderConfig(),
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stderr"},
}

config.EncoderConfig.TimeKey = "timestamp"
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
config.EncoderConfig.LevelKey = "level"
config.EncoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
config.EncoderConfig.CallerKey = "caller"
config.EncoderConfig.EncodeCaller = zapcore.ShortCallerEncoder
config.EncoderConfig.MessageKey = "message"
config.EncoderConfig.StacktraceKey = ""

log.Formatter = &formatter.Formatter{
TimestampFormat: time.RFC3339,
TrimMessages: true,
NoFieldsSpace: true,
HideKeys: true,
FieldsOrder: []string{"component", "category"},
var err error
log, err = config.Build()
if err != nil {
panic(err)
}

AppLog = log.WithFields(logrus.Fields{"component": "UDM", "category": "App"})
InitLog = log.WithFields(logrus.Fields{"component": "UDM", "category": "Init"})
CfgLog = log.WithFields(logrus.Fields{"component": "UDM", "category": "CFG"})
Handlelog = log.WithFields(logrus.Fields{"component": "UDM", "category": "HDLR"})
HttpLog = log.WithFields(logrus.Fields{"component": "UDM", "category": "HTTP"})
UeauLog = log.WithFields(logrus.Fields{"component": "UDM", "category": "UEAU"})
UecmLog = log.WithFields(logrus.Fields{"component": "UDM", "category": "UECM"})
SdmLog = log.WithFields(logrus.Fields{"component": "UDM", "category": "SDM"})
PpLog = log.WithFields(logrus.Fields{"component": "UDM", "category": "PP"})
EeLog = log.WithFields(logrus.Fields{"component": "UDM", "category": "EE"})
UtilLog = log.WithFields(logrus.Fields{"component": "UDM", "category": "Util"})
CallbackLog = log.WithFields(logrus.Fields{"component": "UDM", "category": "CB"})
ContextLog = log.WithFields(logrus.Fields{"component": "UDM", "category": "CTX"})
ConsumerLog = log.WithFields(logrus.Fields{"component": "UDM", "category": "Consumer"})
GinLog = log.WithFields(logrus.Fields{"component": "UDM", "category": "GIN"})
GrpcLog = log.WithFields(logrus.Fields{"component": "UDM", "category": "GRPC"})
ProducerLog = log.WithFields(logrus.Fields{"component": "UDM", "category": "Producer"})
AppLog = log.Sugar().With("component", "UDM", "category", "App")
InitLog = log.Sugar().With("component", "UDM", "category", "Init")
CfgLog = log.Sugar().With("component", "UDM", "category", "CFG")
Handlelog = log.Sugar().With("component", "UDM", "category", "HDLR")
HttpLog = log.Sugar().With("component", "UDM", "category", "HTTP")
UeauLog = log.Sugar().With("component", "UDM", "category", "UEAU")
UecmLog = log.Sugar().With("component", "UDM", "category", "UECM")
SdmLog = log.Sugar().With("component", "UDM", "category", "SDM")
PpLog = log.Sugar().With("component", "UDM", "category", "PP")
EeLog = log.Sugar().With("component", "UDM", "category", "EE")
UtilLog = log.Sugar().With("component", "UDM", "category", "Util")
CallbackLog = log.Sugar().With("component", "UDM", "category", "CB")
ContextLog = log.Sugar().With("component", "UDM", "category", "CTX")
ConsumerLog = log.Sugar().With("component", "UDM", "category", "Consumer")
GinLog = log.Sugar().With("component", "UDM", "category", "GIN")
GrpcLog = log.Sugar().With("component", "UDM", "category", "GRPC")
ProducerLog = log.Sugar().With("component", "UDM", "category", "Producer")
}

func SetLogLevel(level logrus.Level) {
log.SetLevel(level)
func GetLogger() *zap.Logger {
return log
}

func SetReportCaller(set bool) {
log.SetReportCaller(set)
// SetLogLevel: set the log level (panic|fatal|error|warn|info|debug)
func SetLogLevel(level zapcore.Level) {
InitLog.Infoln("set log level:", level)
atomicLevel.SetLevel(level)
}
12 changes: 6 additions & 6 deletions parameterprovision/api_subscription_data_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ func TestUpdate(t *testing.T) {

router.PATCH("/nudr-dr/v1/:gpsi/pp-data", func(c *gin.Context) {
gpsi := c.Param("gpsi")
fmt.Println("==========CreateEeSubscription - Subscribe==========")
fmt.Println("ueIdentity: ", gpsi)
t.Logf("==========CreateEeSubscription - Subscribe==========")
t.Logf("ueIdentity:", gpsi)
var testppData models.PpData
if err := c.ShouldBindJSON(&testppData); err != nil {
fmt.Println("fake udm server error: ", err.Error())
t.Logf("fake udm server error:", err.Error())
c.JSON(http.StatusInternalServerError, gin.H{})
return
}
fmt.Println("patchItems: ", testppData)
t.Logf("patchItems:", testppData)
c.JSON(http.StatusCreated, gin.H{})
})

Expand All @@ -73,9 +73,9 @@ func TestUpdate(t *testing.T) {
gpsi := "SDM1234"
resp, err := clientAPI.SubscriptionDataUpdateApi.Update(context.Background(), gpsi, ppData)
if err != nil {
fmt.Println(err.Error())
t.Logf(err.Error())
} else {
fmt.Println("resp: ", resp)
t.Logf("resp:", resp)
}
*/
}
4 changes: 2 additions & 2 deletions parameterprovision/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/gin-gonic/gin"

"github.com/omec-project/udm/logger"
logger_util "github.com/omec-project/util/logger"
utilLogger "github.com/omec-project/util/logger"
)

// Route is the information for every URI.
Expand All @@ -41,7 +41,7 @@ type Routes []Route

// NewRouter returns a new router.
func NewRouter() *gin.Engine {
router := logger_util.NewGinWithLogrus(logger.GinLog)
router := utilLogger.NewGinWithZap(logger.GinLog)
AddService(router)
return router
}
Expand Down
Loading