feat: logx with color (#1872)
* feat: logx with color * chore: update logs * fix test error * chore: change colors of http codes * chore: add comments * chore: use faith/color instead of ascii code color * chore: update colors * chore: update colors * chore: fix duplicated slowcall text * chore: remove slowcall colors
This commit is contained in:
73
core/color/color.go
Normal file
73
core/color/color.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package color
|
||||
|
||||
import "github.com/fatih/color"
|
||||
|
||||
const (
|
||||
// NoColor is no color for both foreground and background.
|
||||
NoColor Color = iota
|
||||
// FgBlack is the foreground color black.
|
||||
FgBlack
|
||||
// FgRed is the foreground color red.
|
||||
FgRed
|
||||
// FgGreen is the foreground color green.
|
||||
FgGreen
|
||||
// FgYellow is the foreground color yellow.
|
||||
FgYellow
|
||||
// FgBlue is the foreground color blue.
|
||||
FgBlue
|
||||
// FgMagenta is the foreground color magenta.
|
||||
FgMagenta
|
||||
// FgCyan is the foreground color cyan.
|
||||
FgCyan
|
||||
// FgWhite is the foreground color white.
|
||||
FgWhite
|
||||
|
||||
// BgBlack is the background color black.
|
||||
BgBlack
|
||||
// BgRed is the background color red.
|
||||
BgRed
|
||||
// BgGreen is the background color green.
|
||||
BgGreen
|
||||
// BgYellow is the background color yellow.
|
||||
BgYellow
|
||||
// BgBlue is the background color blue.
|
||||
BgBlue
|
||||
// BgMagenta is the background color magenta.
|
||||
BgMagenta
|
||||
// BgCyan is the background color cyan.
|
||||
BgCyan
|
||||
// BgWhite is the background color white.
|
||||
BgWhite
|
||||
)
|
||||
|
||||
var colors = map[Color][]color.Attribute{
|
||||
FgBlack: {color.FgBlack, color.Bold},
|
||||
FgRed: {color.FgRed, color.Bold},
|
||||
FgGreen: {color.FgGreen, color.Bold},
|
||||
FgYellow: {color.FgYellow, color.Bold},
|
||||
FgBlue: {color.FgBlue, color.Bold},
|
||||
FgMagenta: {color.FgMagenta, color.Bold},
|
||||
FgCyan: {color.FgCyan, color.Bold},
|
||||
FgWhite: {color.FgWhite, color.Bold},
|
||||
BgBlack: {color.BgBlack, color.FgHiWhite, color.Bold},
|
||||
BgRed: {color.BgRed, color.FgHiWhite, color.Bold},
|
||||
BgGreen: {color.BgGreen, color.FgHiWhite, color.Bold},
|
||||
BgYellow: {color.BgHiYellow, color.FgHiBlack, color.Bold},
|
||||
BgBlue: {color.BgBlue, color.FgHiWhite, color.Bold},
|
||||
BgMagenta: {color.BgMagenta, color.FgHiWhite, color.Bold},
|
||||
BgCyan: {color.BgCyan, color.FgHiWhite, color.Bold},
|
||||
BgWhite: {color.BgHiWhite, color.FgHiBlack, color.Bold},
|
||||
}
|
||||
|
||||
type Color uint32
|
||||
|
||||
// WithColor returns a string with the given color applied.
|
||||
func WithColor(text string, colour Color) string {
|
||||
c := color.New(colors[colour]...)
|
||||
return c.Sprint(text)
|
||||
}
|
||||
|
||||
// WithColorPadding returns a string with the given color applied with leading and trailing spaces.
|
||||
func WithColorPadding(text string, colour Color) string {
|
||||
return WithColor(" "+text+" ", colour)
|
||||
}
|
||||
17
core/color/color_test.go
Normal file
17
core/color/color_test.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package color
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestWithColor(t *testing.T) {
|
||||
output := WithColor("Hello", BgRed)
|
||||
assert.Equal(t, "Hello", output)
|
||||
}
|
||||
|
||||
func TestWithColorPadding(t *testing.T) {
|
||||
output := WithColorPadding("Hello", BgRed)
|
||||
assert.Equal(t, " Hello ", output)
|
||||
}
|
||||
26
core/logx/color.go
Normal file
26
core/logx/color.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package logx
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/color"
|
||||
)
|
||||
|
||||
// WithColor is a helper function to add color to a string, only in plain encoding.
|
||||
func WithColor(text string, colour color.Color) string {
|
||||
if atomic.LoadUint32(&encoding) == plainEncodingType {
|
||||
return color.WithColor(text, colour)
|
||||
}
|
||||
|
||||
return text
|
||||
}
|
||||
|
||||
// WithColorPadding is a helper function to add color to a string with leading and trailing spaces,
|
||||
// only in plain encoding.
|
||||
func WithColorPadding(text string, colour color.Color) string {
|
||||
if atomic.LoadUint32(&encoding) == plainEncodingType {
|
||||
return color.WithColorPadding(text, colour)
|
||||
}
|
||||
|
||||
return text
|
||||
}
|
||||
33
core/logx/color_test.go
Normal file
33
core/logx/color_test.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package logx
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/zeromicro/go-zero/core/color"
|
||||
)
|
||||
|
||||
func TestWithColor(t *testing.T) {
|
||||
old := atomic.SwapUint32(&encoding, plainEncodingType)
|
||||
defer atomic.StoreUint32(&encoding, old)
|
||||
|
||||
output := WithColor("hello", color.BgBlue)
|
||||
assert.Equal(t, "hello", output)
|
||||
|
||||
atomic.StoreUint32(&encoding, jsonEncodingType)
|
||||
output = WithColor("hello", color.BgBlue)
|
||||
assert.Equal(t, "hello", output)
|
||||
}
|
||||
|
||||
func TestWithColorPadding(t *testing.T) {
|
||||
old := atomic.SwapUint32(&encoding, plainEncodingType)
|
||||
defer atomic.StoreUint32(&encoding, old)
|
||||
|
||||
output := WithColorPadding("hello", color.BgBlue)
|
||||
assert.Equal(t, " hello ", output)
|
||||
|
||||
atomic.StoreUint32(&encoding, jsonEncodingType)
|
||||
output = WithColorPadding("hello", color.BgBlue)
|
||||
assert.Equal(t, "hello", output)
|
||||
}
|
||||
@@ -223,13 +223,13 @@ func SetUp(c LogConf) error {
|
||||
}
|
||||
|
||||
switch c.Mode {
|
||||
case consoleMode:
|
||||
setupWithConsole()
|
||||
return nil
|
||||
case fileMode:
|
||||
return setupWithFiles(c)
|
||||
case volumeMode:
|
||||
return setupWithVolume(c)
|
||||
default:
|
||||
return setupWithFiles(c)
|
||||
setupWithConsole()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -210,6 +210,12 @@ func (l *RotateLogger) maybeCompressFile(file string) {
|
||||
ErrorStack(r)
|
||||
}
|
||||
}()
|
||||
|
||||
if _, err := os.Stat(file); err != nil {
|
||||
// file not exists or other error, ignore compression
|
||||
return
|
||||
}
|
||||
|
||||
compressLogFile(file)
|
||||
}
|
||||
|
||||
@@ -292,7 +298,7 @@ func compressLogFile(file string) {
|
||||
start := time.Now()
|
||||
Infof("compressing log file: %s", file)
|
||||
if err := gzipFile(file); err != nil {
|
||||
ErrorStackf("compress error: %s", err)
|
||||
Errorf("compress error: %s", err)
|
||||
} else {
|
||||
Infof("compressed log file: %s, took %s", file, time.Since(start))
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ const (
|
||||
statFilename = "stat.log"
|
||||
|
||||
consoleMode = "console"
|
||||
fileMode = "file"
|
||||
volumeMode = "volume"
|
||||
|
||||
levelAlert = "alert"
|
||||
|
||||
@@ -10,6 +10,8 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/color"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -239,6 +241,7 @@ func output(writer io.Writer, level string, val interface{}, fields ...LogField)
|
||||
|
||||
switch atomic.LoadUint32(&encoding) {
|
||||
case plainEncodingType:
|
||||
level = wrapLevelWithColor(level)
|
||||
writePlainAny(writer, level, val, buildFields(fields...)...)
|
||||
default:
|
||||
entry := make(logEntryWithFields)
|
||||
@@ -252,6 +255,30 @@ func output(writer io.Writer, level string, val interface{}, fields ...LogField)
|
||||
}
|
||||
}
|
||||
|
||||
func wrapLevelWithColor(level string) string {
|
||||
var colour color.Color
|
||||
switch level {
|
||||
case levelAlert:
|
||||
colour = color.FgRed
|
||||
case levelError:
|
||||
colour = color.FgRed
|
||||
case levelFatal:
|
||||
colour = color.FgRed
|
||||
case levelInfo:
|
||||
colour = color.FgBlue
|
||||
case levelSlow:
|
||||
colour = color.FgYellow
|
||||
case levelStat:
|
||||
colour = color.FgGreen
|
||||
}
|
||||
|
||||
if colour == color.NoColor {
|
||||
return level
|
||||
}
|
||||
|
||||
return color.WithColorPadding(level, colour)
|
||||
}
|
||||
|
||||
func writeJson(writer io.Writer, info interface{}) {
|
||||
if content, err := json.Marshal(info); err != nil {
|
||||
log.Println(err.Error())
|
||||
|
||||
Reference in New Issue
Block a user