github.com/laof/lite-speed-test@v0.0.0-20230930011949-1f39b7037845/log/log.go (about)

     1  package log
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"os"
     7  )
     8  
     9  var (
    10  	logger    = log.New(os.Stdout, "", log.Ldate|log.Ltime)
    11  	loggerErr = log.New(os.Stderr, "", log.Ldate|log.Ltime)
    12  	level     = ERROR
    13  )
    14  
    15  func SetLevel(newLevel LogLevel) {
    16  	level = newLevel
    17  }
    18  
    19  type Message interface {
    20  	String() string
    21  }
    22  
    23  // Write a simple logger
    24  func Write(msg Message) {
    25  	logger.Print(msg.String() + "\n")
    26  }
    27  
    28  func D(format string, v ...interface{}) {
    29  	print(format, v...)
    30  }
    31  
    32  func Debug(format Message, v ...interface{}) {
    33  	D(format.String(), v...)
    34  }
    35  
    36  func I(format string, v ...interface{}) {
    37  	if INFO < level {
    38  		return
    39  	}
    40  	print(format, v...)
    41  }
    42  
    43  func W(format string, v ...interface{}) {
    44  	if WARNING < level {
    45  		return
    46  	}
    47  	print(format, v...)
    48  }
    49  
    50  func Warnln(format string, v ...interface{}) {
    51  	W(format, v)
    52  }
    53  
    54  func E(format string, v ...interface{}) {
    55  	printErr(format, v...)
    56  }
    57  
    58  func Error(format Message, v ...interface{}) {
    59  	E(format.String(), v...)
    60  }
    61  
    62  func print(msg string, args ...interface{}) {
    63  	// m := fmt.Sprintf(msg, args...)
    64  	for _, arg := range args {
    65  		msg += fmt.Sprintf(" %v", arg)
    66  	}
    67  	logger.Println(msg)
    68  }
    69  
    70  func printErr(msg string, args ...interface{}) {
    71  	for _, arg := range args {
    72  		msg += fmt.Sprintf(" %v", arg)
    73  	}
    74  	loggerErr.Println(msg)
    75  }