refact: remove color sub package; allow intercepting logging

master v0.0.4
Evan Chen 2021-11-05 23:29:34 +08:00
parent 32b758725e
commit b8d296346c
6 changed files with 140 additions and 85 deletions

View File

@ -1,5 +1,41 @@
# Logging with color and format string
## Usage
```go
import (
"kumoly.io/lib/klog"
)
func main() {
Lklog.EVEL = Lerror | Ldebug | Lwarn | Linfo
klog.PROD = false
klog.Info("Hello, world.")
klog.Error("Error, world.")
// create sub logger
log := klog.Sub("me")
log.Warn("log with me")
// print fields
log.InfoF(klog.H{"Key":"value"}, "other ", "stuffs.")
// intercepting logs
p := log.GetPrinter()
log.SetPrinter(func(w io.Writer, d *Ldata, l *Logger) {
d.Message += " intercepted"
// NOT IN HERE
// as it will cause infinate loop
// p := log.GetPrinter()
p(w, d, l)
})
}
```
## Test
Mem: `go test -o tests/test.exe -memprofile tests/mem && go tool pprof -http : tests/mem`
@ -21,4 +57,12 @@ BenchmarkDefault-8 12410 109143 ns/op
BenchmarkSub1-8 10000 113340 ns/op
BenchmarkDefault-8 12186 112974 ns/op 21944 B/op 303 allocs/op
BenchmarkSub1-8 10000 114026 ns/op 22702 B/op 363 allocs/op
v0.0.4
goos: linux
goarch: amd64
pkg: kumoly.io/lib/klog
cpu: AMD Ryzen 9 5900X 12-Core Processor
BenchmarkDefault-12 24697 47673 ns/op 21337 B/op 291 allocs/op
BenchmarkSub1-12 22639 52841 ns/op 22103 B/op 351 allocs/op
```

View File

@ -3,11 +3,9 @@ package klog
import (
"fmt"
"strconv"
"kumoly.io/lib/klog/color"
)
func (l *Logger) M(s interface{}, attrs ...color.Attribute) string {
func (l *Logger) M(s interface{}, attrs ...Attribute) string {
if !l.color {
return fmt.Sprint(s)
}
@ -23,3 +21,67 @@ func (l *Logger) M(s interface{}, attrs ...color.Attribute) string {
}
return fmt.Sprintf("\033[%sm%s\033[0m", styles, s)
}
type Attribute int
// Base attributes
const (
Reset Attribute = iota
Bold
Faint
Italic
Underline
BlinkSlow
BlinkRapid
ReverseVideo
Concealed
CrossedOut
)
// Foreground text colors
const (
FgBlack Attribute = iota + 30
FgRed
FgGreen
FgYellow
FgBlue
FgMagenta
FgCyan
FgWhite
)
// Foreground Hi-Intensity text colors
const (
FgHiBlack Attribute = iota + 90
FgHiRed
FgHiGreen
FgHiYellow
FgHiBlue
FgHiMagenta
FgHiCyan
FgHiWhite
)
// Background text colors
const (
BgBlack Attribute = iota + 40
BgRed
BgGreen
BgYellow
BgBlue
BgMagenta
BgCyan
BgWhite
)
// Background Hi-Intensity text colors
const (
BgHiBlack Attribute = iota + 100
BgHiRed
BgHiGreen
BgHiYellow
BgHiBlue
BgHiMagenta
BgHiCyan
BgHiWhite
)

View File

@ -1,65 +0,0 @@
package color
type Attribute int
// Base attributes
const (
Reset Attribute = iota
Bold
Faint
Italic
Underline
BlinkSlow
BlinkRapid
ReverseVideo
Concealed
CrossedOut
)
// Foreground text colors
const (
FgBlack Attribute = iota + 30
FgRed
FgGreen
FgYellow
FgBlue
FgMagenta
FgCyan
FgWhite
)
// Foreground Hi-Intensity text colors
const (
FgHiBlack Attribute = iota + 90
FgHiRed
FgHiGreen
FgHiYellow
FgHiBlue
FgHiMagenta
FgHiCyan
FgHiWhite
)
// Background text colors
const (
BgBlack Attribute = iota + 40
BgRed
BgGreen
BgYellow
BgBlue
BgMagenta
BgCyan
BgWhite
)
// Background Hi-Intensity text colors
const (
BgHiBlack Attribute = iota + 100
BgHiRed
BgHiGreen
BgHiYellow
BgHiBlue
BgHiMagenta
BgHiCyan
BgHiWhite
)

19
log.go
View File

@ -9,7 +9,6 @@ import (
"time"
"github.com/mattn/go-isatty"
"kumoly.io/lib/klog/color"
)
type Llevel int
@ -76,21 +75,21 @@ func DefaultPrinter() Printer {
}
switch d.Level {
case Lerror:
level = l.M("ERROR", color.FgHiRed)
level = l.M("ERROR", FgHiRed)
caller = d.Caller
case Ldebug:
level = l.M("DEBUG", color.FgHiMagenta)
level = l.M("DEBUG", FgHiMagenta)
caller = d.Caller
case Lwarn:
level = l.M("WARN ", color.FgHiYellow)
level = l.M("WARN ", FgHiYellow)
case Linfo:
level = l.M("INFO ", color.FgHiBlue)
level = l.M("INFO ", FgHiBlue)
}
_, err := fmt.Fprintf(w, "%s [%s]%s%s %s%s\n%s",
d.Time.Format("2006/01/02 15:04:05"),
level, l.M(sys, color.FgHiCyan),
caller, l.M(d.Message, color.FgHiGreen), fields,
l.M(d.Stack, color.FgRed),
level, l.M(sys, FgHiCyan),
caller, l.M(d.Message, FgHiGreen), fields,
l.M(d.Stack, FgRed),
)
if err != nil {
fmt.Println(err)
@ -201,6 +200,10 @@ func (l *Logger) SetOutputAll(out io.Writer) {
}
}
func (l *Logger) GetPrinter() Printer {
return l.printer
}
func (l *Logger) SetPrinter(p Printer) {
l.printer = p
}

View File

@ -4,8 +4,6 @@ import (
"io"
"os"
"testing"
c "kumoly.io/lib/klog/color"
)
func TestDev(t *testing.T) {
@ -63,9 +61,9 @@ func TestSubProd(t *testing.T) {
func TestColoring(t *testing.T) {
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.FgHiGreen), ", ", l.M("world", c.Underline, c.FgMagenta))
Error(M("Hello", c.FgHiGreen), ", ", M("world", c.Underline, c.FgMagenta))
l.Error(l.M("Hello", BgHiGreen, Italic, FgWhite), ", ", l.M("world", FgBlue, BgHiYellow))
l.Error(l.M("Hello", FgHiGreen), ", ", l.M("world", Underline, FgMagenta))
Error(M("Hello", FgHiGreen), ", ", M("world", Underline, FgMagenta))
}
func TestToFile(t *testing.T) {
@ -173,3 +171,14 @@ func BenchmarkSub1(b *testing.B) {
l.WarnF(H{"test": "fields"}, "err in", "BenchmarkSub1: ", i)
}
}
func TestIntercept(t *testing.T) {
l := Sub("inter")
l.Info("original")
p := l.GetPrinter()
l.SetPrinter(func(w io.Writer, d *Ldata, l *Logger) {
d.Message += " intercepted"
p(w, d, l)
})
l.Info("original")
}

10
std.go
View File

@ -2,8 +2,6 @@ package klog
import (
"io"
"kumoly.io/lib/klog/color"
)
var std = New("")
@ -13,7 +11,11 @@ func DefaultLogger() *Logger {
}
func SetPrinter(p Printer) {
std.printer = p
std.SetPrinter(p)
}
func GetPrinter() Printer {
return std.GetPrinter()
}
func SetPrinterAll(p Printer) {
@ -22,7 +24,7 @@ func SetPrinterAll(p Printer) {
}
}
func M(s interface{}, attrs ...color.Attribute) string {
func M(s interface{}, attrs ...Attribute) string {
return std.M(s, attrs...)
}