Browse Source

perf: update logger encoder

dev
Dnomd343 11 months ago
parent
commit
5ca6d6a521
  1. 31
      logger/encoder.go

31
logger/encoder.go

@ -1,7 +1,6 @@
package logger package logger
import ( import (
"fmt"
"github.com/gookit/color" "github.com/gookit/color"
"github.com/petermattis/goid" "github.com/petermattis/goid"
"go.uber.org/zap/zapcore" "go.uber.org/zap/zapcore"
@ -11,6 +10,11 @@ import (
"time" "time"
) )
// / getGid return goroutine id with string.
func getGid() string {
return strconv.FormatInt(goid.Get(), 10)
}
// getCaller calculate relative source path of caller. // getCaller calculate relative source path of caller.
func getCaller(ec zapcore.EntryCaller, verbose bool) string { func getCaller(ec zapcore.EntryCaller, verbose bool) string {
file, err := filepath.Rel(project, ec.File) file, err := filepath.Rel(project, ec.File)
@ -32,33 +36,28 @@ func timeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
// timeColoredEncoder formats the time as a colored string // timeColoredEncoder formats the time as a colored string
// with custom prefix. // with custom prefix.
func timeColoredEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) { func timeColoredEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(fmt.Sprintf( enc.AppendString(color.Cyan.Render(logger.prefix))
"%s %s", enc.AppendString(color.Gray.Render(t.Format("2006-01-02 15:04:05.000")))
color.Cyan.Render(logger.prefix), // colored prefix
color.Gray.Render(t.Format("2006-01-02 15:04:05.000")),
))
} }
// callerEncoder formats caller in square brackets. // callerEncoder formats caller in square brackets.
func callerEncoder(ec zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { func callerEncoder(ec zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) {
if !logger.verbose { if logger.verbose {
enc.AppendString("[" + getGid() + "]")
enc.AppendString("[" + getCaller(ec, true) + "]")
} else {
enc.AppendString("[" + getCaller(ec, false) + "]") enc.AppendString("[" + getCaller(ec, false) + "]")
return
} }
enc.AppendString(fmt.Sprintf("[%d] [%s]", goid.Get(), getCaller(ec, true)))
} }
// callerColoredEncoder formats caller in square brackets with magenta color. // callerColoredEncoder formats caller in square brackets with magenta color.
func callerColoredEncoder(ec zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { func callerColoredEncoder(ec zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) {
if !logger.verbose { if logger.verbose {
enc.AppendString(color.Blue.Render("[" + getGid() + "]"))
enc.AppendString(color.Magenta.Render("[" + getCaller(ec, true) + "]"))
} else {
enc.AppendString(color.Magenta.Render("[" + getCaller(ec, false) + "]")) enc.AppendString(color.Magenta.Render("[" + getCaller(ec, false) + "]"))
return
} }
enc.AppendString(fmt.Sprintf(
"%s %s",
color.Blue.Render(fmt.Sprintf("[%d]", goid.Get())),
color.Magenta.Render("["+getCaller(ec, true)+"]"),
))
} }
// levelEncoder formats log level using square brackets. // levelEncoder formats log level using square brackets.

Loading…
Cancel
Save