fix: use reload instead

master
Evan Chen 2021-11-02 00:50:15 +08:00
parent 33ccd2714f
commit ccfbb72546
4 changed files with 62 additions and 11 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
dist

View File

@ -0,0 +1 @@
# Logging with color and format string

41
log.go
View File

@ -81,6 +81,7 @@ type logger struct {
out io.Writer out io.Writer
formatter *LogFormater formatter *LogFormater
funcMap template.FuncMap
err_tmpl *template.Template err_tmpl *template.Template
debug_tmpl *template.Template debug_tmpl *template.Template
warn_tmpl *template.Template warn_tmpl *template.Template
@ -116,15 +117,33 @@ func (l *logger) Sub(sys string) *logger {
} }
func (l *logger) guessColor() { func (l *logger) guessColor() {
l.color = true
if w, ok := l.out.(*os.File); !ok || os.Getenv("TERM") == "dumb" || if w, ok := l.out.(*os.File); !ok || os.Getenv("TERM") == "dumb" ||
(!isatty.IsTerminal(w.Fd()) && !isatty.IsCygwinTerminal(w.Fd())) { (!isatty.IsTerminal(w.Fd()) && !isatty.IsCygwinTerminal(w.Fd())) {
l.color = false l.color = false
} }
if w, ok := l.err.(*os.File); !ok || os.Getenv("TERM") == "dumb" ||
(!isatty.IsTerminal(w.Fd()) && !isatty.IsCygwinTerminal(w.Fd())) {
l.color = false
}
} }
func (l *logger) Color(c bool) { func (l *logger) SetColor(c bool) {
l.color = c l.color = c
} }
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()
}
func NewLogFormater() *LogFormater { func NewLogFormater() *LogFormater {
return &LogFormater{ return &LogFormater{
ErrTmplStr: DEFAULT_ERR, ErrTmplStr: DEFAULT_ERR,
@ -134,7 +153,7 @@ func NewLogFormater() *LogFormater {
} }
} }
func (l *logger) FuncMap() template.FuncMap { func (l *logger) DefaultFuncMap() template.FuncMap {
funcMap := template.FuncMap{ funcMap := template.FuncMap{
"Time": func() string { return time.Now().Format("2006/01/02 15:04:05") }, "Time": func() string { return time.Now().Format("2006/01/02 15:04:05") },
} }
@ -143,23 +162,25 @@ func (l *logger) FuncMap() template.FuncMap {
} }
func (l *logger) ParseTmpl() error { func (l *logger) ParseTmpl() error {
funcMap := l.FuncMap() if l.funcMap == nil {
err_tmpl, err := template.New("err_tmpl").Funcs(funcMap).Parse(l.formatter.ErrTmplStr) l.funcMap = l.DefaultFuncMap()
}
err_tmpl, err := template.New("err_tmpl").Funcs(l.funcMap).Parse(l.formatter.ErrTmplStr)
if err != nil { if err != nil {
return err return err
} }
l.err_tmpl = err_tmpl l.err_tmpl = err_tmpl
warn_tmpl, err := template.New("warn_tmpl").Funcs(funcMap).Parse(l.formatter.WarnTmplStr) warn_tmpl, err := template.New("warn_tmpl").Funcs(l.funcMap).Parse(l.formatter.WarnTmplStr)
if err != nil { if err != nil {
return err return err
} }
l.warn_tmpl = warn_tmpl l.warn_tmpl = warn_tmpl
info_tmpl, err := template.New("info_tmpl").Funcs(funcMap).Parse(l.formatter.InfoTmplStr) info_tmpl, err := template.New("info_tmpl").Funcs(l.funcMap).Parse(l.formatter.InfoTmplStr)
if err != nil { if err != nil {
return err return err
} }
l.info_tmpl = info_tmpl l.info_tmpl = info_tmpl
debug_tmpl, err := template.New("debug_tmpl").Funcs(funcMap).Parse(l.formatter.DebugTmplStr) debug_tmpl, err := template.New("debug_tmpl").Funcs(l.funcMap).Parse(l.formatter.DebugTmplStr)
if err != nil { if err != nil {
return err return err
} }
@ -167,9 +188,11 @@ func (l *logger) ParseTmpl() error {
return nil return nil
} }
func (l *logger) SetTmpl(formatter *LogFormater) error { func (l *logger) SetTmpl(formatter *LogFormater, funcMap template.FuncMap) {
l.formatter = formatter l.formatter = formatter
return l.ParseTmpl() if funcMap != nil {
l.funcMap = funcMap
}
} }
func (l *logger) output(t tout, depth int, stack string, fields H, v ...interface{}) { func (l *logger) output(t tout, depth int, stack string, fields H, v ...interface{}) {

View File

@ -1,6 +1,9 @@
package log package log
import "testing" import (
"os"
"testing"
)
func TestDev(t *testing.T) { func TestDev(t *testing.T) {
PROD = false PROD = false
@ -61,7 +64,30 @@ func TestCustTmpl(t *testing.T) {
CustFormater.InfoTmplStr = `{{Time}} [{{blue}}INFO {{reset}}]` + CustFormater.InfoTmplStr = `{{Time}} [{{blue}}INFO {{reset}}]` +
`{{if .System}}({{cyan}}{{.System}}{{reset}}){{end}} ` + `{{if .System}}({{cyan}}{{.System}}{{reset}}){{end}} ` +
`key: test, value:{{.Fields.Test}}{{"\n"}}` `key: test, value:{{.Fields.Test}}{{"\n"}}`
l.SetTmpl(CustFormater) l.SetTmpl(CustFormater, nil)
l.Reload()
l.Error("d", "sdf", "sdfsdf")
l.ErrorF(H{"Test": "set"}, "d", "sdf", "sdfsdf")
l.Debug("d", "sdf", "sdfsdf")
l.DebugF(H{"Test": "set"}, "d", "sdf", "sdfsdf")
l.Warn("d", "sdf", "sdfsdf")
l.WarnF(H{"Test": "set"}, "d", "sdf", "sdfsdf")
l.Info("d", "sdf", "sdfsdf")
l.InfoF(H{"Test": "set"}, "d", "sdf", "sdfsdf")
}
func TestToFile(t *testing.T) {
PROD = true
l := Sub("TestToFile")
f, err := os.OpenFile("dist/test.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
t.Error(err)
}
l.SetOutput(f)
l.SetErrOutput(f)
l.Reload()
l.Error("d", "sdf", "sdfsdf") l.Error("d", "sdf", "sdfsdf")
l.ErrorF(H{"Test": "set"}, "d", "sdf", "sdfsdf") l.ErrorF(H{"Test": "set"}, "d", "sdf", "sdfsdf")
l.Debug("d", "sdf", "sdfsdf") l.Debug("d", "sdf", "sdfsdf")