github.com/google/osv-scalibr@v0.4.1/log/log.go (about)

     1  // Copyright 2025 Google LLC
     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  // Package log defines SCALIBR's logger interface. By default it uses the Go logger
    16  // but it can be replaced with user-defined loggers.
    17  package log
    18  
    19  import "log"
    20  
    21  // Logger is SCALIBR's logging interface.
    22  type Logger interface {
    23  	// Logs in different log levels, either formatted or unformatted.
    24  	Errorf(format string, args ...any)
    25  	Error(args ...any)
    26  	Warnf(format string, args ...any)
    27  	Warn(args ...any)
    28  	Infof(format string, args ...any)
    29  	Info(args ...any)
    30  	Debugf(format string, args ...any)
    31  	Debug(args ...any)
    32  }
    33  
    34  var logger Logger = &DefaultLogger{}
    35  
    36  // SetLogger overwrites the default SCALIBR logger with a user specified one.
    37  func SetLogger(l Logger) { logger = l }
    38  
    39  // Errorf is the static formatted error logging function.
    40  func Errorf(format string, args ...any) {
    41  	logger.Errorf(format, args...)
    42  }
    43  
    44  // Warnf is the static formatted warning logging function.
    45  func Warnf(format string, args ...any) {
    46  	logger.Warnf(format, args...)
    47  }
    48  
    49  // Infof is the static formatted info logging function.
    50  func Infof(format string, args ...any) {
    51  	logger.Infof(format, args...)
    52  }
    53  
    54  // Debugf is the static formatted debug logging function.
    55  func Debugf(format string, args ...any) {
    56  	logger.Debugf(format, args...)
    57  }
    58  
    59  // Error is the static error logging function.
    60  func Error(args ...any) {
    61  	logger.Error(args...)
    62  }
    63  
    64  // Warn is the static warning logging function.
    65  func Warn(args ...any) {
    66  	logger.Warn(args...)
    67  }
    68  
    69  // Info is the static info logging function.
    70  func Info(args ...any) {
    71  	logger.Info(args...)
    72  }
    73  
    74  // Debug is the static debug logging function.
    75  func Debug(args ...any) {
    76  	logger.Debug(args...)
    77  }
    78  
    79  // DefaultLogger is the Logger implementation used by default.
    80  // It just logs to stderr using the default Go logger.
    81  type DefaultLogger struct {
    82  	Verbose bool // Whether debug logs should be shown.
    83  }
    84  
    85  // Errorf is the formatted error logging function.
    86  func (DefaultLogger) Errorf(format string, args ...any) {
    87  	log.Printf(format, args...)
    88  }
    89  
    90  // Warnf is the formatted warning logging function.
    91  func (DefaultLogger) Warnf(format string, args ...any) {
    92  	log.Printf(format, args...)
    93  }
    94  
    95  // Infof is the formatted info logging function.
    96  func (DefaultLogger) Infof(format string, args ...any) {
    97  	log.Printf(format, args...)
    98  }
    99  
   100  // Debugf is the formatted debug logging function.
   101  func (l *DefaultLogger) Debugf(format string, args ...any) {
   102  	if l.Verbose {
   103  		log.Printf(format, args...)
   104  	}
   105  }
   106  
   107  // Error is the error logging function.
   108  func (DefaultLogger) Error(args ...any) {
   109  	log.Println(args...)
   110  }
   111  
   112  // Warn is the warning logging function.
   113  func (DefaultLogger) Warn(args ...any) {
   114  	log.Println(args...)
   115  }
   116  
   117  // Info is the info logging function.
   118  func (DefaultLogger) Info(args ...any) {
   119  	log.Println(args...)
   120  }
   121  
   122  // Debug is the debug logging function.
   123  func (l *DefaultLogger) Debug(args ...any) {
   124  	if l.Verbose {
   125  		log.Println(args...)
   126  	}
   127  }