github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/logger/logger.go (about)

     1  // Copyright 2022-2023 The Inspektor Gadget authors
     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 implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  /*
    16  Package logger provides a logger interface that is available to gadgets
    17  and runtimes.
    18  */
    19  package logger
    20  
    21  import log "github.com/sirupsen/logrus"
    22  
    23  type Level = log.Level
    24  
    25  // we use the log levels from logrus here
    26  const (
    27  	PanicLevel = log.PanicLevel
    28  	FatalLevel = log.FatalLevel
    29  	ErrorLevel = log.ErrorLevel
    30  	WarnLevel  = log.WarnLevel
    31  	InfoLevel  = log.InfoLevel
    32  	DebugLevel = log.DebugLevel
    33  	TraceLevel = log.TraceLevel
    34  )
    35  
    36  type DedicatedLogger interface {
    37  	Panic(params ...any)
    38  	Panicf(fmt string, params ...any)
    39  	Fatal(params ...any)
    40  	Fatalf(fmt string, params ...any)
    41  	Error(params ...any)
    42  	Errorf(fmt string, params ...any)
    43  	Warn(params ...any)
    44  	Warnf(fmt string, params ...any)
    45  	Info(params ...any)
    46  	Infof(fmt string, params ...any)
    47  	Debug(params ...any)
    48  	Debugf(fmt string, params ...any)
    49  	Trace(params ...any)
    50  	Tracef(fmt string, params ...any)
    51  }
    52  
    53  type GenericLogger interface {
    54  	Log(severity Level, params ...any)
    55  	Logf(severity Level, format string, params ...any)
    56  }
    57  
    58  type GenericLoggerWithLevelSetter interface {
    59  	GenericLogger
    60  	LevelGetterSetter
    61  }
    62  
    63  type DedicatedLoggerWithLevelSetter interface {
    64  	DedicatedLogger
    65  	LevelGetterSetter
    66  }
    67  
    68  type LevelGetterSetter interface {
    69  	SetLevel(Level)
    70  	GetLevel() Level
    71  }
    72  
    73  type Logger interface {
    74  	DedicatedLogger
    75  	GenericLogger
    76  	LevelGetterSetter
    77  }
    78  
    79  func NewFromGenericLogger(logger GenericLoggerWithLevelSetter) Logger {
    80  	return &StandardDedicatedLogger{GenericLoggerWithLevelSetter: logger}
    81  }
    82  
    83  func NewFromDedicatedLogger(logger DedicatedLoggerWithLevelSetter) Logger {
    84  	return &StandardGenericLogger{DedicatedLoggerWithLevelSetter: logger}
    85  }
    86  
    87  type StandardDedicatedLogger struct {
    88  	GenericLoggerWithLevelSetter
    89  }
    90  
    91  func (s *StandardDedicatedLogger) Panic(params ...any) {
    92  	s.Log(PanicLevel, params...)
    93  }
    94  
    95  func (s *StandardDedicatedLogger) Panicf(fmt string, params ...any) {
    96  	s.Logf(PanicLevel, fmt, params...)
    97  }
    98  
    99  func (s *StandardDedicatedLogger) Fatal(params ...any) {
   100  	s.Log(FatalLevel, params...)
   101  }
   102  
   103  func (s *StandardDedicatedLogger) Fatalf(fmt string, params ...any) {
   104  	s.Logf(FatalLevel, fmt, params...)
   105  }
   106  
   107  func (s *StandardDedicatedLogger) Error(params ...any) {
   108  	s.Log(ErrorLevel, params...)
   109  }
   110  
   111  func (s *StandardDedicatedLogger) Errorf(fmt string, params ...any) {
   112  	s.Logf(ErrorLevel, fmt, params...)
   113  }
   114  
   115  func (s *StandardDedicatedLogger) Warn(params ...any) {
   116  	s.Log(WarnLevel, params...)
   117  }
   118  
   119  func (s *StandardDedicatedLogger) Warnf(fmt string, params ...any) {
   120  	s.Logf(WarnLevel, fmt, params...)
   121  }
   122  
   123  func (s *StandardDedicatedLogger) Info(params ...any) {
   124  	s.Log(InfoLevel, params...)
   125  }
   126  
   127  func (s *StandardDedicatedLogger) Infof(fmt string, params ...any) {
   128  	s.Logf(InfoLevel, fmt, params...)
   129  }
   130  
   131  func (s *StandardDedicatedLogger) Debug(params ...any) {
   132  	s.Log(DebugLevel, params...)
   133  }
   134  
   135  func (s *StandardDedicatedLogger) Debugf(fmt string, params ...any) {
   136  	s.Logf(DebugLevel, fmt, params...)
   137  }
   138  
   139  func (s *StandardDedicatedLogger) Trace(params ...any) {
   140  	s.Log(TraceLevel, params...)
   141  }
   142  
   143  func (s *StandardDedicatedLogger) Tracef(fmt string, params ...any) {
   144  	s.Logf(TraceLevel, fmt, params...)
   145  }
   146  
   147  type StandardGenericLogger struct {
   148  	DedicatedLoggerWithLevelSetter
   149  }
   150  
   151  func (s *StandardGenericLogger) Log(severity Level, params ...any) {
   152  	switch severity {
   153  	case PanicLevel:
   154  		s.Panic(params...)
   155  	case FatalLevel:
   156  		s.Fatal(params...)
   157  	case ErrorLevel:
   158  		s.Error(params...)
   159  	case WarnLevel:
   160  		s.Warn(params...)
   161  	case InfoLevel:
   162  		s.Info(params...)
   163  	case DebugLevel:
   164  		s.Debug(params...)
   165  	case TraceLevel:
   166  		s.Trace(params...)
   167  	}
   168  }
   169  
   170  func (s *StandardGenericLogger) Logf(severity Level, format string, params ...any) {
   171  	switch severity {
   172  	case PanicLevel:
   173  		s.Panicf(format, params...)
   174  	case FatalLevel:
   175  		s.Fatalf(format, params...)
   176  	case ErrorLevel:
   177  		s.Errorf(format, params...)
   178  	case WarnLevel:
   179  		s.Warnf(format, params...)
   180  	case InfoLevel:
   181  		s.Infof(format, params...)
   182  	case DebugLevel:
   183  		s.Debugf(format, params...)
   184  	case TraceLevel:
   185  		s.Tracef(format, params...)
   186  	}
   187  }
   188  
   189  func DefaultLogger() Logger {
   190  	return log.StandardLogger()
   191  }