diff --git a/aper_test.go b/aper_test.go index ecec84e..794e02f 100644 --- a/aper_test.go +++ b/aper_test.go @@ -18,7 +18,7 @@ var perTestTraceLevel = 2 func perTestTrace(level int, s string) { if level <= perTestTraceLevel { - logger.AperLog.Traceln(s) + logger.AperLog.Debugln(s) } } diff --git a/go.mod b/go.mod index f9dd9d5..9058db8 100644 --- a/go.mod +++ b/go.mod @@ -3,13 +3,13 @@ module github.com/omec-project/aper go 1.21 require ( - github.com/antonfisher/nested-logrus-formatter v1.3.1 - github.com/sirupsen/logrus v1.8.1 - github.com/stretchr/testify v1.2.2 + github.com/stretchr/testify v1.8.1 + go.uber.org/zap v1.27.0 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect + go.uber.org/multierr v1.10.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.mod.license b/go.mod.license index 4521a03..cdc43df 100644 --- a/go.mod.license +++ b/go.mod.license @@ -1,4 +1,5 @@ SPDX-FileCopyrightText: 2019 Communication Service/Software Laboratory, National Chiao Tung University (free5gc.org) SPDX-FileCopyrightText: 2022 Open Networking Foundation +SPDX-FileCopyrightText: 2024 Intel Corporation SPDX-License-Identifier: Apache-2.0 diff --git a/go.sum b/go.sum index 278f380..d99d513 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,23 @@ -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/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= -github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= -github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= -github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +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= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= +go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= +go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= +go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/logger/logger.go b/logger/logger.go index f3a447c..695fed9 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -1,48 +1,55 @@ // Copyright 2019 Communication Service/Software Laboratory, National Chiao Tung University (free5gc.org) +// 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 - -// AperLog : Log entry of aper -var AperLog *logrus.Entry +var ( + log *zap.Logger + AperLog *zap.SugaredLogger + atomicLevel zap.AtomicLevel +) func init() { - log = logrus.New() - log.SetReportCaller(false) - - log.Formatter = &formatter.Formatter{ - TimestampFormat: time.RFC3339, - TrimMessages: true, - NoFieldsSpace: true, - HideKeys: true, - FieldsOrder: []string{"component", "category"}, + atomicLevel = zap.NewAtomicLevelAt(zap.InfoLevel) + config := zap.Config{ + Level: atomicLevel, + Development: false, + Encoding: "console", + EncoderConfig: zap.NewProductionEncoderConfig(), + OutputPaths: []string{"stdout"}, + ErrorOutputPaths: []string{"stderr"}, } - AperLog = log.WithFields(logrus.Fields{"component": "LIB", "category": "Aper"}) -} + 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" + + var err error + log, err = config.Build() + if err != nil { + panic(err) + } -func GetLogger() *logrus.Logger { - return log + AperLog = log.Sugar().With("component", "LIB", "category", "Aper") } -// SetLogLevel : set the log level (panic|fatal|error|warn|info|debug|trace) -func SetLogLevel(level logrus.Level) { - AperLog.Infoln("set log level :", level) - log.SetLevel(level) +func GetLogger() *zap.Logger { + return log } -// SetReportCaller : Set whether shows the filePath and functionName on loggers -func SetReportCaller(enable bool) { - AperLog.Infoln("set report call :", enable) - log.SetReportCaller(enable) +// SetLogLevel: set the log level (panic|fatal|error|warn|info|debug) +func SetLogLevel(level zapcore.Level) { + AperLog.Infoln("set log level:", level) + atomicLevel.SetLevel(level) }