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.
23 "go.uber.org/zap/zapcore"
32 // Copied from zap logger
34 // A Level is a logging priority. Higher levels are more important.
38 // DebugLevel logs are typically voluminous, and are usually disabled in
40 DebugLevel LogLevel = iota - 1
41 // InfoLevel is the default logging priority.
43 // WarnLevel logs are more important than Info, but don't need individual
46 // ErrorLevel logs are high-priority. If an application is running smoothly,
47 // it shouldn't generate any error-level logs.
49 // DPanicLevel logs are particularly important errors. In development the
50 // logger panics after writing the message.
52 // PanicLevel logs a message, then panics.
54 // FatalLevel logs a message, then calls os.Exit(1).
57 _minLevel = DebugLevel
58 _maxLevel = FatalLevel
61 var logLevelTokenToLevel = map[string] LogLevel {
66 "dpanic": DPanicLevel,
71 func LogLevelTokenToLevel(level string) (LogLevel, bool) {
72 if level, ok := logLevelTokenToLevel[strings.TrimSpace(strings.ToLower(level))];ok {
75 return _maxLevel+1, false
78 func InitLogger(requested LogLevel) (*Logger, error) {
79 var logger *zap.Logger
83 logger, err = initLoggerByLevel(zapcore.DebugLevel)
85 logger, err = initLoggerByLevel(zapcore.InfoLevel)
87 logger, err = initLoggerByLevel(zapcore.WarnLevel)
89 logger, err = initLoggerByLevel(zapcore.ErrorLevel)
91 logger, err = initLoggerByLevel(zapcore.DPanicLevel)
93 logger, err = initLoggerByLevel(zapcore.PanicLevel)
95 logger, err = initLoggerByLevel(zapcore.FatalLevel)
97 err = fmt.Errorf("Invalid logging Level :%d",requested)
102 return &Logger{Logger:logger}, nil
105 func(l *Logger)Sync() error {
106 l.Debugf("#logger.Sync - Going to flush buffered log")
107 return l.Logger.Sync()
110 func (l *Logger)Infof(formatMsg string, a ...interface{}) {
112 msg := fmt.Sprintf(formatMsg, a...)
113 l.Logger.Info(msg, zap.Any("mdc", l.getTimeStampMdc()))
117 func (l *Logger)Debugf(formatMsg string, a ...interface{}) {
119 msg := fmt.Sprintf(formatMsg, a...)
120 l.Logger.Debug(msg, zap.Any("mdc", l.getTimeStampMdc()))
124 func (l *Logger)Errorf(formatMsg string, a ...interface{}) {
125 msg := fmt.Sprintf(formatMsg, a...)
126 l.Logger.Error(msg, zap.Any("mdc", l.getTimeStampMdc()))
129 func (l *Logger)Warnf(formatMsg string, a ...interface{}) {
130 msg := fmt.Sprintf(formatMsg, a...)
131 l.Logger.Warn(msg, zap.Any("mdc", l.getTimeStampMdc()))
134 func (l *Logger) getTimeStampMdc() map[string]string {
135 timeStr := time.Now().Format("2006-01-02 15:04:05.000")
136 mdc := map[string]string{"time": timeStr}
140 func (l *Logger)InfoEnabled()bool{
141 return l.Logger.Core().Enabled(zap.InfoLevel)
144 func (l *Logger)DebugEnabled()bool{
145 return l.Logger.Core().Enabled(zap.DebugLevel)
148 func (l *Logger)DPanicf(formatMsg string, a ...interface{}) {
149 msg := fmt.Sprintf(formatMsg, a...)
150 l.Logger.DPanic(msg, zap.Any("mdc", l.getTimeStampMdc()))
153 func initLoggerByLevel(l zapcore.Level) (*zap.Logger, error) {
156 Level: zap.NewAtomicLevelAt(l),
157 OutputPaths: []string{"stdout"},
158 ErrorOutputPaths: []string{"stderr"},
159 EncoderConfig: zapcore.EncoderConfig{
163 EncodeLevel: zapcore.CapitalLevelEncoder,
166 EncodeTime: epochMillisIntegerTimeEncoder,
169 EncodeCaller: e2ManagerCallerEncoder,
175 func e2ManagerCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) {
176 enc.AppendString("E2Manager")
179 func epochMillisIntegerTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
180 nanos := t.UnixNano()
181 millis := int64(nanos) / int64(time.Millisecond)
182 enc.AppendInt64(millis)