2021-04-30 23:13:57 +00:00
|
|
|
// Package logger provides a wrapper around log that adds color support with github.com/fatih/color.
|
|
|
|
package logger
|
2021-02-19 16:12:14 +00:00
|
|
|
|
|
|
|
import (
|
2021-06-11 22:14:16 +00:00
|
|
|
"errors"
|
2021-02-19 16:12:14 +00:00
|
|
|
"io"
|
|
|
|
"log"
|
2021-03-23 21:57:53 +00:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2021-02-19 16:12:14 +00:00
|
|
|
|
|
|
|
c "github.com/fatih/color"
|
|
|
|
)
|
|
|
|
|
2021-09-18 12:43:11 +00:00
|
|
|
// type Logger interface {
|
|
|
|
// Printf(format string, v ...interface{})
|
|
|
|
// Print(v ...interface{})
|
|
|
|
// Println(v ...interface{})
|
|
|
|
// Fatal(v ...interface{})
|
|
|
|
// Fatalf(format string, v ...interface{})
|
|
|
|
// SetFatalFunc(f func(err interface{}))
|
|
|
|
// }
|
2021-02-19 16:12:14 +00:00
|
|
|
|
2021-09-18 12:43:11 +00:00
|
|
|
type Logger struct {
|
|
|
|
empty bool
|
2021-03-23 21:57:53 +00:00
|
|
|
logger *log.Logger
|
|
|
|
shortfile bool
|
|
|
|
printer *c.Color
|
2021-06-11 22:14:16 +00:00
|
|
|
fatalFunc func(err interface{})
|
2021-03-23 21:57:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Lshortfile() string {
|
2021-03-27 16:07:22 +00:00
|
|
|
// 0 = This function, 1 = Print/Printf/Println, 2 = Caller of Print/Printf/Println
|
2021-03-23 21:57:53 +00:00
|
|
|
_, file, line, ok := runtime.Caller(2)
|
|
|
|
lineString := strconv.Itoa(line)
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if file == "" {
|
|
|
|
return lineString
|
|
|
|
}
|
|
|
|
for i := len(file) - 1; i > 0; i-- {
|
|
|
|
if file[i] == '/' || file[i] == '\\' {
|
|
|
|
file = file[i+1:]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return file + ":" + lineString + ":"
|
2021-02-19 16:12:14 +00:00
|
|
|
}
|
|
|
|
|
2021-09-18 12:43:11 +00:00
|
|
|
func NewLogger(out io.Writer, prefix string, flag int, color c.Attribute) (l *Logger) {
|
|
|
|
l = &Logger{}
|
2021-03-23 21:57:53 +00:00
|
|
|
// Use reimplemented Lshortfile since wrapping the log functions messes them up
|
|
|
|
if flag&log.Lshortfile != 0 {
|
|
|
|
flag -= log.Lshortfile
|
|
|
|
l.shortfile = true
|
|
|
|
}
|
|
|
|
|
2021-02-19 16:12:14 +00:00
|
|
|
l.logger = log.New(out, prefix, flag)
|
|
|
|
l.printer = c.New(color)
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2021-10-03 14:59:58 +00:00
|
|
|
func NewEmptyLogger() (l *Logger) {
|
|
|
|
l = &Logger{
|
|
|
|
empty: true,
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2021-09-18 12:43:11 +00:00
|
|
|
|
|
|
|
func (l *Logger) Printf(format string, v ...interface{}) {
|
|
|
|
if l.empty {
|
|
|
|
return
|
|
|
|
}
|
2021-03-23 21:57:53 +00:00
|
|
|
var out string
|
|
|
|
if l.shortfile {
|
|
|
|
out = Lshortfile()
|
|
|
|
}
|
|
|
|
out += " " + l.printer.Sprintf(format, v...)
|
|
|
|
l.logger.Print(out)
|
|
|
|
}
|
|
|
|
|
2021-09-18 12:43:11 +00:00
|
|
|
func (l *Logger) Print(v ...interface{}) {
|
|
|
|
if l.empty {
|
|
|
|
return
|
|
|
|
}
|
2021-03-23 21:57:53 +00:00
|
|
|
var out string
|
|
|
|
if l.shortfile {
|
|
|
|
out = Lshortfile()
|
|
|
|
}
|
|
|
|
out += " " + l.printer.Sprint(v...)
|
|
|
|
l.logger.Print(out)
|
2021-02-19 16:12:14 +00:00
|
|
|
}
|
2021-03-23 21:57:53 +00:00
|
|
|
|
2021-09-18 12:43:11 +00:00
|
|
|
func (l *Logger) Println(v ...interface{}) {
|
|
|
|
if l.empty {
|
|
|
|
return
|
|
|
|
}
|
2021-03-23 21:57:53 +00:00
|
|
|
var out string
|
|
|
|
if l.shortfile {
|
|
|
|
out = Lshortfile()
|
|
|
|
}
|
|
|
|
out += " " + l.printer.Sprintln(v...)
|
|
|
|
l.logger.Print(out)
|
|
|
|
}
|
|
|
|
|
2021-09-18 12:43:11 +00:00
|
|
|
func (l *Logger) Fatal(v ...interface{}) {
|
|
|
|
if l.empty {
|
|
|
|
return
|
|
|
|
}
|
2021-03-23 21:57:53 +00:00
|
|
|
var out string
|
|
|
|
if l.shortfile {
|
|
|
|
out = Lshortfile()
|
|
|
|
}
|
|
|
|
out += " " + l.printer.Sprint(v...)
|
|
|
|
l.logger.Fatal(out)
|
|
|
|
}
|
|
|
|
|
2021-09-18 12:43:11 +00:00
|
|
|
func (l *Logger) Fatalf(format string, v ...interface{}) {
|
|
|
|
if l.empty {
|
|
|
|
return
|
|
|
|
}
|
2021-03-23 21:57:53 +00:00
|
|
|
var out string
|
|
|
|
if l.shortfile {
|
|
|
|
out = Lshortfile()
|
|
|
|
}
|
|
|
|
out += " " + l.printer.Sprintf(format, v...)
|
2021-06-11 22:14:16 +00:00
|
|
|
if l.fatalFunc != nil {
|
|
|
|
l.fatalFunc(errors.New(out))
|
|
|
|
} else {
|
|
|
|
l.logger.Fatal(out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-18 12:43:11 +00:00
|
|
|
func (l *Logger) SetFatalFunc(f func(err interface{})) {
|
2021-06-11 22:14:16 +00:00
|
|
|
l.fatalFunc = f
|
2021-02-19 16:12:14 +00:00
|
|
|
}
|