feat: shorten color funcions

master
Evan Chen 2021-11-03 13:51:59 +08:00
parent 8fcc66e9e4
commit c540497ce6
4 changed files with 125 additions and 103 deletions

View File

@ -12,41 +12,40 @@ func (l *Logger) M(s interface{}, attrs ...color.Attribute) string {
if !l.color { if !l.color {
return fmt.Sprint(s) return fmt.Sprint(s)
} }
style := "" styles := ""
for i, v := range attrs { for i, v := range attrs {
if i >= 3 { if i >= 3 {
break break
} }
if i > 0 { if i > 0 {
style += ";" styles += ";"
} }
style = style + strconv.Itoa(int(v)) styles = styles + strconv.Itoa(int(v))
} }
return fmt.Sprintf("\033[%sm%s\033[0m", style, s) return fmt.Sprintf("\033[%sm%s\033[0m", styles, s)
} }
func setColorMap(funcMap template.FuncMap, color bool) { func (l *Logger) setColorMap(funcMap template.FuncMap) {
if color { funcMap["M"] = l.M
funcMap["red"] = func() string { return "\033[91m" } if l.color {
funcMap["redl"] = func() string { return "\033[31m" } funcMap["red"] = func(s interface{}) string { return fmt.Sprintf("\033[91m%s\033[0m", s) }
funcMap["green"] = func() string { return "\033[92m" } funcMap["redl"] = func(s interface{}) string { return fmt.Sprintf("\033[31m%s\033[0m", s) }
funcMap["yellow"] = func() string { return "\033[93m" } funcMap["green"] = func(s interface{}) string { return fmt.Sprintf("\033[92m%s\033[0m", s) }
funcMap["blue"] = func() string { return "\033[94m" } funcMap["yellow"] = func(s interface{}) string { return fmt.Sprintf("\033[93m%s\033[0m", s) }
funcMap["magenta"] = func() string { return "\033[95m" } funcMap["blue"] = func(s interface{}) string { return fmt.Sprintf("\033[94m%s\033[0m", s) }
funcMap["cyan"] = func() string { return "\033[96m" } funcMap["magenta"] = func(s interface{}) string { return fmt.Sprintf("\033[95m%s\033[0m", s) }
funcMap["white"] = func() string { return "\033[97m" } funcMap["cyan"] = func(s interface{}) string { return fmt.Sprintf("\033[96m%s\033[0m", s) }
funcMap["reset"] = func() string { return "\033[0m" } funcMap["white"] = func(s interface{}) string { return fmt.Sprintf("\033[97m%s\033[0m", s) }
return return
} }
funcMap["red"] = func() string { return "" } funcMap["red"] = func(s string) string { return s }
funcMap["redl"] = func() string { return "" } funcMap["redl"] = func(s string) string { return s }
funcMap["green"] = func() string { return "" } funcMap["green"] = func(s string) string { return s }
funcMap["yellow"] = func() string { return "" } funcMap["yellow"] = func(s string) string { return s }
funcMap["blue"] = func() string { return "" } funcMap["blue"] = func(s string) string { return s }
funcMap["magenta"] = func() string { return "" } funcMap["magenta"] = func(s string) string { return s }
funcMap["cyan"] = func() string { return "" } funcMap["cyan"] = func(s string) string { return s }
funcMap["white"] = func() string { return "" } funcMap["white"] = func(s string) string { return s }
funcMap["reset"] = func() string { return "" }
} }
// func // func

40
log.go
View File

@ -1,6 +1,7 @@
package log package log
import ( import (
"encoding/json"
"fmt" "fmt"
"io" "io"
"os" "os"
@ -33,24 +34,24 @@ var PROD = true
var LEVEL = Lerror | Linfo var LEVEL = Lerror | Linfo
const ( const (
DEFAULT_ERR = `{{Time}} [{{red}}ERROR{{reset}}]` + DEFAULT_ERR = `{{Time}} [{{"ERROR"|red}}]` +
`{{if .System}}({{cyan}}{{.System}}{{reset}}){{end}} ` + `{{if .System}}({{.System|cyan}}){{end}} ` +
`{{.Caller}} {{.Message}}{{"\n"}}` + `{{.Caller}} {{.Message}}` +
`{{if .Fields}}{{printf "%v\n" .Fields}}{{end}}` + `{{if .Fields}} {{.Fields|json|green}}{{end}}{{"\n"}}` +
`{{if .Stack}}{{redl}}{{.Stack}}{{reset}}{{end}}` `{{if .Stack}}{{.Stack|redl}}{{end}}`
DEFAULT_DEBUG = `{{Time}} [{{magenta}}DEBUG{{reset}}]` + DEFAULT_DEBUG = `{{Time}} [{{"DEBUG"|magenta}}]` +
`{{if .System}}({{cyan}}{{.System}}{{reset}}){{end}} ` + `{{if .System}}({{.System|cyan}}){{end}} ` +
`{{.Caller}} {{.Message}}{{"\n"}}` + `{{.Caller}} {{.Message}}` +
`{{if .Fields}}{{green}}{{printf "%v\n" .Fields}}{{reset}}{{end}}` + `{{if .Fields}} {{.Fields|json|green}}{{end}}{{"\n"}}` +
`{{if .Stack}}{{redl}}{{.Stack}}{{reset}}{{end}}` `{{if .Stack}}{{.Stack|redl}}{{end}}`
DEFAULT_WARN = `{{Time}} [{{yellow}}WARN {{reset}}]` + DEFAULT_WARN = `{{Time}} [{{"WARN"|yellow}} ]` +
`{{if .System}}({{cyan}}{{.System}}{{reset}}){{end}} ` + `{{if .System}}({{.System|cyan}}){{end}} ` +
`{{.Message}}{{"\n"}}` + `{{.Message}}` +
`{{if .Fields}}{{green}}{{printf "%v\n" .Fields}}{{reset}}{{end}}` `{{if .Fields}} {{.Fields|json|green}}{{end}}{{"\n"}}`
DEFAULT_INFO = `{{Time}} [{{blue}}INFO {{reset}}]` + DEFAULT_INFO = `{{Time}} [{{"INFO"|blue}} ]` +
`{{if .System}}({{cyan}}{{.System}}{{reset}}){{end}} ` + `{{if .System}}({{.System|cyan}}){{end}} ` +
`{{.Message}}{{"\n"}}` + `{{.Message}}` +
`{{if .Fields}}{{green}}{{printf "%v\n" .Fields}}{{reset}}{{end}}` `{{if .Fields}} {{.Fields|json|green}}{{end}}{{"\n"}}`
) )
type LogFormater struct { type LogFormater struct {
@ -196,6 +197,7 @@ func NewLogFormater() *LogFormater {
func (l *Logger) DefaultFuncMap() 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") },
"json": func(i interface{}) (string, error) { r, err := json.Marshal(i); return string(r), err },
} }
return funcMap return funcMap
} }
@ -205,7 +207,7 @@ func (l *Logger) ParseTmpl() error {
l.funcMap = l.DefaultFuncMap() l.funcMap = l.DefaultFuncMap()
} }
funcMap := copyFuncMap(l.funcMap) funcMap := copyFuncMap(l.funcMap)
setColorMap(funcMap, l.color) l.setColorMap(funcMap)
err_tmpl, err := template.New("err_tmpl").Funcs(funcMap).Parse(l.formatter.ErrTmplStr) err_tmpl, err := template.New("err_tmpl").Funcs(funcMap).Parse(l.formatter.ErrTmplStr)
if err != nil { if err != nil {

View File

@ -12,78 +12,86 @@ func TestDev(t *testing.T) {
LEVEL = Lerror | Ldebug | Lwarn | Linfo LEVEL = Lerror | Ldebug | Lwarn | Linfo
PROD = false PROD = false
// log.system = "dev" // log.system = "dev"
Error("d", "sdf", "sdfsdf") Error("TestDev")
ErrorF(H{"Test": "set"}, "d", "sdf", "sdfsdf") ErrorF(H{"Test": "set"}, "TestDev")
Debug("d", "sdf", "sdfsdf") Debug("TestDev")
DebugF(H{"Test": "set"}, "d", "sdf", "sdfsdf") DebugF(H{"Test": "set"}, "TestDev")
Warn("d", "sdf", "sdfsdf") Warn("TestDev")
WarnF(H{"Test": "set"}, "d", "sdf", "sdfsdf") WarnF(H{"Test": "set"}, "TestDev")
Info("d", "sdf", "sdfsdf") Info("TestDev")
InfoF(H{"Test": "set"}, "d", "sdf", "sdfsdf") InfoF(H{"Test": "set"}, "TestDev")
} }
func TestProd(t *testing.T) { func TestProd(t *testing.T) {
PROD = true PROD = true
// log.system = "prod" // log.system = "prod"
Error("d", "sdf", "sdfsdf") Error("TestProd")
ErrorF(H{"Test": "set"}, "d", "sdf", "sdfsdf") ErrorF(H{"Test": "set"}, "TestProd")
Debug("d", "sdf", "sdfsdf") Debug("TestProd")
DebugF(H{"Test": "set"}, "d", "sdf", "sdfsdf") DebugF(H{"Test": "set"}, "TestProd")
Warn("d", "sdf", "sdfsdf") Warn("TestProd")
WarnF(H{"Test": "set"}, "d", "sdf", "sdfsdf") WarnF(H{"Test": "set"}, "TestProd")
Info("d", "sdf", "sdfsdf") Info("TestProd")
InfoF(H{"Test": "set"}, "d", "sdf", "sdfsdf") InfoF(H{"Test": "set"}, "TestProd")
} }
func TestSubDev(t *testing.T) { func TestSubDev(t *testing.T) {
PROD = false PROD = false
l := Sub("TestSubDev") l := Sub("TestSubDev")
l.Error("d", "sdf", "sdfsdf") l.Error("TestSubDev")
l.ErrorF(H{"Test": "set"}, "d", "sdf", "sdfsdf") l.ErrorF(H{"Test": "set"}, "TestSubDev")
l.Debug("d", "sdf", "sdfsdf") l.Debug("TestSubDev")
l.DebugF(H{"Test": "set"}, "d", "sdf", "sdfsdf") l.DebugF(H{"Test": "set"}, "TestSubDev")
l.Warn("d", "sdf", "sdfsdf") l.Warn("TestSubDev")
l.WarnF(H{"Test": "set"}, "d", "sdf", "sdfsdf") l.WarnF(H{"Test": "set"}, "TestSubDev")
l.Info("d", "sdf", "sdfsdf") l.Info("TestSubDev")
l.InfoF(H{"Test": "set"}, "d", "sdf", "sdfsdf") l.InfoF(H{"Test": "set"}, "TestSubDev")
} }
func TestSubProd(t *testing.T) { func TestSubProd(t *testing.T) {
PROD = true PROD = true
l := Sub("TestSubProd") l := Sub("TestSubProd")
l.Error("d", "sdf", "sdfsdf") l.Error("TestSubProd")
l.ErrorF(H{"Test": "set"}, "d", "sdf", "sdfsdf") l.ErrorF(H{"Test": "set"}, "TestSubProd")
l.Debug("d", "sdf", "sdfsdf") l.Debug("TestSubProd")
l.DebugF(H{"Test": "set"}, "d", "sdf", "sdfsdf") l.DebugF(H{"Test": "set"}, "TestSubProd")
l.Warn("d", "sdf", "sdfsdf") l.Warn("TestSubProd")
l.WarnF(H{"Test": "set"}, "d", "sdf", "sdfsdf") l.WarnF(H{"Test": "set"}, "TestSubProd")
l.Info("d", "sdf", "sdfsdf") l.Info("TestSubProd")
l.InfoF(H{"Test": "set"}, "d", "sdf", "sdfsdf") l.InfoF(H{"Test": "set"}, "TestSubProd")
} }
func TestCustTmpl(t *testing.T) { func TestCustTmpl(t *testing.T) {
PROD = true PROD = true
l := Sub("TestCustTmpl") l := Sub("TestCustTmpl")
CustFormater := NewLogFormater() CustFormater := NewLogFormater()
CustFormater.InfoTmplStr = `{{Time}} [{{blue}}INFO {{reset}}]` + CustFormater.InfoTmplStr = `{{Time}} [{{ M "INFO" 104 93 9}} ]` +
`{{if .System}}({{cyan}}{{.System}}{{reset}}){{end}} ` + `{{if .System}}({{M .System 96}}){{end}} ` +
`key: test, value:{{.Fields.Test}}{{"\n"}}` `Hello, {{.Fields.Name}}. {{.Message}}{{"\n"}}`
CustFormater.ErrTmplStr = `{{Time}} [{{ M "ERROR" 91 43 1}}]` +
`{{if .System}}({{M .System 96}}){{end}} ` +
`NO! {{.Fields.Name}} {{M .Message 31 3}} {{"\n"}}`
l.SetTmpl(CustFormater, nil) l.SetTmpl(CustFormater, nil)
l.Reload() l.Reload()
l.Error("d", "sdf", "sdfsdf") l.ErrorF(H{"Name": "Brandon"}, "TestCustTmpl")
l.ErrorF(H{"Test": "set"}, "d", "sdf", "sdfsdf") l.InfoF(H{"Name": "Brandon"}, "TestCustTmpl")
l.Debug("d", "sdf", "sdfsdf")
l.DebugF(H{"Test": "set"}, "d", "sdf", "sdfsdf") SetTmpl(CustFormater, nil)
l.Warn("d", "sdf", "sdfsdf") Reload()
l.WarnF(H{"Test": "set"}, "d", "sdf", "sdfsdf")
l.Info("d", "sdf", "sdfsdf") ErrorF(H{"Name": "Brandon"}, "TestCustTmpl std")
l.InfoF(H{"Test": "set"}, "d", "sdf", "sdfsdf") InfoF(H{"Name": "Brandon"}, "TestCustTmpl std")
//reset
SetTmpl(NewLogFormater(), nil)
Reload()
} }
func TestColoring(t *testing.T) { func TestColoring(t *testing.T) {
l := Sub("color") l := Sub("color")
l.Error(l.M("Hello", c.BgHiGreen, c.Italic, c.FgWhite), ", ", l.M("world", c.FgBlue, c.BgHiYellow)) l.Error(l.M("Hello", c.BgHiGreen, c.Italic, c.FgWhite), ", ", l.M("world", c.FgBlue, c.BgHiYellow))
l.Error(l.M("Hello", c.FgHiGreen), ", ", l.M("world", c.Underline, c.FgMagenta)) l.Error(l.M("Hello", c.FgHiGreen), ", ", l.M("world", c.Underline, c.FgMagenta))
Error(M("Hello", c.FgHiGreen), ", ", M("world", c.Underline, c.FgMagenta))
} }
func TestToFile(t *testing.T) { func TestToFile(t *testing.T) {
@ -98,25 +106,17 @@ func TestToFile(t *testing.T) {
l.SetErrOutput(f) l.SetErrOutput(f)
l.Reload() l.Reload()
l.Error("d", "sdf", "sdfsdf") l.Error("TestToFile")
l.ErrorF(H{"Test": "set"}, "d", "sdf", "sdfsdf") l.Debug("TestToFile")
l.Debug("d", "sdf", "sdfsdf") l.Warn("TestToFile")
l.DebugF(H{"Test": "set"}, "d", "sdf", "sdfsdf") l.Info("TestToFile")
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")
l.SetColor(true) l.SetColor(true)
l.Reload() l.Reload()
l.Error("d", "sdf", "sdfsdf") l.Error("TestToFile colored")
l.ErrorF(H{"Test": "set"}, "d", "sdf", "sdfsdf") l.Debug("TestToFile colored")
l.Debug("d", "sdf", "sdfsdf") l.Warn("TestToFile colored")
l.DebugF(H{"Test": "set"}, "d", "sdf", "sdfsdf") l.Info("TestToFile colored")
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 TestApplyChild(t *testing.T) { func TestApplyChild(t *testing.T) {

23
std.go
View File

@ -1,9 +1,18 @@
package log package log
import "io" import (
"io"
"text/template"
"kumoly.io/core/log/color"
)
var std = New("") var std = New("")
func M(s interface{}, attrs ...color.Attribute) string {
return std.M(s, attrs...)
}
func Sub(sys string) *Logger { func Sub(sys string) *Logger {
return std.Sub(sys) return std.Sub(sys)
} }
@ -40,6 +49,18 @@ func ReloadAll() error {
return std.ReloadAll() return std.ReloadAll()
} }
func DefaultFuncMap() template.FuncMap {
return std.DefaultFuncMap()
}
func ParseTmpl() error {
return std.ParseTmpl()
}
func SetTmpl(formatter *LogFormater, funcMap template.FuncMap) {
std.SetTmpl(formatter, funcMap)
}
func ErrorF(fields H, v ...interface{}) { func ErrorF(fields H, v ...interface{}) {
if PROD { if PROD {
std.output(terror, 3, "", fields, v...) std.output(terror, 3, "", fields, v...)