klog/log.go

284 lines
5.8 KiB
Go
Raw Normal View History

2021-11-01 14:38:30 +00:00
package log
import (
"fmt"
"io"
"os"
"text/template"
"time"
"github.com/mattn/go-isatty"
)
const (
Lerror = 1 << iota
2021-11-01 14:41:11 +00:00
Ldebug
2021-11-01 14:38:30 +00:00
Lwarn
Linfo
2021-11-01 14:41:11 +00:00
)
type tout int
const (
terror tout = iota
tdebug
tinfo
twarn
2021-11-01 14:38:30 +00:00
)
var PROD = true
var LEVEL = Lerror | Linfo
const (
2021-11-01 14:41:11 +00:00
DEFAULT_ERR = `{{Time}} [{{red}}ERROR{{reset}}]` +
`{{if .System}}({{cyan}}{{.System}}{{reset}}){{end}} ` +
`{{.Caller}} {{.Message}}{{"\n"}}` +
`{{if .Fields}}{{printf "%v\n" .Fields}}{{end}}` +
`{{if .Stack}}{{redl}}{{.Stack}}{{reset}}{{end}}`
DEFAULT_DEBUG = `{{Time}} [{{magenta}}DEBUG{{reset}}]` +
`{{if .System}}({{cyan}}{{.System}}{{reset}}){{end}} ` +
`{{.Caller}} {{.Message}}{{"\n"}}` +
`{{if .Fields}}{{green}}{{printf "%v\n" .Fields}}{{reset}}{{end}}` +
`{{if .Stack}}{{redl}}{{.Stack}}{{reset}}{{end}}`
DEFAULT_WARN = `{{Time}} [{{yellow}}WARN {{reset}}]` +
`{{if .System}}({{cyan}}{{.System}}{{reset}}){{end}} ` +
`{{.Message}}{{"\n"}}` +
`{{if .Fields}}{{green}}{{printf "%v\n" .Fields}}{{reset}}{{end}}`
DEFAULT_INFO = `{{Time}} [{{blue}}INFO {{reset}}]` +
`{{if .System}}({{cyan}}{{.System}}{{reset}}){{end}} ` +
`{{.Message}}{{"\n"}}` +
`{{if .Fields}}{{green}}{{printf "%v\n" .Fields}}{{reset}}{{end}}`
2021-11-01 14:38:30 +00:00
)
type LogFormater struct {
ErrTmplStr string
WarnTmplStr string
InfoTmplStr string
DebugTmplStr string
}
type H map[string]interface{}
type Ldata struct {
Message string
System string
// Caller only evaluates in DEBUG, and ERROR calls
Caller string
// Stack only eval when !PROD and {DEBUG, ERROR}
Stack string
Fields H
Color bool
}
type logger struct {
system string
color bool
err io.Writer
out io.Writer
2021-11-01 14:41:11 +00:00
formatter *LogFormater
2021-11-01 16:50:15 +00:00
funcMap template.FuncMap
2021-11-01 14:38:30 +00:00
err_tmpl *template.Template
2021-11-01 14:41:11 +00:00
debug_tmpl *template.Template
2021-11-01 14:38:30 +00:00
warn_tmpl *template.Template
info_tmpl *template.Template
}
2021-11-01 14:41:11 +00:00
func Default() *logger {
2021-11-01 14:38:30 +00:00
l := &logger{
2021-11-01 14:41:11 +00:00
err: os.Stderr,
out: os.Stdout,
color: true,
formatter: NewLogFormater(),
2021-11-01 14:38:30 +00:00
}
l.guessColor()
2021-11-01 14:41:11 +00:00
l.ParseTmpl()
2021-11-01 14:38:30 +00:00
return l
}
2021-11-01 14:41:11 +00:00
func (l *logger) Sub(sys string) *logger {
ret := &logger{
system: sys,
err: l.err,
out: l.out,
color: l.color,
formatter: l.formatter,
err_tmpl: l.err_tmpl,
debug_tmpl: l.debug_tmpl,
warn_tmpl: l.warn_tmpl,
info_tmpl: l.info_tmpl,
}
return ret
}
2021-11-01 14:38:30 +00:00
func (l *logger) guessColor() {
2021-11-01 16:50:15 +00:00
l.color = true
2021-11-01 14:38:30 +00:00
if w, ok := l.out.(*os.File); !ok || os.Getenv("TERM") == "dumb" ||
(!isatty.IsTerminal(w.Fd()) && !isatty.IsCygwinTerminal(w.Fd())) {
l.color = false
}
2021-11-01 16:50:15 +00:00
if w, ok := l.err.(*os.File); !ok || os.Getenv("TERM") == "dumb" ||
(!isatty.IsTerminal(w.Fd()) && !isatty.IsCygwinTerminal(w.Fd())) {
l.color = false
}
2021-11-01 14:38:30 +00:00
}
2021-11-01 16:50:15 +00:00
func (l *logger) SetColor(c bool) {
2021-11-01 14:38:30 +00:00
l.color = c
}
2021-11-01 16:50:15 +00:00
func (l *logger) SetErrOutput(err io.Writer) {
l.err = err
}
func (l *logger) SetOutput(out io.Writer) {
l.out = out
}
func (l *logger) Reload() error {
l.guessColor()
return l.ParseTmpl()
}
2021-11-01 14:38:30 +00:00
func NewLogFormater() *LogFormater {
return &LogFormater{
ErrTmplStr: DEFAULT_ERR,
WarnTmplStr: DEFAULT_WARN,
InfoTmplStr: DEFAULT_INFO,
DebugTmplStr: DEFAULT_DEBUG,
}
}
2021-11-01 16:50:15 +00:00
func (l *logger) DefaultFuncMap() template.FuncMap {
2021-11-01 14:38:30 +00:00
funcMap := template.FuncMap{
2021-11-01 14:41:11 +00:00
"Time": func() string { return time.Now().Format("2006/01/02 15:04:05") },
}
setColorMap(funcMap, l.color)
return funcMap
}
func (l *logger) ParseTmpl() error {
2021-11-01 16:50:15 +00:00
if l.funcMap == nil {
l.funcMap = l.DefaultFuncMap()
}
err_tmpl, err := template.New("err_tmpl").Funcs(l.funcMap).Parse(l.formatter.ErrTmplStr)
2021-11-01 14:38:30 +00:00
if err != nil {
return err
}
l.err_tmpl = err_tmpl
2021-11-01 16:50:15 +00:00
warn_tmpl, err := template.New("warn_tmpl").Funcs(l.funcMap).Parse(l.formatter.WarnTmplStr)
2021-11-01 14:41:11 +00:00
if err != nil {
return err
}
l.warn_tmpl = warn_tmpl
2021-11-01 16:50:15 +00:00
info_tmpl, err := template.New("info_tmpl").Funcs(l.funcMap).Parse(l.formatter.InfoTmplStr)
2021-11-01 14:41:11 +00:00
if err != nil {
return err
}
l.info_tmpl = info_tmpl
2021-11-01 16:50:15 +00:00
debug_tmpl, err := template.New("debug_tmpl").Funcs(l.funcMap).Parse(l.formatter.DebugTmplStr)
2021-11-01 14:41:11 +00:00
if err != nil {
return err
}
l.debug_tmpl = debug_tmpl
2021-11-01 14:38:30 +00:00
return nil
}
2021-11-01 16:50:15 +00:00
func (l *logger) SetTmpl(formatter *LogFormater, funcMap template.FuncMap) {
2021-11-01 14:41:11 +00:00
l.formatter = formatter
2021-11-01 16:50:15 +00:00
if funcMap != nil {
l.funcMap = funcMap
}
2021-11-01 14:41:11 +00:00
}
func (l *logger) output(t tout, depth int, stack string, fields H, v ...interface{}) {
msg := fmt.Sprint(v...)
2021-11-01 14:38:30 +00:00
data := Ldata{
Fields: fields,
Message: msg,
System: l.system,
Caller: caller(depth),
Color: l.color,
Stack: stack,
}
2021-11-01 14:41:11 +00:00
var err error
switch t {
case terror:
2021-11-01 16:18:20 +00:00
if LEVEL&Lerror == 0 {
return
}
2021-11-01 14:41:11 +00:00
err = l.err_tmpl.Execute(l.err, data)
case tdebug:
2021-11-01 16:18:20 +00:00
if LEVEL&Ldebug == 0 {
return
}
2021-11-01 14:41:11 +00:00
err = l.debug_tmpl.Execute(l.err, data)
case twarn:
2021-11-01 16:18:20 +00:00
if LEVEL&Lwarn == 0 {
return
}
2021-11-01 14:41:11 +00:00
err = l.warn_tmpl.Execute(l.out, data)
case tinfo:
2021-11-01 16:18:20 +00:00
if LEVEL&Linfo == 0 {
return
}
2021-11-01 14:41:11 +00:00
err = l.info_tmpl.Execute(l.out, data)
}
2021-11-01 14:38:30 +00:00
if err != nil {
2021-11-01 14:41:11 +00:00
fmt.Fprintln(l.err, "[FATAL] logger error:", err)
2021-11-01 14:38:30 +00:00
panic(err)
}
}
func (l *logger) ErrorF(fields H, v ...interface{}) {
2021-11-01 14:41:11 +00:00
if PROD {
l.output(terror, 3, "", fields, v...)
} else {
l.output(terror, 3, stack(), fields, v...)
2021-11-01 14:38:30 +00:00
}
}
func (l *logger) Error(v ...interface{}) {
2021-11-01 14:41:11 +00:00
if PROD {
l.output(terror, 3, "", H{}, v...)
} else {
l.output(terror, 3, stack(), H{}, v...)
2021-11-01 14:38:30 +00:00
}
}
2021-11-01 14:41:11 +00:00
func (l *logger) DebugF(fields H, v ...interface{}) {
if PROD {
l.output(tdebug, 3, "", fields, v...)
} else {
l.output(tdebug, 3, stack(), fields, v...)
2021-11-01 14:38:30 +00:00
}
}
2021-11-01 14:41:11 +00:00
func (l *logger) Debug(v ...interface{}) {
if PROD {
l.output(tdebug, 3, "", H{}, v...)
} else {
l.output(tdebug, 3, stack(), H{}, v...)
2021-11-01 14:38:30 +00:00
}
}
2021-11-01 14:41:11 +00:00
func (l *logger) WarnF(fields H, v ...interface{}) {
l.output(twarn, 3, "", fields, v...)
}
func (l *logger) Warn(v ...interface{}) {
l.output(twarn, 3, "", H{}, v...)
}
func (l *logger) InfoF(fields H, v ...interface{}) {
l.output(tinfo, 3, "", fields, v...)
}
func (l *logger) Info(v ...interface{}) {
l.output(tinfo, 3, "", H{}, v...)
}