1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- package slog
- import (
- "bytes"
- "fmt"
- "github.com/sirupsen/logrus"
- )
- const (
- nocolor = 0
- red = 31
- green = 32
- yellow = 33
- blue = 34
- gray = 39
- )
- var (
- formatParams = map[logrus.Level]formatParam{
- logrus.DebugLevel: formatParam{
- color: blue,
- levelText: "DEBUG",
- },
- logrus.InfoLevel: formatParam{
- color: green,
- levelText: "INFO",
- },
- logrus.WarnLevel: formatParam{
- color: yellow,
- levelText: "WARN",
- },
- logrus.ErrorLevel: formatParam{
- color: red,
- levelText: "ERROR",
- },
- }
- )
- type (
- formatter struct {
- logrus.TextFormatter
- }
- formatParam struct {
- color int
- levelText string
- }
- )
- func newFormatter(color bool) *formatter {
- return &formatter{
- logrus.TextFormatter{
- ForceColors: color,
- DisableColors: !color,
- },
- }
- }
- /**************************
- formatter function
- **************************/
- func (f *formatter) Format(entry *logrus.Entry) ([]byte, error) {
- formatParams := formatParams[entry.Level]
- b := &bytes.Buffer{}
- if f.ForceColors && !f.DisableColors {
- fmt.Fprintf(b,
- "\x1b[%dm[%-5s]\x1b[0m %s",
- formatParams.color,
- formatParams.levelText,
- entry.Message,
- )
- } else {
- fmt.Fprintf(b,
- "[%-5s] %s",
- formatParams.levelText,
- entry.Message,
- )
- }
- b.WriteByte('\n')
- return b.Bytes(), nil
- }
|