forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/labstack/gommon/log/log.go

657 lines
7.4 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
package log
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"path"
"runtime"
"strconv"
"sync"
"sync/atomic"
"time"
2024-06-14 08:41:36 +00:00
"github.com/labstack/gommon/color"
2024-02-18 10:42:21 +00:00
"github.com/mattn/go-isatty"
"github.com/valyala/fasttemplate"
)
type (
Logger struct {
prefix string
level uint32
skip int
output io.Writer
template *fasttemplate.Template
levels []string
color *color.Color
2024-02-18 10:42:21 +00:00
bufferPool sync.Pool
mutex sync.Mutex
2024-02-18 10:42:21 +00:00
}
Lvl uint8
JSON map[string]interface{}
)
const (
DEBUG Lvl = iota + 1
2024-02-18 10:42:21 +00:00
INFO
2024-02-18 10:42:21 +00:00
WARN
2024-02-18 10:42:21 +00:00
ERROR
2024-02-18 10:42:21 +00:00
OFF
2024-02-18 10:42:21 +00:00
panicLevel
2024-02-18 10:42:21 +00:00
fatalLevel
)
var (
global = New("-")
2024-02-18 10:42:21 +00:00
defaultHeader = `{"time":"${time_rfc3339_nano}","level":"${level}","prefix":"${prefix}",` +
2024-02-18 10:42:21 +00:00
`"file":"${short_file}","line":"${line}"}`
)
func init() {
2024-02-18 10:42:21 +00:00
global.skip = 3
2024-02-18 10:42:21 +00:00
}
func New(prefix string) (l *Logger) {
2024-02-18 10:42:21 +00:00
l = &Logger{
level: uint32(INFO),
skip: 2,
prefix: prefix,
2024-02-18 10:42:21 +00:00
template: l.newTemplate(defaultHeader),
color: color.New(),
2024-02-18 10:42:21 +00:00
bufferPool: sync.Pool{
2024-02-18 10:42:21 +00:00
New: func() interface{} {
2024-02-18 10:42:21 +00:00
return bytes.NewBuffer(make([]byte, 256))
2024-02-18 10:42:21 +00:00
},
},
}
2024-02-18 10:42:21 +00:00
l.initLevels()
2024-02-18 10:42:21 +00:00
l.SetOutput(output())
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
func (l *Logger) initLevels() {
2024-02-18 10:42:21 +00:00
l.levels = []string{
2024-02-18 10:42:21 +00:00
"-",
2024-02-18 10:42:21 +00:00
l.color.Blue("DEBUG"),
2024-02-18 10:42:21 +00:00
l.color.Green("INFO"),
2024-02-18 10:42:21 +00:00
l.color.Yellow("WARN"),
2024-02-18 10:42:21 +00:00
l.color.Red("ERROR"),
2024-02-18 10:42:21 +00:00
"",
2024-02-18 10:42:21 +00:00
l.color.Yellow("PANIC", color.U),
2024-02-18 10:42:21 +00:00
l.color.Red("FATAL", color.U),
}
2024-02-18 10:42:21 +00:00
}
func (l *Logger) newTemplate(format string) *fasttemplate.Template {
2024-02-18 10:42:21 +00:00
return fasttemplate.New(format, "${", "}")
2024-02-18 10:42:21 +00:00
}
func (l *Logger) DisableColor() {
2024-02-18 10:42:21 +00:00
l.color.Disable()
2024-02-18 10:42:21 +00:00
l.initLevels()
2024-02-18 10:42:21 +00:00
}
func (l *Logger) EnableColor() {
2024-02-18 10:42:21 +00:00
l.color.Enable()
2024-02-18 10:42:21 +00:00
l.initLevels()
2024-02-18 10:42:21 +00:00
}
func (l *Logger) Prefix() string {
2024-02-18 10:42:21 +00:00
return l.prefix
2024-02-18 10:42:21 +00:00
}
func (l *Logger) SetPrefix(p string) {
2024-02-18 10:42:21 +00:00
l.prefix = p
2024-02-18 10:42:21 +00:00
}
func (l *Logger) Level() Lvl {
2024-02-18 10:42:21 +00:00
return Lvl(atomic.LoadUint32(&l.level))
2024-02-18 10:42:21 +00:00
}
func (l *Logger) SetLevel(level Lvl) {
2024-02-18 10:42:21 +00:00
atomic.StoreUint32(&l.level, uint32(level))
2024-02-18 10:42:21 +00:00
}
func (l *Logger) Output() io.Writer {
2024-02-18 10:42:21 +00:00
return l.output
2024-02-18 10:42:21 +00:00
}
func (l *Logger) SetOutput(w io.Writer) {
2024-02-18 10:42:21 +00:00
l.output = w
2024-02-18 10:42:21 +00:00
if w, ok := w.(*os.File); !ok || !isatty.IsTerminal(w.Fd()) {
2024-02-18 10:42:21 +00:00
l.DisableColor()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
func (l *Logger) Color() *color.Color {
2024-02-18 10:42:21 +00:00
return l.color
2024-02-18 10:42:21 +00:00
}
func (l *Logger) SetHeader(h string) {
2024-02-18 10:42:21 +00:00
l.template = l.newTemplate(h)
2024-02-18 10:42:21 +00:00
}
func (l *Logger) Print(i ...interface{}) {
2024-02-18 10:42:21 +00:00
l.log(0, "", i...)
2024-02-18 10:42:21 +00:00
// fmt.Fprintln(l.output, i...)
2024-02-18 10:42:21 +00:00
}
func (l *Logger) Printf(format string, args ...interface{}) {
2024-02-18 10:42:21 +00:00
l.log(0, format, args...)
2024-02-18 10:42:21 +00:00
}
func (l *Logger) Printj(j JSON) {
2024-02-18 10:42:21 +00:00
l.log(0, "json", j)
2024-02-18 10:42:21 +00:00
}
func (l *Logger) Debug(i ...interface{}) {
2024-02-18 10:42:21 +00:00
l.log(DEBUG, "", i...)
2024-02-18 10:42:21 +00:00
}
func (l *Logger) Debugf(format string, args ...interface{}) {
2024-02-18 10:42:21 +00:00
l.log(DEBUG, format, args...)
2024-02-18 10:42:21 +00:00
}
func (l *Logger) Debugj(j JSON) {
2024-02-18 10:42:21 +00:00
l.log(DEBUG, "json", j)
2024-02-18 10:42:21 +00:00
}
func (l *Logger) Info(i ...interface{}) {
2024-02-18 10:42:21 +00:00
l.log(INFO, "", i...)
2024-02-18 10:42:21 +00:00
}
func (l *Logger) Infof(format string, args ...interface{}) {
2024-02-18 10:42:21 +00:00
l.log(INFO, format, args...)
2024-02-18 10:42:21 +00:00
}
func (l *Logger) Infoj(j JSON) {
2024-02-18 10:42:21 +00:00
l.log(INFO, "json", j)
2024-02-18 10:42:21 +00:00
}
func (l *Logger) Warn(i ...interface{}) {
2024-02-18 10:42:21 +00:00
l.log(WARN, "", i...)
2024-02-18 10:42:21 +00:00
}
func (l *Logger) Warnf(format string, args ...interface{}) {
2024-02-18 10:42:21 +00:00
l.log(WARN, format, args...)
2024-02-18 10:42:21 +00:00
}
func (l *Logger) Warnj(j JSON) {
2024-02-18 10:42:21 +00:00
l.log(WARN, "json", j)
2024-02-18 10:42:21 +00:00
}
func (l *Logger) Error(i ...interface{}) {
2024-02-18 10:42:21 +00:00
l.log(ERROR, "", i...)
2024-02-18 10:42:21 +00:00
}
func (l *Logger) Errorf(format string, args ...interface{}) {
2024-02-18 10:42:21 +00:00
l.log(ERROR, format, args...)
2024-02-18 10:42:21 +00:00
}
func (l *Logger) Errorj(j JSON) {
2024-02-18 10:42:21 +00:00
l.log(ERROR, "json", j)
2024-02-18 10:42:21 +00:00
}
func (l *Logger) Fatal(i ...interface{}) {
2024-02-18 10:42:21 +00:00
l.log(fatalLevel, "", i...)
2024-02-18 10:42:21 +00:00
os.Exit(1)
2024-02-18 10:42:21 +00:00
}
func (l *Logger) Fatalf(format string, args ...interface{}) {
2024-02-18 10:42:21 +00:00
l.log(fatalLevel, format, args...)
2024-02-18 10:42:21 +00:00
os.Exit(1)
2024-02-18 10:42:21 +00:00
}
func (l *Logger) Fatalj(j JSON) {
2024-02-18 10:42:21 +00:00
l.log(fatalLevel, "json", j)
2024-02-18 10:42:21 +00:00
os.Exit(1)
2024-02-18 10:42:21 +00:00
}
func (l *Logger) Panic(i ...interface{}) {
2024-02-18 10:42:21 +00:00
l.log(panicLevel, "", i...)
2024-02-18 10:42:21 +00:00
panic(fmt.Sprint(i...))
2024-02-18 10:42:21 +00:00
}
func (l *Logger) Panicf(format string, args ...interface{}) {
2024-02-18 10:42:21 +00:00
l.log(panicLevel, format, args...)
2024-02-18 10:42:21 +00:00
panic(fmt.Sprintf(format, args...))
2024-02-18 10:42:21 +00:00
}
func (l *Logger) Panicj(j JSON) {
2024-02-18 10:42:21 +00:00
l.log(panicLevel, "json", j)
2024-02-18 10:42:21 +00:00
panic(j)
2024-02-18 10:42:21 +00:00
}
func DisableColor() {
2024-02-18 10:42:21 +00:00
global.DisableColor()
2024-02-18 10:42:21 +00:00
}
func EnableColor() {
2024-02-18 10:42:21 +00:00
global.EnableColor()
2024-02-18 10:42:21 +00:00
}
func Prefix() string {
2024-02-18 10:42:21 +00:00
return global.Prefix()
2024-02-18 10:42:21 +00:00
}
func SetPrefix(p string) {
2024-02-18 10:42:21 +00:00
global.SetPrefix(p)
2024-02-18 10:42:21 +00:00
}
func Level() Lvl {
2024-02-18 10:42:21 +00:00
return global.Level()
2024-02-18 10:42:21 +00:00
}
func SetLevel(level Lvl) {
2024-02-18 10:42:21 +00:00
global.SetLevel(level)
2024-02-18 10:42:21 +00:00
}
func Output() io.Writer {
2024-02-18 10:42:21 +00:00
return global.Output()
2024-02-18 10:42:21 +00:00
}
func SetOutput(w io.Writer) {
2024-02-18 10:42:21 +00:00
global.SetOutput(w)
2024-02-18 10:42:21 +00:00
}
func SetHeader(h string) {
2024-02-18 10:42:21 +00:00
global.SetHeader(h)
2024-02-18 10:42:21 +00:00
}
func Print(i ...interface{}) {
2024-02-18 10:42:21 +00:00
global.Print(i...)
2024-02-18 10:42:21 +00:00
}
func Printf(format string, args ...interface{}) {
2024-02-18 10:42:21 +00:00
global.Printf(format, args...)
2024-02-18 10:42:21 +00:00
}
func Printj(j JSON) {
2024-02-18 10:42:21 +00:00
global.Printj(j)
2024-02-18 10:42:21 +00:00
}
func Debug(i ...interface{}) {
2024-02-18 10:42:21 +00:00
global.Debug(i...)
2024-02-18 10:42:21 +00:00
}
func Debugf(format string, args ...interface{}) {
2024-02-18 10:42:21 +00:00
global.Debugf(format, args...)
2024-02-18 10:42:21 +00:00
}
func Debugj(j JSON) {
2024-02-18 10:42:21 +00:00
global.Debugj(j)
2024-02-18 10:42:21 +00:00
}
func Info(i ...interface{}) {
2024-02-18 10:42:21 +00:00
global.Info(i...)
2024-02-18 10:42:21 +00:00
}
func Infof(format string, args ...interface{}) {
2024-02-18 10:42:21 +00:00
global.Infof(format, args...)
2024-02-18 10:42:21 +00:00
}
func Infoj(j JSON) {
2024-02-18 10:42:21 +00:00
global.Infoj(j)
2024-02-18 10:42:21 +00:00
}
func Warn(i ...interface{}) {
2024-02-18 10:42:21 +00:00
global.Warn(i...)
2024-02-18 10:42:21 +00:00
}
func Warnf(format string, args ...interface{}) {
2024-02-18 10:42:21 +00:00
global.Warnf(format, args...)
2024-02-18 10:42:21 +00:00
}
func Warnj(j JSON) {
2024-02-18 10:42:21 +00:00
global.Warnj(j)
2024-02-18 10:42:21 +00:00
}
func Error(i ...interface{}) {
2024-02-18 10:42:21 +00:00
global.Error(i...)
2024-02-18 10:42:21 +00:00
}
func Errorf(format string, args ...interface{}) {
2024-02-18 10:42:21 +00:00
global.Errorf(format, args...)
2024-02-18 10:42:21 +00:00
}
func Errorj(j JSON) {
2024-02-18 10:42:21 +00:00
global.Errorj(j)
2024-02-18 10:42:21 +00:00
}
func Fatal(i ...interface{}) {
2024-02-18 10:42:21 +00:00
global.Fatal(i...)
2024-02-18 10:42:21 +00:00
}
func Fatalf(format string, args ...interface{}) {
2024-02-18 10:42:21 +00:00
global.Fatalf(format, args...)
2024-02-18 10:42:21 +00:00
}
func Fatalj(j JSON) {
2024-02-18 10:42:21 +00:00
global.Fatalj(j)
2024-02-18 10:42:21 +00:00
}
func Panic(i ...interface{}) {
2024-02-18 10:42:21 +00:00
global.Panic(i...)
2024-02-18 10:42:21 +00:00
}
func Panicf(format string, args ...interface{}) {
2024-02-18 10:42:21 +00:00
global.Panicf(format, args...)
2024-02-18 10:42:21 +00:00
}
func Panicj(j JSON) {
2024-02-18 10:42:21 +00:00
global.Panicj(j)
2024-02-18 10:42:21 +00:00
}
func (l *Logger) log(level Lvl, format string, args ...interface{}) {
2024-02-18 10:42:21 +00:00
if level >= l.Level() || level == 0 {
2024-02-18 10:42:21 +00:00
buf := l.bufferPool.Get().(*bytes.Buffer)
2024-02-18 10:42:21 +00:00
buf.Reset()
2024-02-18 10:42:21 +00:00
defer l.bufferPool.Put(buf)
2024-02-18 10:42:21 +00:00
_, file, line, _ := runtime.Caller(l.skip)
2024-02-18 10:42:21 +00:00
message := ""
if format == "" {
2024-02-18 10:42:21 +00:00
message = fmt.Sprint(args...)
2024-02-18 10:42:21 +00:00
} else if format == "json" {
2024-02-18 10:42:21 +00:00
b, err := json.Marshal(args[0])
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
panic(err)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
message = string(b)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
message = fmt.Sprintf(format, args...)
2024-02-18 10:42:21 +00:00
}
_, err := l.template.ExecuteFunc(buf, func(w io.Writer, tag string) (int, error) {
2024-02-18 10:42:21 +00:00
switch tag {
2024-02-18 10:42:21 +00:00
case "time_rfc3339":
2024-02-18 10:42:21 +00:00
return w.Write([]byte(time.Now().Format(time.RFC3339)))
2024-02-18 10:42:21 +00:00
case "time_rfc3339_nano":
2024-02-18 10:42:21 +00:00
return w.Write([]byte(time.Now().Format(time.RFC3339Nano)))
2024-02-18 10:42:21 +00:00
case "level":
2024-02-18 10:42:21 +00:00
return w.Write([]byte(l.levels[level]))
2024-02-18 10:42:21 +00:00
case "prefix":
2024-02-18 10:42:21 +00:00
return w.Write([]byte(l.prefix))
2024-02-18 10:42:21 +00:00
case "long_file":
2024-02-18 10:42:21 +00:00
return w.Write([]byte(file))
2024-02-18 10:42:21 +00:00
case "short_file":
2024-02-18 10:42:21 +00:00
return w.Write([]byte(path.Base(file)))
2024-02-18 10:42:21 +00:00
case "line":
2024-02-18 10:42:21 +00:00
return w.Write([]byte(strconv.Itoa(line)))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return 0, nil
2024-02-18 10:42:21 +00:00
})
if err == nil {
2024-02-18 10:42:21 +00:00
s := buf.String()
2024-02-18 10:42:21 +00:00
i := buf.Len() - 1
2024-02-18 10:42:21 +00:00
if i >= 0 && s[i] == '}' {
2024-02-18 10:42:21 +00:00
// JSON header
2024-02-18 10:42:21 +00:00
buf.Truncate(i)
2024-02-18 10:42:21 +00:00
buf.WriteByte(',')
2024-02-18 10:42:21 +00:00
if format == "json" {
2024-02-18 10:42:21 +00:00
buf.WriteString(message[1:])
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
buf.WriteString(`"message":`)
2024-02-18 10:42:21 +00:00
buf.WriteString(strconv.Quote(message))
2024-02-18 10:42:21 +00:00
buf.WriteString(`}`)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
// Text header
2024-02-18 10:42:21 +00:00
if len(s) > 0 {
2024-02-18 10:42:21 +00:00
buf.WriteByte(' ')
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
buf.WriteString(message)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
buf.WriteByte('\n')
2024-02-18 10:42:21 +00:00
l.mutex.Lock()
2024-02-18 10:42:21 +00:00
defer l.mutex.Unlock()
2024-02-18 10:42:21 +00:00
l.output.Write(buf.Bytes())
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}