gmessages/libgm/debug/logger.go

45 lines
1 KiB
Go
Raw Normal View History

2023-06-30 09:54:08 +00:00
package debug
import (
"fmt"
"time"
2023-06-30 09:55:49 +00:00
2023-06-30 09:54:08 +00:00
"github.com/mattn/go-colorable"
zerolog "github.com/rs/zerolog"
)
var colors = map[string]string{
2023-06-30 09:55:49 +00:00
"text": "\x1b[38;5;6m%s\x1b[0m",
2023-06-30 09:54:08 +00:00
"debug": "\x1b[32mDEBUG\x1b[0m",
2023-06-30 09:55:49 +00:00
"gray": "\x1b[38;5;8m%s\x1b[0m",
"info": "\x1b[38;5;111mINFO\x1b[0m",
2023-06-30 09:54:08 +00:00
"error": "\x1b[38;5;204mERROR\x1b[0m",
"fatal": "\x1b[38;5;52mFATAL\x1b[0m",
}
var output = zerolog.ConsoleWriter{
2023-06-30 09:55:49 +00:00
Out: colorable.NewColorableStdout(),
2023-06-30 09:54:08 +00:00
TimeFormat: time.ANSIC,
FormatLevel: func(i interface{}) string {
name := fmt.Sprintf("%s", i)
coloredName := colors[name]
return coloredName
},
FormatMessage: func(i interface{}) string {
coloredMsg := fmt.Sprintf(colors["text"], i)
return coloredMsg
},
FormatFieldName: func(i interface{}) string {
name := fmt.Sprintf("%s", i)
return fmt.Sprintf(colors["gray"], name+"=")
},
FormatFieldValue: func(i interface{}) string {
return fmt.Sprintf("%s", i)
},
NoColor: false,
}
func NewLogger() zerolog.Logger {
return zerolog.New(output).With().Timestamp().Logger()
2023-06-30 09:55:49 +00:00
}