tcp-proxy/pkg/logger/logger.go
2020-01-13 18:05:18 +08:00

61 lines
1.2 KiB
Go

package logger
import (
"fmt"
"github.com/mgutz/ansi"
)
type Logger interface {
Trace(f string, args ...interface{})
Debug(f string, args ...interface{})
Info(f string, args ...interface{})
Warn(f string, args ...interface{})
}
type NullLogger struct{}
func (n NullLogger) Trace(f string, args ...interface{}) {}
func (n NullLogger) Debug(f string, args ...interface{}) {}
func (n NullLogger) Info(f string, args ...interface{}) {}
func (n NullLogger) Warn(f string, args ...interface{}) {}
type ColorLogger struct {
VeryVerbose bool
Verbose bool
Prefix string
Color bool
}
func (c ColorLogger) Trace(f string, args ...interface{}) {
if !c.VeryVerbose {
return
}
c.output("blue", f, args...)
}
func (c ColorLogger) Debug(f string, args ...interface{}) {
if !c.Verbose {
return
}
c.output("green", f, args...)
}
func (c ColorLogger) Info(f string, args ...interface{}) {
c.output("green", f, args...)
}
func (c ColorLogger) Warn(f string, args ...interface{}) {
c.output("red", f, args...)
}
func (c ColorLogger) output(color, f string, args ...interface{}) {
if c.Color && color != "" {
f = ansi.Color(f, color)
}
fmt.Printf(fmt.Sprintf("%s%s\n", c.Prefix, f), args...)
}