Skip to content

Commit

Permalink
Do color encoding internally
Browse files Browse the repository at this point in the history
  • Loading branch information
bufdev committed Feb 20, 2017
1 parent 862920f commit ea8f67c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 34 deletions.
11 changes: 7 additions & 4 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"os"
"testing"

"go.uber.org/zap/zapcore"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -47,9 +49,9 @@ func TestConfig(t *testing.T) {
desc: "development",
cfg: NewDevelopmentConfig(),
expectN: 3 + 200, // 3 initial logs, all 200 subsequent logs
expectRe: "DEBUG\t.*go.uber.org/zap/config_test.go:" + `\d+` + "\tdebug\t" + `{"k": "v", "z": "zz"}` + "\n" +
"INFO\t.*go.uber.org/zap/config_test.go:" + `\d+` + "\tinfo\t" + `{"k": "v", "z": "zz"}` + "\n" +
"WARN\t.*go.uber.org/zap/config_test.go:" + `\d+` + "\twarn\t" + `{"k": "v", "z": "zz"}` + "\n" +
expectRe: "debug\t.*go.uber.org/zap/config_test.go:" + `\d+` + "\tdebug\t" + `{"k": "v", "z": "zz"}` + "\n" +
"info\t.*go.uber.org/zap/config_test.go:" + `\d+` + "\tinfo\t" + `{"k": "v", "z": "zz"}` + "\n" +
"warn\t.*go.uber.org/zap/config_test.go:" + `\d+` + "\twarn\t" + `{"k": "v", "z": "zz"}` + "\n" +
`goroutine \d+ \[running\]:`,
},
}
Expand All @@ -61,7 +63,8 @@ func TestConfig(t *testing.T) {
defer os.Remove(temp.Name())

tt.cfg.OutputPaths = []string{temp.Name()}
tt.cfg.EncoderConfig.TimeKey = "" // no timestamps in tests
tt.cfg.EncoderConfig.TimeKey = "" // no timestamps in tests
tt.cfg.EncoderConfig.EncodeLevel = zapcore.LowercaseLevelEncoder // no tty in tests
tt.cfg.InitialFields = map[string]interface{}{"z": "zz", "k": "v"}

hook, count := makeCountingHook()
Expand Down
24 changes: 12 additions & 12 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package: go.uber.org/zap
license: MIT
import:
- package: go.uber.org/atomic
- package: github.com/fatih/color
version: ~1.4.0
testImport:
- package: github.com/satori/go.uuid
- package: github.com/sirupsen/logrus
Expand Down
50 changes: 34 additions & 16 deletions zapcore/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,35 @@
package zapcore

import (
"fmt"
"strings"
"time"

"github.com/fatih/color"

"go.uber.org/zap/buffer"
)

const (
fgBlack colorCode = iota + 30
fgRed
fgGreen
fgYellow
fgBlue
fgMagenta
fgCyan
fgWhite
)

type colorCode int

var (
// TODO(pedge): add a test to verify all levels covered? It would be nice
// to maintain a slice of all levels in level.go for testing, even if there
// is already _minLevel and _maxLevel
levelToColor = map[Level]*color.Color{
DebugLevel: color.New(color.FgMagenta),
InfoLevel: color.New(color.FgBlue),
WarnLevel: color.New(color.FgYellow),
ErrorLevel: color.New(color.FgRed),
DPanicLevel: color.New(color.FgRed),
PanicLevel: color.New(color.FgRed),
FatalLevel: color.New(color.FgRed),
levelToColorCode = map[Level]colorCode{
DebugLevel: fgMagenta,
InfoLevel: fgBlue,
WarnLevel: fgYellow,
ErrorLevel: fgRed,
DPanicLevel: fgRed,
PanicLevel: fgRed,
FatalLevel: fgRed,
}
)

Expand All @@ -58,7 +67,7 @@ func LowercaseLevelEncoder(l Level, enc PrimitiveArrayEncoder) {
// ColoredLowercaseLevelEncoder serializes a Level to a lowercase string and adds coloring.
// For example, InfoLevel is serialized to "info".
func ColoredLowercaseLevelEncoder(l Level, enc PrimitiveArrayEncoder) {
enc.AppendString(levelToColor[l].Sprint(l.String()))
encodeWithColor(l, enc, l.String())
}

// CapitalLevelEncoder serializes a Level to an all-caps string. For example,
Expand All @@ -70,7 +79,16 @@ func CapitalLevelEncoder(l Level, enc PrimitiveArrayEncoder) {
// ColoredCapitalLevelEncoder serializes a Level to an all-caps string and adds color.
// For example, InfoLevel is serialized to "INFO".
func ColoredCapitalLevelEncoder(l Level, enc PrimitiveArrayEncoder) {
enc.AppendString(levelToColor[l].Sprint(strings.ToUpper(l.String())))
encodeWithColor(l, enc, strings.ToUpper(l.String()))
}

func encodeWithColor(l Level, enc PrimitiveArrayEncoder, s string) {
colorCode, ok := levelToColorCode[l]
if !ok {
enc.AppendString(s)
return
}
enc.AppendString(fmt.Sprintf("\x1b[%dm%s\x1b[0m", colorCode, s))
}

// UnmarshalText unmarshals text to a LevelEncoder. "capital" is unmarshaled to
Expand All @@ -81,7 +99,7 @@ func (e *LevelEncoder) UnmarshalText(text []byte) error {
switch string(text) {
case "capital":
*e = CapitalLevelEncoder
case "colored_capital":
case "coloredCapital":
*e = ColoredCapitalLevelEncoder
case "colored":
*e = ColoredLowercaseLevelEncoder
Expand Down

0 comments on commit ea8f67c

Please sign in to comment.