github.com/intel/goresctrl@v0.5.0/pkg/log/log.go (about)

     1  /*
     2  Copyright 2019-2021 Intel Corporation
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package log
    18  
    19  import (
    20  	"fmt"
    21  	stdlog "log"
    22  	"strings"
    23  )
    24  
    25  // Logger is the logging interface for goresctl
    26  type Logger interface {
    27  	Debugf(format string, v ...interface{})
    28  	Infof(format string, v ...interface{})
    29  	Warnf(format string, v ...interface{})
    30  	Errorf(format string, v ...interface{})
    31  	Panicf(format string, v ...interface{})
    32  	Fatalf(format string, v ...interface{})
    33  }
    34  
    35  type logger struct {
    36  	*stdlog.Logger
    37  }
    38  
    39  // NewLoggerWrapper wraps an implementation of the golang standard intreface
    40  // into a goresctl specific compatible logger interface
    41  func NewLoggerWrapper(l *stdlog.Logger) Logger {
    42  	return &logger{Logger: l}
    43  }
    44  
    45  func (l *logger) Debugf(format string, v ...interface{}) {
    46  	l.Logger.Printf("DEBUG: "+format, v...)
    47  }
    48  
    49  func (l *logger) Infof(format string, v ...interface{}) {
    50  	l.Logger.Printf("INFO: "+format, v...)
    51  }
    52  
    53  func (l *logger) Warnf(format string, v ...interface{}) {
    54  	l.Logger.Printf("WARN: "+format, v...)
    55  }
    56  
    57  func (l *logger) Errorf(format string, v ...interface{}) {
    58  	l.Logger.Printf("ERROR: "+format, v...)
    59  }
    60  
    61  func (l *logger) Panicf(format string, v ...interface{}) {
    62  	l.Logger.Panicf(format, v...)
    63  }
    64  
    65  func (l *logger) Fatalf(format string, v ...interface{}) {
    66  	l.Logger.Fatalf(format, v...)
    67  }
    68  
    69  func InfoBlock(l Logger, heading, linePrefix, format string, v ...interface{}) {
    70  	l.Infof("%s", heading)
    71  
    72  	lines := strings.Split(fmt.Sprintf(format, v...), "\n")
    73  	for _, line := range lines {
    74  		l.Infof("%s%s", linePrefix, line)
    75  	}
    76  }
    77  
    78  func DebugBlock(l Logger, heading, linePrefix, format string, v ...interface{}) {
    79  	l.Debugf("%s", heading)
    80  
    81  	lines := strings.Split(fmt.Sprintf(format, v...), "\n")
    82  	for _, line := range lines {
    83  		l.Debugf("%s%s", linePrefix, line)
    84  	}
    85  }