Skip to content

Commit a58a2f9

Browse files
orobardetappleboy
authored andcommitted
Add a function to force color in console output (gin-gonic#1724)
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 (gin-gonic#1590) and as a workaround for gin-gonic#1547.
1 parent 90587c7 commit a58a2f9

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

README.md

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,6 @@ $ go build -tags=jsoniter .
215215

216216
```go
217217
func main() {
218-
// Disable Console Color
219-
// gin.DisableConsoleColor()
220-
221218
// Creates a gin router with default middleware:
222219
// logger and recovery (crash-free) middleware
223220
router := gin.Default()
@@ -570,6 +567,48 @@ func main() {
570567
::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" "
571568
```
572569

570+
### Controlling Log output coloring
571+
572+
By default, logs output on console should be colorized depending on the detected TTY.
573+
574+
Never colorize logs:
575+
576+
```go
577+
func main() {
578+
// Disable log's color
579+
gin.DisableConsoleColor()
580+
581+
// Creates a gin router with default middleware:
582+
// logger and recovery (crash-free) middleware
583+
router := gin.Default()
584+
585+
router.GET("/ping", func(c *gin.Context) {
586+
c.String(200, "pong")
587+
})
588+
589+
router.Run(":8080")
590+
}
591+
```
592+
593+
Always colorize logs:
594+
595+
```go
596+
func main() {
597+
// Force log's color
598+
gin.ForceConsoleColor()
599+
600+
// Creates a gin router with default middleware:
601+
// logger and recovery (crash-free) middleware
602+
router := gin.Default()
603+
604+
router.GET("/ping", func(c *gin.Context) {
605+
c.String(200, "pong")
606+
})
607+
608+
router.Run(":8080")
609+
}
610+
```
611+
573612
### Model binding and validation
574613

575614
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).

logger.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var (
2424
cyan = string([]byte{27, 91, 57, 55, 59, 52, 54, 109})
2525
reset = string([]byte{27, 91, 48, 109})
2626
disableColor = false
27+
forceColor = false
2728
)
2829

2930
// LoggerConfig defines the config for Logger middleware.
@@ -90,6 +91,11 @@ func DisableConsoleColor() {
9091
disableColor = true
9192
}
9293

94+
// ForceConsoleColor force color output in the console.
95+
func ForceConsoleColor() {
96+
forceColor = true
97+
}
98+
9399
// ErrorLogger returns a handlerfunc for any error type.
94100
func ErrorLogger() HandlerFunc {
95101
return ErrorLoggerT(ErrorTypeAny)
@@ -144,9 +150,9 @@ func LoggerWithConfig(conf LoggerConfig) HandlerFunc {
144150

145151
isTerm := true
146152

147-
if w, ok := out.(*os.File); !ok ||
153+
if w, ok := out.(*os.File); (!ok ||
148154
(os.Getenv("TERM") == "dumb" || (!isatty.IsTerminal(w.Fd()) && !isatty.IsCygwinTerminal(w.Fd()))) ||
149-
disableColor {
155+
disableColor) && !forceColor {
150156
isTerm = false
151157
}
152158

logger_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,10 @@ func TestDisableConsoleColor(t *testing.T) {
340340
DisableConsoleColor()
341341
assert.True(t, disableColor)
342342
}
343+
344+
func TestForceConsoleColor(t *testing.T) {
345+
New()
346+
assert.False(t, forceColor)
347+
ForceConsoleColor()
348+
assert.True(t, forceColor)
349+
}

0 commit comments

Comments
 (0)