refactor; move logger to module
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
module github.com/hrfee/jfa-go/logger
|
||||
|
||||
go 1.16
|
||||
|
||||
require github.com/fatih/color v1.10.0
|
||||
@@ -0,0 +1,9 @@
|
||||
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
|
||||
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
|
||||
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
|
||||
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
|
||||
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
|
||||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
|
||||
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
@@ -0,0 +1,109 @@
|
||||
// Package logger provides a wrapper around log that adds color support with github.com/fatih/color.
|
||||
package logger
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"runtime"
|
||||
"strconv"
|
||||
|
||||
c "github.com/fatih/color"
|
||||
)
|
||||
|
||||
type Logger interface {
|
||||
Printf(format string, v ...interface{})
|
||||
Print(v ...interface{})
|
||||
Println(v ...interface{})
|
||||
Fatal(v ...interface{})
|
||||
Fatalf(format string, v ...interface{})
|
||||
}
|
||||
|
||||
type logger struct {
|
||||
logger *log.Logger
|
||||
shortfile bool
|
||||
printer *c.Color
|
||||
}
|
||||
|
||||
func Lshortfile() string {
|
||||
// 0 = This function, 1 = Print/Printf/Println, 2 = Caller of Print/Printf/Println
|
||||
_, 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 + ":"
|
||||
}
|
||||
|
||||
func NewLogger(out io.Writer, prefix string, flag int, color c.Attribute) (l logger) {
|
||||
// Use reimplemented Lshortfile since wrapping the log functions messes them up
|
||||
if flag&log.Lshortfile != 0 {
|
||||
flag -= log.Lshortfile
|
||||
l.shortfile = true
|
||||
}
|
||||
|
||||
l.logger = log.New(out, prefix, flag)
|
||||
l.printer = c.New(color)
|
||||
return l
|
||||
}
|
||||
|
||||
func (l logger) Printf(format string, v ...interface{}) {
|
||||
var out string
|
||||
if l.shortfile {
|
||||
out = Lshortfile()
|
||||
}
|
||||
out += " " + l.printer.Sprintf(format, v...)
|
||||
l.logger.Print(out)
|
||||
}
|
||||
|
||||
func (l logger) Print(v ...interface{}) {
|
||||
var out string
|
||||
if l.shortfile {
|
||||
out = Lshortfile()
|
||||
}
|
||||
out += " " + l.printer.Sprint(v...)
|
||||
l.logger.Print(out)
|
||||
}
|
||||
|
||||
func (l logger) Println(v ...interface{}) {
|
||||
var out string
|
||||
if l.shortfile {
|
||||
out = Lshortfile()
|
||||
}
|
||||
out += " " + l.printer.Sprintln(v...)
|
||||
l.logger.Print(out)
|
||||
}
|
||||
|
||||
func (l logger) Fatal(v ...interface{}) {
|
||||
var out string
|
||||
if l.shortfile {
|
||||
out = Lshortfile()
|
||||
}
|
||||
out += " " + l.printer.Sprint(v...)
|
||||
l.logger.Fatal(out)
|
||||
}
|
||||
|
||||
func (l logger) Fatalf(format string, v ...interface{}) {
|
||||
var out string
|
||||
if l.shortfile {
|
||||
out = Lshortfile()
|
||||
}
|
||||
out += " " + l.printer.Sprintf(format, v...)
|
||||
l.logger.Fatal(out)
|
||||
}
|
||||
|
||||
type EmptyLogger bool
|
||||
|
||||
func (l EmptyLogger) Printf(format string, v ...interface{}) {}
|
||||
func (l EmptyLogger) Print(v ...interface{}) {}
|
||||
func (l EmptyLogger) Println(v ...interface{}) {}
|
||||
func (l EmptyLogger) Fatal(v ...interface{}) {}
|
||||
func (l EmptyLogger) Fatalf(format string, v ...interface{}) {}
|
||||
Reference in New Issue
Block a user