2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 // platform project (RICP).
26 "go.uber.org/zap/zapcore"
35 // Copied from zap logger
37 // A Level is a logging priority. Higher levels are more important.
41 // DebugLevel logs are typically voluminous, and are usually disabled in
43 DebugLevel LogLevel = iota - 1
44 // InfoLevel is the default logging priority.
46 // WarnLevel logs are more important than Info, but don't need individual
49 // ErrorLevel logs are high-priority. If an application is running smoothly,
50 // it shouldn't generate any error-level logs.
52 // DPanicLevel logs are particularly important errors. In development the
53 // logger panics after writing the message.
55 // PanicLevel logs a message, then panics.
57 // FatalLevel logs a message, then calls os.Exit(1).
60 _minLevel = DebugLevel
61 _maxLevel = FatalLevel
64 var logLevelTokenToLevel = map[string] LogLevel {
69 "dpanic": DPanicLevel,
74 func LogLevelTokenToLevel(level string) (LogLevel, bool) {
75 if level, ok := logLevelTokenToLevel[strings.TrimSpace(strings.ToLower(level))];ok {
78 return _maxLevel+1, false
81 func InitLogger(requested LogLevel) (*Logger, error) {
82 var logger *zap.Logger
86 logger, err = initLoggerByLevel(zapcore.DebugLevel)
88 logger, err = initLoggerByLevel(zapcore.InfoLevel)
90 logger, err = initLoggerByLevel(zapcore.WarnLevel)
92 logger, err = initLoggerByLevel(zapcore.ErrorLevel)
94 logger, err = initLoggerByLevel(zapcore.DPanicLevel)
96 logger, err = initLoggerByLevel(zapcore.PanicLevel)
98 logger, err = initLoggerByLevel(zapcore.FatalLevel)
100 err = fmt.Errorf("Invalid logging Level :%d",requested)
105 return &Logger{Logger:logger}, nil
108 func(l *Logger)Sync() error {
109 l.Debugf("#logger.Sync - Going to flush buffered log")
110 return l.Logger.Sync()
113 func (l *Logger)Infof(formatMsg string, a ...interface{}) {
115 msg := fmt.Sprintf(formatMsg, a...)
116 l.Logger.Info(msg, zap.Any("mdc", l.getTimeStampMdc()))
120 func (l *Logger)Debugf(formatMsg string, a ...interface{}) {
122 msg := fmt.Sprintf(formatMsg, a...)
123 l.Logger.Debug(msg, zap.Any("mdc", l.getTimeStampMdc()))
127 func (l *Logger)Errorf(formatMsg string, a ...interface{}) {
128 msg := fmt.Sprintf(formatMsg, a...)
129 l.Logger.Error(msg, zap.Any("mdc", l.getTimeStampMdc()))
132 func (l *Logger)Warnf(formatMsg string, a ...interface{}) {
133 msg := fmt.Sprintf(formatMsg, a...)
134 l.Logger.Warn(msg, zap.Any("mdc", l.getTimeStampMdc()))
137 func (l *Logger) getTimeStampMdc() map[string]string {
138 timeStr := time.Now().Format("2006-01-02 15:04:05.000")
139 mdc := map[string]string{"time": timeStr}
143 func (l *Logger)InfoEnabled()bool{
144 return l.Logger.Core().Enabled(zap.InfoLevel)
147 func (l *Logger)DebugEnabled()bool{
148 return l.Logger.Core().Enabled(zap.DebugLevel)
151 func (l *Logger)DPanicf(formatMsg string, a ...interface{}) {
152 msg := fmt.Sprintf(formatMsg, a...)
153 l.Logger.DPanic(msg, zap.Any("mdc", l.getTimeStampMdc()))
156 func initLoggerByLevel(l zapcore.Level) (*zap.Logger, error) {
159 Level: zap.NewAtomicLevelAt(l),
160 OutputPaths: []string{"stdout"},
161 ErrorOutputPaths: []string{"stderr"},
162 EncoderConfig: zapcore.EncoderConfig{
166 EncodeLevel: zapcore.CapitalLevelEncoder,
169 EncodeTime: epochMillisIntegerTimeEncoder,
172 EncodeCaller: e2ManagerCallerEncoder,
178 func e2ManagerCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) {
179 enc.AppendString("E2Manager")
182 func epochMillisIntegerTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
183 nanos := t.UnixNano()
184 millis := int64(nanos) / int64(time.Millisecond)
185 enc.AppendInt64(millis)