logger_test: color (#1926)

* logger color: string literals

* logger_test: color
This commit is contained in:
Shuo 2019-08-29 08:32:22 +08:00 committed by thinkerou
parent 9a820cf005
commit a22377b09b
2 changed files with 23 additions and 22 deletions

View File

@ -22,18 +22,19 @@ const (
forceColor forceColor
) )
var ( const (
green = string([]byte{27, 91, 57, 55, 59, 52, 50, 109}) green = "\033[97;42m"
white = string([]byte{27, 91, 57, 48, 59, 52, 55, 109}) white = "\033[90;47m"
yellow = string([]byte{27, 91, 57, 48, 59, 52, 51, 109}) yellow = "\033[90;43m"
red = string([]byte{27, 91, 57, 55, 59, 52, 49, 109}) red = "\033[97;41m"
blue = string([]byte{27, 91, 57, 55, 59, 52, 52, 109}) blue = "\033[97;44m"
magenta = string([]byte{27, 91, 57, 55, 59, 52, 53, 109}) magenta = "\033[97;45m"
cyan = string([]byte{27, 91, 57, 55, 59, 52, 54, 109}) cyan = "\033[97;46m"
reset = string([]byte{27, 91, 48, 109}) reset = "\033[0m"
consoleColorMode = autoColor
) )
var consoleColorMode = autoColor
// LoggerConfig defines the config for Logger middleware. // LoggerConfig defines the config for Logger middleware.
type LoggerConfig struct { type LoggerConfig struct {
// Optional. Default value is gin.defaultLogFormatter // Optional. Default value is gin.defaultLogFormatter

View File

@ -291,14 +291,14 @@ func TestColorForMethod(t *testing.T) {
return p.MethodColor() return p.MethodColor()
} }
assert.Equal(t, string([]byte{27, 91, 57, 55, 59, 52, 52, 109}), colorForMethod("GET"), "get should be blue") assert.Equal(t, blue, colorForMethod("GET"), "get should be blue")
assert.Equal(t, string([]byte{27, 91, 57, 55, 59, 52, 54, 109}), colorForMethod("POST"), "post should be cyan") assert.Equal(t, cyan, colorForMethod("POST"), "post should be cyan")
assert.Equal(t, string([]byte{27, 91, 57, 48, 59, 52, 51, 109}), colorForMethod("PUT"), "put should be yellow") assert.Equal(t, yellow, colorForMethod("PUT"), "put should be yellow")
assert.Equal(t, string([]byte{27, 91, 57, 55, 59, 52, 49, 109}), colorForMethod("DELETE"), "delete should be red") assert.Equal(t, red, colorForMethod("DELETE"), "delete should be red")
assert.Equal(t, string([]byte{27, 91, 57, 55, 59, 52, 50, 109}), colorForMethod("PATCH"), "patch should be green") assert.Equal(t, green, colorForMethod("PATCH"), "patch should be green")
assert.Equal(t, string([]byte{27, 91, 57, 55, 59, 52, 53, 109}), colorForMethod("HEAD"), "head should be magenta") assert.Equal(t, magenta, colorForMethod("HEAD"), "head should be magenta")
assert.Equal(t, string([]byte{27, 91, 57, 48, 59, 52, 55, 109}), colorForMethod("OPTIONS"), "options should be white") assert.Equal(t, white, colorForMethod("OPTIONS"), "options should be white")
assert.Equal(t, string([]byte{27, 91, 48, 109}), colorForMethod("TRACE"), "trace is not defined and should be the reset color") assert.Equal(t, reset, colorForMethod("TRACE"), "trace is not defined and should be the reset color")
} }
func TestColorForStatus(t *testing.T) { func TestColorForStatus(t *testing.T) {
@ -309,10 +309,10 @@ func TestColorForStatus(t *testing.T) {
return p.StatusCodeColor() return p.StatusCodeColor()
} }
assert.Equal(t, string([]byte{27, 91, 57, 55, 59, 52, 50, 109}), colorForStatus(http.StatusOK), "2xx should be green") assert.Equal(t, green, colorForStatus(http.StatusOK), "2xx should be green")
assert.Equal(t, string([]byte{27, 91, 57, 48, 59, 52, 55, 109}), colorForStatus(http.StatusMovedPermanently), "3xx should be white") assert.Equal(t, white, colorForStatus(http.StatusMovedPermanently), "3xx should be white")
assert.Equal(t, string([]byte{27, 91, 57, 48, 59, 52, 51, 109}), colorForStatus(http.StatusNotFound), "4xx should be yellow") assert.Equal(t, yellow, colorForStatus(http.StatusNotFound), "4xx should be yellow")
assert.Equal(t, string([]byte{27, 91, 57, 55, 59, 52, 49, 109}), colorForStatus(2), "other things should be red") assert.Equal(t, red, colorForStatus(2), "other things should be red")
} }
func TestResetColor(t *testing.T) { func TestResetColor(t *testing.T) {