github.com/osrg/gobgp/v3@v3.30.0/pkg/log/logger.go (about)

     1  // Copyright (C) 2021 Nippon Telegraph and Telephone Corporation.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    12  // implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package log
    17  
    18  import (
    19  	"github.com/sirupsen/logrus"
    20  )
    21  
    22  type LogLevel uint32
    23  
    24  const (
    25  	PanicLevel LogLevel = iota
    26  	FatalLevel
    27  	ErrorLevel
    28  	WarnLevel
    29  	InfoLevel
    30  	DebugLevel
    31  	TraceLevel
    32  )
    33  
    34  type Fields map[string]interface{}
    35  
    36  type Logger interface {
    37  	Panic(msg string, fields Fields)
    38  	Fatal(msg string, fields Fields)
    39  	Error(msg string, fields Fields)
    40  	Warn(msg string, fields Fields)
    41  	Info(msg string, fields Fields)
    42  	Debug(msg string, fields Fields)
    43  	SetLevel(level LogLevel)
    44  	GetLevel() LogLevel
    45  }
    46  
    47  type DefaultLogger struct {
    48  	logger *logrus.Logger
    49  }
    50  
    51  func (l *DefaultLogger) Panic(msg string, fields Fields) {
    52  	l.logger.WithFields(logrus.Fields(fields)).Panic(msg)
    53  }
    54  
    55  func (l *DefaultLogger) Fatal(msg string, fields Fields) {
    56  	l.logger.WithFields(logrus.Fields(fields)).Fatal(msg)
    57  }
    58  
    59  func (l *DefaultLogger) Error(msg string, fields Fields) {
    60  	l.logger.WithFields(logrus.Fields(fields)).Error(msg)
    61  }
    62  
    63  func (l *DefaultLogger) Warn(msg string, fields Fields) {
    64  	l.logger.WithFields(logrus.Fields(fields)).Warn(msg)
    65  }
    66  
    67  func (l *DefaultLogger) Info(msg string, fields Fields) {
    68  	l.logger.WithFields(logrus.Fields(fields)).Info(msg)
    69  }
    70  
    71  func (l *DefaultLogger) Debug(msg string, fields Fields) {
    72  	l.logger.WithFields(logrus.Fields(fields)).Debug(msg)
    73  }
    74  
    75  func (l *DefaultLogger) SetLevel(level LogLevel) {
    76  	l.logger.SetLevel(logrus.Level(level))
    77  }
    78  
    79  func (l *DefaultLogger) GetLevel() LogLevel {
    80  	return LogLevel(l.logger.GetLevel())
    81  }
    82  
    83  func NewDefaultLogger() *DefaultLogger {
    84  	return &DefaultLogger{
    85  		logger: logrus.New(),
    86  	}
    87  }