forked from mirror/gin
Support disable console color.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
parent
e06cef0672
commit
d158ef2e82
|
@ -108,6 +108,9 @@ BenchmarkZeus_GithubAll | 2000 | 944234 | 300688 | 2648
|
|||
|
||||
```go
|
||||
func main() {
|
||||
// Disable Console Color
|
||||
// gin.DisableConsoleColor()
|
||||
|
||||
// Creates a gin router with default middleware:
|
||||
// logger and recovery (crash-free) middleware
|
||||
router := gin.Default()
|
||||
|
|
|
@ -7,6 +7,8 @@ import (
|
|||
var DB = make(map[string]string)
|
||||
|
||||
func main() {
|
||||
// Disable Console Color
|
||||
// gin.DisableConsoleColor()
|
||||
r := gin.Default()
|
||||
|
||||
// Ping test
|
||||
|
|
|
@ -22,8 +22,13 @@ var (
|
|||
magenta = string([]byte{27, 91, 57, 55, 59, 52, 53, 109})
|
||||
cyan = string([]byte{27, 91, 57, 55, 59, 52, 54, 109})
|
||||
reset = string([]byte{27, 91, 48, 109})
|
||||
disableColor = false
|
||||
)
|
||||
|
||||
func DisableConsoleColor() {
|
||||
disableColor = true
|
||||
}
|
||||
|
||||
func ErrorLogger() HandlerFunc {
|
||||
return ErrorLoggerT(ErrorTypeAny)
|
||||
}
|
||||
|
@ -49,7 +54,7 @@ func Logger() HandlerFunc {
|
|||
func LoggerWithWriter(out io.Writer, notlogged ...string) HandlerFunc {
|
||||
isTerm := true
|
||||
|
||||
if w, ok := out.(*os.File); !ok || !isatty.IsTerminal(w.Fd()) {
|
||||
if w, ok := out.(*os.File); !ok || !isatty.IsTerminal(w.Fd()) || disableColor {
|
||||
isTerm = false
|
||||
}
|
||||
|
||||
|
|
|
@ -132,3 +132,10 @@ func TestSkippingPaths(t *testing.T) {
|
|||
performRequest(router, "GET", "/skipped")
|
||||
assert.Contains(t, buffer.String(), "")
|
||||
}
|
||||
|
||||
func TestDisableConsoleColor(t *testing.T) {
|
||||
New()
|
||||
assert.False(t, disableColor)
|
||||
DisableConsoleColor()
|
||||
assert.True(t, disableColor)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue