mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-22 01:12:16 +08:00
Merge branch 'master' into autochthe-patch-1
This commit is contained in:
commit
e0b66aaecb
5
Makefile
5
Makefile
@ -21,7 +21,10 @@ test:
|
|||||||
exit 1; \
|
exit 1; \
|
||||||
elif grep -q "build failed" tmp.out; then \
|
elif grep -q "build failed" tmp.out; then \
|
||||||
rm tmp.out; \
|
rm tmp.out; \
|
||||||
exit; \
|
exit 1; \
|
||||||
|
elif grep -q "setup failed" tmp.out; then \
|
||||||
|
rm tmp.out; \
|
||||||
|
exit 1; \
|
||||||
fi; \
|
fi; \
|
||||||
if [ -f profile.out ]; then \
|
if [ -f profile.out ]; then \
|
||||||
cat profile.out | grep -v "mode:" >> coverage.out; \
|
cat profile.out | grep -v "mode:" >> coverage.out; \
|
||||||
|
45
README.md
45
README.md
@ -215,9 +215,6 @@ $ go build -tags=jsoniter .
|
|||||||
|
|
||||||
```go
|
```go
|
||||||
func main() {
|
func main() {
|
||||||
// Disable Console Color
|
|
||||||
// gin.DisableConsoleColor()
|
|
||||||
|
|
||||||
// Creates a gin router with default middleware:
|
// Creates a gin router with default middleware:
|
||||||
// logger and recovery (crash-free) middleware
|
// logger and recovery (crash-free) middleware
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
@ -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" "
|
::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
|
### 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).
|
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).
|
||||||
|
10
logger.go
10
logger.go
@ -24,6 +24,7 @@ var (
|
|||||||
cyan = string([]byte{27, 91, 57, 55, 59, 52, 54, 109})
|
cyan = string([]byte{27, 91, 57, 55, 59, 52, 54, 109})
|
||||||
reset = string([]byte{27, 91, 48, 109})
|
reset = string([]byte{27, 91, 48, 109})
|
||||||
disableColor = false
|
disableColor = false
|
||||||
|
forceColor = false
|
||||||
)
|
)
|
||||||
|
|
||||||
// LoggerConfig defines the config for Logger middleware.
|
// LoggerConfig defines the config for Logger middleware.
|
||||||
@ -90,6 +91,11 @@ func DisableConsoleColor() {
|
|||||||
disableColor = true
|
disableColor = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ForceConsoleColor force color output in the console.
|
||||||
|
func ForceConsoleColor() {
|
||||||
|
forceColor = true
|
||||||
|
}
|
||||||
|
|
||||||
// ErrorLogger returns a handlerfunc for any error type.
|
// ErrorLogger returns a handlerfunc for any error type.
|
||||||
func ErrorLogger() HandlerFunc {
|
func ErrorLogger() HandlerFunc {
|
||||||
return ErrorLoggerT(ErrorTypeAny)
|
return ErrorLoggerT(ErrorTypeAny)
|
||||||
@ -144,9 +150,9 @@ func LoggerWithConfig(conf LoggerConfig) HandlerFunc {
|
|||||||
|
|
||||||
isTerm := true
|
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()))) ||
|
(os.Getenv("TERM") == "dumb" || (!isatty.IsTerminal(w.Fd()) && !isatty.IsCygwinTerminal(w.Fd()))) ||
|
||||||
disableColor {
|
disableColor) && !forceColor {
|
||||||
isTerm = false
|
isTerm = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -340,3 +340,10 @@ func TestDisableConsoleColor(t *testing.T) {
|
|||||||
DisableConsoleColor()
|
DisableConsoleColor()
|
||||||
assert.True(t, disableColor)
|
assert.True(t, disableColor)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestForceConsoleColor(t *testing.T) {
|
||||||
|
New()
|
||||||
|
assert.False(t, forceColor)
|
||||||
|
ForceConsoleColor()
|
||||||
|
assert.True(t, forceColor)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user