Skip to content

Commit

Permalink
Add a function to force color in console output (#1724)
Browse files Browse the repository at this point in the history
Add a function `ForceConsoleColor`, like `DisableConsoleColor` but to force coloring the output.

It usefull when some IDE's integrated console (like IntelliJ or Goland) are not detected as TTY, but can display colors.

Also helps if one want to output color in log file (#1590) and as a workaround for #1547.
  • Loading branch information
orobardet authored and appleboy committed Feb 20, 2019
1 parent 90587c7 commit a58a2f9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
45 changes: 42 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,6 @@ $ go build -tags=jsoniter .

```go
func main() {
// Disable Console Color
// gin.DisableConsoleColor()

// Creates a gin router with default middleware:
// logger and recovery (crash-free) middleware
router := gin.Default()
Expand Down Expand Up @@ -570,6 +567,48 @@ func main() {
::1 - [Fri, 07 Dec 2018 17:04:38 JST] "GET /ping HTTP/1.1 200 122.767µs "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36" "
```

### Controlling Log output coloring

By default, logs output on console should be colorized depending on the detected TTY.

Never colorize logs:

```go
func main() {
// Disable log's color
gin.DisableConsoleColor()

// Creates a gin router with default middleware:
// logger and recovery (crash-free) middleware
router := gin.Default()

router.GET("/ping", func(c *gin.Context) {
c.String(200, "pong")
})

router.Run(":8080")
}
```

Always colorize logs:

```go
func main() {
// Force log's color
gin.ForceConsoleColor()

// Creates a gin router with default middleware:
// logger and recovery (crash-free) middleware
router := gin.Default()

router.GET("/ping", func(c *gin.Context) {
c.String(200, "pong")
})

router.Run(":8080")
}
```

### Model binding and validation

To bind a request body into a type, use model binding. We currently support binding of JSON, XML, YAML and standard form values (foo=bar&boo=baz).
Expand Down
10 changes: 8 additions & 2 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var (
cyan = string([]byte{27, 91, 57, 55, 59, 52, 54, 109})
reset = string([]byte{27, 91, 48, 109})
disableColor = false
forceColor = false
)

// LoggerConfig defines the config for Logger middleware.
Expand Down Expand Up @@ -90,6 +91,11 @@ func DisableConsoleColor() {
disableColor = true
}

// ForceConsoleColor force color output in the console.
func ForceConsoleColor() {
forceColor = true
}

// ErrorLogger returns a handlerfunc for any error type.
func ErrorLogger() HandlerFunc {
return ErrorLoggerT(ErrorTypeAny)
Expand Down Expand Up @@ -144,9 +150,9 @@ func LoggerWithConfig(conf LoggerConfig) HandlerFunc {

isTerm := true

if w, ok := out.(*os.File); !ok ||
if w, ok := out.(*os.File); (!ok ||
(os.Getenv("TERM") == "dumb" || (!isatty.IsTerminal(w.Fd()) && !isatty.IsCygwinTerminal(w.Fd()))) ||
disableColor {
disableColor) && !forceColor {
isTerm = false
}

Expand Down
7 changes: 7 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,10 @@ func TestDisableConsoleColor(t *testing.T) {
DisableConsoleColor()
assert.True(t, disableColor)
}

func TestForceConsoleColor(t *testing.T) {
New()
assert.False(t, forceColor)
ForceConsoleColor()
assert.True(t, forceColor)
}

0 comments on commit a58a2f9

Please sign in to comment.