-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Replace `logrus` with `zap` logger Signed-off-by: Arrobo, Gabriel <[email protected]> * Address issue with `golangci-lint` Signed-off-by: Arrobo, Gabriel <[email protected]> --------- Signed-off-by: Arrobo, Gabriel <[email protected]>
- Loading branch information
1 parent
c7970c1
commit 6f62a2c
Showing
5 changed files
with
70 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.4.5-dev | ||
1.5.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,58 @@ | ||
// SPDX-FileCopyrightText: 2021 Open Networking Foundation <[email protected]> | ||
// SPDX-FileCopyrightText: 2024 Intel Corporation | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package logger | ||
|
||
import ( | ||
"os" | ||
"time" | ||
|
||
formatter "github.com/antonfisher/nested-logrus-formatter" | ||
"github.com/omec-project/util/logger" | ||
"github.com/omec-project/util/logger_conf" | ||
"github.com/sirupsen/logrus" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
var ( | ||
log *logrus.Logger | ||
CfgLog *logrus.Entry | ||
InitLog *logrus.Entry | ||
GrpcLog *logrus.Entry | ||
log *zap.Logger | ||
CfgLog *zap.SugaredLogger | ||
GrpcLog *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"}, | ||
} | ||
|
||
free5gcLogHook, err := logger.NewFileHook(logger_conf.Free5gcLogFile, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0o666) | ||
if err == nil { | ||
log.Hooks.Add(free5gcLogHook) | ||
atomicLevel = zap.NewAtomicLevelAt(zap.InfoLevel) | ||
config := zap.Config{ | ||
Level: atomicLevel, | ||
Development: false, | ||
Encoding: "console", | ||
EncoderConfig: zap.NewProductionEncoderConfig(), | ||
OutputPaths: []string{"stdout"}, | ||
ErrorOutputPaths: []string{"stderr"}, | ||
} | ||
|
||
selfLogHook, err := logger.NewFileHook(logger_conf.NfLogDir+"nssf.log", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0o666) | ||
if err == nil { | ||
log.Hooks.Add(selfLogHook) | ||
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 = "" | ||
|
||
var err error | ||
log, err = config.Build() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
CfgLog = log.WithFields(logrus.Fields{"component": "Config5g", "category": "CFG"}) | ||
GrpcLog = log.WithFields(logrus.Fields{"component": "Config5g", "category": "GRPC"}) | ||
CfgLog = log.Sugar().With("component", "Config5g", "category", "CFG") | ||
GrpcLog = log.Sugar().With("component", "Config5g", "category", "GRPC") | ||
} | ||
|
||
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) { | ||
CfgLog.Infoln("set log level:", level) | ||
atomicLevel.SetLevel(level) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters