fix: logger type not exported

master
Evan Chen 2021-11-02 02:07:05 +08:00
parent e02d0106a6
commit a870c866e0
2 changed files with 24 additions and 24 deletions

46
log.go
View File

@ -73,7 +73,7 @@ type Ldata struct {
Color bool Color bool
} }
type logger struct { type Logger struct {
system string system string
color bool color bool
@ -88,8 +88,8 @@ type logger struct {
info_tmpl *template.Template info_tmpl *template.Template
} }
func Default() *logger { func Default() *Logger {
l := &logger{ l := &Logger{
err: os.Stderr, err: os.Stderr,
out: os.Stdout, out: os.Stdout,
color: true, color: true,
@ -99,8 +99,8 @@ func Default() *logger {
return l return l
} }
func (l *logger) Sub(sys string) *logger { func (l *Logger) Sub(sys string) *Logger {
ret := &logger{ ret := &Logger{
system: sys, system: sys,
err: l.err, err: l.err,
out: l.out, out: l.out,
@ -115,7 +115,7 @@ func (l *logger) Sub(sys string) *logger {
return ret return ret
} }
func (l *logger) guessColor() { func (l *Logger) guessColor() {
l.color = true 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())) {
@ -126,19 +126,19 @@ func (l *logger) guessColor() {
l.color = false l.color = false
} }
} }
func (l *logger) SetColor(c bool) { func (l *Logger) SetColor(c bool) {
l.color = c l.color = c
} }
func (l *logger) SetErrOutput(err io.Writer) { func (l *Logger) SetErrOutput(err io.Writer) {
l.err = err l.err = err
} }
func (l *logger) SetOutput(out io.Writer) { func (l *Logger) SetOutput(out io.Writer) {
l.out = out l.out = out
} }
func (l *logger) Reload() error { func (l *Logger) Reload() error {
l.guessColor() l.guessColor()
return l.ParseTmpl() return l.ParseTmpl()
} }
@ -152,7 +152,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") },
} }
@ -160,7 +160,7 @@ func (l *logger) DefaultFuncMap() template.FuncMap {
return funcMap return funcMap
} }
func (l *logger) ParseTmpl() error { func (l *Logger) ParseTmpl() error {
if l.funcMap == nil { if l.funcMap == nil {
l.funcMap = l.DefaultFuncMap() l.funcMap = l.DefaultFuncMap()
} }
@ -187,14 +187,14 @@ func (l *logger) ParseTmpl() error {
return nil return nil
} }
func (l *logger) SetTmpl(formatter *LogFormater, funcMap template.FuncMap) { func (l *Logger) SetTmpl(formatter *LogFormater, funcMap template.FuncMap) {
l.formatter = formatter l.formatter = formatter
if funcMap != nil { if funcMap != nil {
l.funcMap = funcMap 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{}) {
msg := fmt.Sprint(v...) msg := fmt.Sprint(v...)
data := Ldata{ data := Ldata{
Fields: fields, Fields: fields,
@ -228,12 +228,12 @@ func (l *logger) output(t tout, depth int, stack string, fields H, v ...interfac
err = l.info_tmpl.Execute(l.out, data) err = l.info_tmpl.Execute(l.out, data)
} }
if err != nil { if err != nil {
fmt.Fprintln(l.err, "[FATAL] logger error:", err) fmt.Fprintln(l.err, "[FATAL] Logger error:", err)
panic(err) panic(err)
} }
} }
func (l *logger) ErrorF(fields H, v ...interface{}) { func (l *Logger) ErrorF(fields H, v ...interface{}) {
if PROD { if PROD {
l.output(terror, 3, "", fields, v...) l.output(terror, 3, "", fields, v...)
} else { } else {
@ -241,7 +241,7 @@ func (l *logger) ErrorF(fields H, v ...interface{}) {
} }
} }
func (l *logger) Error(v ...interface{}) { func (l *Logger) Error(v ...interface{}) {
if PROD { if PROD {
l.output(terror, 3, "", H{}, v...) l.output(terror, 3, "", H{}, v...)
} else { } else {
@ -249,7 +249,7 @@ func (l *logger) Error(v ...interface{}) {
} }
} }
func (l *logger) DebugF(fields H, v ...interface{}) { func (l *Logger) DebugF(fields H, v ...interface{}) {
if PROD { if PROD {
l.output(tdebug, 3, "", fields, v...) l.output(tdebug, 3, "", fields, v...)
} else { } else {
@ -257,7 +257,7 @@ func (l *logger) DebugF(fields H, v ...interface{}) {
} }
} }
func (l *logger) Debug(v ...interface{}) { func (l *Logger) Debug(v ...interface{}) {
if PROD { if PROD {
l.output(tdebug, 3, "", H{}, v...) l.output(tdebug, 3, "", H{}, v...)
} else { } else {
@ -265,18 +265,18 @@ func (l *logger) Debug(v ...interface{}) {
} }
} }
func (l *logger) WarnF(fields H, v ...interface{}) { func (l *Logger) WarnF(fields H, v ...interface{}) {
l.output(twarn, 3, "", fields, v...) l.output(twarn, 3, "", fields, v...)
} }
func (l *logger) Warn(v ...interface{}) { func (l *Logger) Warn(v ...interface{}) {
l.output(twarn, 3, "", H{}, v...) l.output(twarn, 3, "", H{}, v...)
} }
func (l *logger) InfoF(fields H, v ...interface{}) { func (l *Logger) InfoF(fields H, v ...interface{}) {
l.output(tinfo, 3, "", fields, v...) l.output(tinfo, 3, "", fields, v...)
} }
func (l *logger) Info(v ...interface{}) { func (l *Logger) Info(v ...interface{}) {
l.output(tinfo, 3, "", H{}, v...) l.output(tinfo, 3, "", H{}, v...)
} }

2
std.go
View File

@ -2,7 +2,7 @@ package log
var std = Default() var std = Default()
func Sub(sys string) *logger { func Sub(sys string) *Logger {
return std.Sub(sys) return std.Sub(sys)
} }