Skip to content

Commit

Permalink
Replace logrus with zap logger (#314)
Browse files Browse the repository at this point in the history
* Replace `logrus` with `zap` logger

Signed-off-by: Arrobo, Gabriel <gabriel.arrobo@intel.com>

* Address `lint` issues

Signed-off-by: Arrobo, Gabriel <gabriel.arrobo@intel.com>

---------

Signed-off-by: Arrobo, Gabriel <gabriel.arrobo@intel.com>
  • Loading branch information
gab-arrobo authored Oct 3, 2024
1 parent 0da78a8 commit 9adbe9b
Show file tree
Hide file tree
Showing 38 changed files with 1,080 additions and 1,152 deletions.
20 changes: 0 additions & 20 deletions .fossa.yml

This file was deleted.

2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.4.6-dev
1.5.0
4 changes: 2 additions & 2 deletions amf.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import (

"github.com/omec-project/amf/logger"
"github.com/omec-project/amf/service"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"go.uber.org/zap"
)

var AMF = &service.AMF{}

var appLog *logrus.Entry
var appLog *zap.SugaredLogger

func init() {
appLog = logger.AppLog
Expand Down
16 changes: 8 additions & 8 deletions amf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ package main

import (
"encoding/json"
"fmt"
"log"
"os"
"testing"
"time"
Expand All @@ -27,10 +27,10 @@ var AMFTest = &service.AMF{}

func init() {
if err := os.Setenv("POD_IP", "127.0.0.1"); err != nil {
fmt.Printf("Could not set env POD_IP: %+v\n", err)
log.Printf("Could not set env POD_IP: %+v", err)
}
if err := factory.InitConfigFactory("amfTest/amfcfg.yaml"); err != nil {
fmt.Printf("Could not InitConfigFactory: %+v\n", err)
log.Printf("Could not InitConfigFactory: %+v", err)
}
}

Expand Down Expand Up @@ -72,7 +72,7 @@ func TestInitialConfig(t *testing.T) {
if factory.AmfConfig.Configuration.PlmnSupportList != nil &&
factory.AmfConfig.Configuration.ServedGumaiList != nil &&
factory.AmfConfig.Configuration.SupportTAIList != nil {
fmt.Printf("test passed")
t.Logf("test passed")
} else {
t.Errorf("test failed")
}
Expand All @@ -88,7 +88,7 @@ var Data = []byte(`{
"Site": {
"SiteName": "siteOne",
"Gnb": [
{"Name": "gnb1", "Tac": 1},
{"Name": "gnb1", "Tac": 1},
{"Name": "gnb2", "Tac": 2}
],
"Plmn": {"mcc": "208", "mnc": "93"}
Expand All @@ -112,7 +112,7 @@ func TestUpdateConfig(t *testing.T) {

time.Sleep(2 * time.Second)
if len(factory.AmfConfig.Configuration.SupportTAIList) == 2 {
fmt.Printf("test passed")
t.Logf("test passed")
} else {
t.Errorf("test failed")
}
Expand All @@ -128,12 +128,12 @@ func TestRegisterNF(t *testing.T) {
// consumer.SendSearchNFInstances = origSearchNFInstances
consumer.SendUpdateNFInstance = origUpdateNFInstance
}()
fmt.Printf("test case TestRegisterNF \n")
t.Logf("test case TestRegisterNF")
var prof models.NfProfile
consumer.SendRegisterNFInstance = func(nrfUri string, nfInstanceId string, profile models.NfProfile) (models.NfProfile, string, string, error) {
prof = profile
prof.HeartBeatTimer = 1
fmt.Printf("Test RegisterNFInstance called\n")
t.Logf("Test RegisterNFInstance called")
return prof, "", "", nil
}
/*consumer.SendSearchNFInstances = func(nrfUri string, targetNfType, requestNfType models.NfType, param Nnrf_NFDiscovery.SearchNFInstancesParamOpts) (*models.SearchResult, error) {
Expand Down
6 changes: 3 additions & 3 deletions communication/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import (
"strings"

"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"go.uber.org/zap"

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

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

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

// NewRouter returns a new router.
func NewRouter() *gin.Engine {
router := utilLogger.NewGinWithLogrus(logger.GinLog)
router := utilLogger.NewGinWithZap(logger.GinLog)
AddService(router)
return router
}
Expand Down
7 changes: 3 additions & 4 deletions consumer/nf_mangement.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,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("AMF register to NRF Error[%s]", err.Error()))
logger.ConsumerLog.Errorf("AMF register to NRF Error[%s]", err.Error())
time.Sleep(2 * time.Second)
continue
}
Expand All @@ -107,8 +106,8 @@ var SendRegisterNFInstance = func(nrfUri, nfInstanceId string, profile models.Nf
retrieveNfInstanceId = resourceUri[strings.LastIndex(resourceUri, "/")+1:]
break
} else {
fmt.Println(fmt.Errorf("handler returned wrong status code %d", status))
fmt.Println(fmt.Errorf("NRF return wrong status code %d", status))
logger.ConsumerLog.Errorf("handler returned wrong status code %d", status)
logger.ConsumerLog.Errorf("NRF return wrong status code %d", status)
}
}
return prof, resouceNrfUri, retrieveNfInstanceId, err
Expand Down
16 changes: 8 additions & 8 deletions context/amf_ran.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import (
"github.com/omec-project/amf/logger"
"github.com/omec-project/amf/metrics"
"github.com/omec-project/amf/protos/sdcoreAmfServer"
mi "github.com/omec-project/metricfunc/pkg/metricinfo"
"github.com/omec-project/ngap/ngapConvert"
"github.com/omec-project/ngap/ngapType"
"github.com/omec-project/openapi/models"
"github.com/sirupsen/logrus"
mi "github.com/omec-project/util/metricinfo"
"go.uber.org/zap"
)

const (
Expand Down Expand Up @@ -48,7 +48,7 @@ type AmfRan struct {

Amf2RanMsgChan chan *sdcoreAmfServer.AmfMessage `json:"-"`
/* logger */
Log *logrus.Entry `json:"-"`
Log *zap.SugaredLogger `json:"-"`
}

type SupportedTAI struct {
Expand Down Expand Up @@ -76,12 +76,12 @@ func (ran *AmfRan) Remove() {
}
if *factory.AmfConfig.Configuration.KafkaInfo.EnableKafka {
if err := metrics.StatWriter.PublishNfStatusEvent(gnbStatus); err != nil {
ran.Log.Errorf("Could not publish NfStatusEvent: %v", err)
ran.Log.Errorf("could not publish NfStatusEvent: %v", err)
}
}

ran.SetRanStats(RanDisconnected)
ran.Log.Infof("Remove RAN Context[ID: %+v]", ran.RanID())
ran.Log.Infof("remove RAN Context[ID: %+v]", ran.RanID())
ran.RemoveAllUeInRan()
if AMF_Self().EnableSctpLb {
if ran.GnbId != "" {
Expand All @@ -97,13 +97,13 @@ func (ran *AmfRan) NewRanUe(ranUeNgapID int64) (*RanUe, error) {
self := AMF_Self()
amfUeNgapID, err := self.AllocateAmfUeNgapID()
if err != nil {
ran.Log.Errorln("Alloc Amf ue ngap id failed", err)
ran.Log.Errorln("alloc Amf ue ngap id failed", err)
return nil, fmt.Errorf("allocate AMF UE NGAP ID error: %+v", err)
}
ranUe.AmfUeNgapId = amfUeNgapID
ranUe.RanUeNgapId = ranUeNgapID
ranUe.Ran = ran
ranUe.Log = ran.Log.WithField(logger.FieldAmfUeNgapID, fmt.Sprintf("AMF_UE_NGAP_ID:%d", ranUe.AmfUeNgapId))
ranUe.Log = ran.Log.With(logger.FieldAmfUeNgapID, fmt.Sprintf("AMF_UE_NGAP_ID:%d", ranUe.AmfUeNgapId))
ran.RanUeList = append(ran.RanUeList, &ranUe)
self.RanUePool.Store(ranUe.AmfUeNgapId, &ranUe)
return &ranUe, nil
Expand All @@ -124,7 +124,7 @@ func (ran *AmfRan) RanUeFindByRanUeNgapIDLocal(ranUeNgapID int64) *RanUe {
return ranUe
}
}
ran.Log.Infof("RanUe is not exist")
ran.Log.Infof("RanUe does not exist")
return nil
}

Expand Down
20 changes: 10 additions & 10 deletions context/amf_ue.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ import (
"github.com/omec-project/amf/logger"
"github.com/omec-project/amf/metrics"
"github.com/omec-project/amf/protos/sdcoreAmfServer"
mi "github.com/omec-project/metricfunc/pkg/metricinfo"
"github.com/omec-project/nas/nasMessage"
"github.com/omec-project/nas/nasType"
"github.com/omec-project/nas/security"
"github.com/omec-project/ngap/ngapType"
"github.com/omec-project/openapi/models"
"github.com/omec-project/util/fsm"
"github.com/omec-project/util/idgenerator"
mi "github.com/omec-project/util/metricinfo"
"github.com/omec-project/util/ueauth"
"github.com/sirupsen/logrus"
"go.uber.org/zap"
)

type OnGoingProcedure string
Expand Down Expand Up @@ -211,10 +211,10 @@ type AmfUe struct {
// GmmLog *logrus.Entry `json:"gmmLog,omitempty" yaml:"gmmLog" bson:"gmmLog,omitempty"`
// TxLog *logrus.Entry `json:"txLog,omitempty" yaml:"txLog" bson:"txLog,omitempty"`
// ProducerLog *logrus.Entry `json:"producerLog,omitempty" yaml:"producerLog" bson:"producerLog,omitempty"`
NASLog *logrus.Entry `json:"-"`
GmmLog *logrus.Entry `json:"-"`
TxLog *logrus.Entry `json:"-"`
ProducerLog *logrus.Entry `json:"-"`
NASLog *zap.SugaredLogger `json:"-"`
GmmLog *zap.SugaredLogger `json:"-"`
TxLog *zap.SugaredLogger `json:"-"`
ProducerLog *zap.SugaredLogger `json:"-"`
}

func (ue *AmfUe) MarshalJSON() ([]byte, error) {
Expand Down Expand Up @@ -317,7 +317,7 @@ func (ue *AmfUe) UnmarshalJSON(data []byte) error {
}
ue.RanUe[index].RanUeNgapId = aux.RanUeNgapId
ue.RanUe[index].AmfUeNgapId = aux.AmfUeNgapId
ue.RanUe[index].Log = logger.NgapLog.WithField(logger.FieldAmfUeNgapID, fmt.Sprintf("AMF_UE_NGAP_ID:%d", ue.RanUe[index].AmfUeNgapId))
ue.RanUe[index].Log = logger.NgapLog.With(logger.FieldAmfUeNgapID, fmt.Sprintf("AMF_UE_NGAP_ID:%d", ue.RanUe[index].AmfUeNgapId))
if ran != nil {
// ran.RanUeList = append(ran.RanUeList, ue.RanUe[index])
ue.RanUe[index].Ran = ran
Expand Down Expand Up @@ -518,9 +518,9 @@ func (ue *AmfUe) AttachRanUe(ranUe *RanUe) {
}()

// set log information
ue.NASLog = logger.NasLog.WithField(logger.FieldAmfUeNgapID, fmt.Sprintf("AMF_UE_NGAP_ID:%d", ranUe.AmfUeNgapId))
ue.GmmLog = logger.GmmLog.WithField(logger.FieldAmfUeNgapID, fmt.Sprintf("AMF_UE_NGAP_ID:%d", ranUe.AmfUeNgapId))
ue.TxLog = logger.GmmLog.WithField(logger.FieldAmfUeNgapID, fmt.Sprintf("AMF_UE_NGAP_ID:%d", ranUe.AmfUeNgapId))
ue.NASLog = logger.NasLog.With(logger.FieldAmfUeNgapID, fmt.Sprintf("AMF_UE_NGAP_ID:%d", ranUe.AmfUeNgapId))
ue.GmmLog = logger.GmmLog.With(logger.FieldAmfUeNgapID, fmt.Sprintf("AMF_UE_NGAP_ID:%d", ranUe.AmfUeNgapId))
ue.TxLog = logger.GmmLog.With(logger.FieldAmfUeNgapID, fmt.Sprintf("AMF_UE_NGAP_ID:%d", ranUe.AmfUeNgapId))
}

func (ue *AmfUe) GetAnType() models.AccessType {
Expand Down
6 changes: 3 additions & 3 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ func (context *AMFContext) NewAmfRan(conn net.Conn) *AmfRan {
ran.SupportedTAList = NewSupportedTAIList()
ran.Conn = conn
ran.GnbIp = conn.RemoteAddr().String()
ran.Log = logger.NgapLog.WithField(logger.FieldRanAddr, conn.RemoteAddr().String())
ran.Log = logger.NgapLog.With(logger.FieldRanAddr, conn.RemoteAddr().String())
context.AmfRanPool.Store(conn, &ran)
return &ran
}
Expand All @@ -389,7 +389,7 @@ func (context *AMFContext) NewAmfRanAddr(remoteAddr string) *AmfRan {
ran := AmfRan{}
ran.SupportedTAList = NewSupportedTAIList()
ran.GnbIp = remoteAddr
ran.Log = logger.NgapLog.WithField(logger.FieldRanAddr, remoteAddr)
ran.Log = logger.NgapLog.With(logger.FieldRanAddr, remoteAddr)
context.AmfRanPool.Store(remoteAddr, &ran)
return &ran
}
Expand All @@ -398,7 +398,7 @@ func (context *AMFContext) NewAmfRanId(GnbId string) *AmfRan {
ran := AmfRan{}
ran.SupportedTAList = NewSupportedTAIList()
ran.GnbId = GnbId
ran.Log = logger.NgapLog.WithField(logger.FieldRanId, GnbId)
ran.Log = logger.NgapLog.With(logger.FieldRanId, GnbId)
context.AmfRanPool.Store(GnbId, &ran)
return &ran
}
Expand Down
30 changes: 15 additions & 15 deletions context/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,25 @@ func SetupAmfCollection() {
for {
mongoapi.ConnectMongo(mongoDbUrl, factory.AmfConfig.Configuration.AmfDBName)
if mongoapi.CommonDBClient.(*mongoapi.MongoClient).Client == nil {
logger.DataRepoLog.Errorf("MongoDb Connection failed")
logger.DataRepoLog.Errorln("mongoDb Connection failed")
} else {
logger.DataRepoLog.Infof("Successfully connected to Mongodb")
logger.DataRepoLog.Infoln("successfully connected to Mongodb")
break
}
}
_, err := mongoapi.CommonDBClient.CreateIndex(AmfUeDataColl, "supi")
if err != nil {
logger.DataRepoLog.Errorf("Create index failed on Supi field.")
logger.DataRepoLog.Errorln("create index failed on Supi field")
}

_, err = mongoapi.CommonDBClient.CreateIndex(AmfUeDataColl, "guti")
if err != nil {
logger.DataRepoLog.Errorf("Create index failed on Guti field.")
logger.DataRepoLog.Errorln("create index failed on Guti field")
}

_, err = mongoapi.CommonDBClient.CreateIndex(AmfUeDataColl, "tmsi")
if err != nil {
logger.DataRepoLog.Errorf("Create index failed on Tmsi field.")
logger.DataRepoLog.Errorln("create index failed on Tmsi field")
}

/*_, err = CommonDBClient.CreateIndex(AmfUeDataColl, "customFieldsAmfUe.amfUeNgapId")
Expand Down Expand Up @@ -179,10 +179,10 @@ func DbFetch(collName string, filter bson.M) *AmfUe {
AMF_Self().RanUePool.Store(ue.RanUe[models.AccessType__3_GPP_ACCESS].AmfUeNgapId, ue.RanUe[models.AccessType__3_GPP_ACCESS])
AMF_Self().UePool.Store(ue.Supi, ue)
ue.EventChannel = nil
ue.NASLog = logger.NasLog.WithField(logger.FieldAmfUeNgapID, fmt.Sprintf("AMF_UE_NGAP_ID:%d", ue.RanUe[models.AccessType__3_GPP_ACCESS].AmfUeNgapId))
ue.GmmLog = logger.GmmLog.WithField(logger.FieldAmfUeNgapID, fmt.Sprintf("AMF_UE_NGAP_ID:%d", ue.RanUe[models.AccessType__3_GPP_ACCESS].AmfUeNgapId))
ue.TxLog = logger.GmmLog.WithField(logger.FieldAmfUeNgapID, fmt.Sprintf("AMF_UE_NGAP_ID:%d", ue.RanUe[models.AccessType__3_GPP_ACCESS].AmfUeNgapId))
ue.ProducerLog = logger.ProducerLog.WithField(logger.FieldSupi, fmt.Sprintf("SUPI:%s", ue.Supi))
ue.NASLog = logger.NasLog.With(logger.FieldAmfUeNgapID, fmt.Sprintf("AMF_UE_NGAP_ID:%d", ue.RanUe[models.AccessType__3_GPP_ACCESS].AmfUeNgapId))
ue.GmmLog = logger.GmmLog.With(logger.FieldAmfUeNgapID, fmt.Sprintf("AMF_UE_NGAP_ID:%d", ue.RanUe[models.AccessType__3_GPP_ACCESS].AmfUeNgapId))
ue.TxLog = logger.GmmLog.With(logger.FieldAmfUeNgapID, fmt.Sprintf("AMF_UE_NGAP_ID:%d", ue.RanUe[models.AccessType__3_GPP_ACCESS].AmfUeNgapId))
ue.ProducerLog = logger.ProducerLog.With(logger.FieldSupi, fmt.Sprintf("SUPI:%s", ue.Supi))
ue.AmfInstanceName = os.Getenv("HOSTNAME")
ue.AmfInstanceIp = os.Getenv("POD_IP")
ue.TxLog.Errorln("amfue fetched")
Expand All @@ -196,7 +196,7 @@ func DbFetchRanUeByRanUeNgapID(ranUeNgapID int64, ran *AmfRan) *RanUe {

ue := DbFetch(AmfUeDataColl, filter)
if ue == nil {
logger.DataRepoLog.Errorln("DbFetchRanUeByRanUeNgapID: no document found for ranUeNgapID ", ranUeNgapID)
logger.DataRepoLog.Errorln("DbFetchRanUeByRanUeNgapID: no document found for ranUeNgapID", ranUeNgapID)
return nil
}

Expand All @@ -218,7 +218,7 @@ func DbFetchRanUeByAmfUeNgapID(amfUeNgapID int64) *RanUe {
filter["customFieldsAmfUe.amfUeNgapId"] = amfUeNgapID
ue := DbFetch(AmfUeDataColl, filter)
if ue == nil {
logger.DataRepoLog.Errorln("DbFetchRanUeByAmfUeNgapID : no document found for amfUeNgapID ", amfUeNgapID)
logger.DataRepoLog.Errorln("DbFetchRanUeByAmfUeNgapID: no document found for amfUeNgapID ", amfUeNgapID)
return nil
}

Expand All @@ -241,7 +241,7 @@ func DbFetchUeByGuti(guti string) (ue *AmfUe, ok bool) {

ue = DbFetch(AmfUeDataColl, filter)
if ue == nil {
logger.DataRepoLog.Warnln("FindByGuti : no document found for guti ", guti)
logger.DataRepoLog.Warnln("FindByGuti: no document found for guti", guti)
return nil, false
} else {
ok = true
Expand All @@ -251,7 +251,7 @@ func DbFetchUeByGuti(guti string) (ue *AmfUe, ok bool) {
// fetched AmfUe. If so, then return the same.
// else return newly fetched AmfUe and store in context
if amfUe, ret := self.AmfUeFindByGutiLocal(guti); ret {
logger.DataRepoLog.Infoln("FindByGuti : found by local", guti)
logger.DataRepoLog.Infoln("FindByGuti: found by local", guti)
ue = amfUe
ok = ret
}
Expand All @@ -266,7 +266,7 @@ func DbFetchUeBySupi(supi string) (ue *AmfUe, ok bool) {

ue = DbFetch(AmfUeDataColl, filter)
if ue == nil {
logger.DataRepoLog.Warnln("FindBySupi : no document found for supi ", supi)
logger.DataRepoLog.Warnln("FindBySupi: no document found for supi", supi)
return nil, false
} else {
ok = true
Expand All @@ -275,7 +275,7 @@ func DbFetchUeBySupi(supi string) (ue *AmfUe, ok bool) {
// fetched AmfUe. If so, then return the same.
// else return newly fetched AmfUe and store in context
if amfUe, ret := self.AmfUeFindBySupiLocal(supi); ret {
logger.DataRepoLog.Infoln("FindBySupi : found by local", supi)
logger.DataRepoLog.Infoln("FindBySupi: found by local", supi)
ue = amfUe
ok = ret
}
Expand Down
Loading

0 comments on commit 9adbe9b

Please sign in to comment.