From 8be30bd382890156e7e813beec9f18b2c65522a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=94=B0=E6=AC=A7?= Date: Tue, 22 Aug 2017 13:55:32 +0800 Subject: [PATCH] Update readme for showing output log to file (#1073) * Update readme for showing output log to file * update indent --- README.md | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index dc3c866..776ae8b 100644 --- a/README.md +++ b/README.md @@ -381,7 +381,8 @@ func main() { r := gin.New() // Global middleware - // Logger middleware will write the logs to gin.DefaultWriter even you set with GIN_MODE=release. By default gin.DefaultWriter = os.Stdout + // Logger middleware will write the logs to gin.DefaultWriter even you set with GIN_MODE=release. + // By default gin.DefaultWriter = os.Stdout r.Use(gin.Logger()) // Recovery middleware recovers from any panics and writes a 500 if there was one. @@ -412,6 +413,27 @@ func main() { } ``` +### Output log to file +```go +func main() { + // Disable Console Color, because not need color when output log to file + gin.DisableConsoleColor() + // Create one file to save logs + f, _ := os.Create("gin.log") + // Reset gin.DefaultWriter + gin.DefaultWriter = io.MultiWriter(f) + // If need to output log to file and console at a time, please use the following code: + // gin.DefaultWriter = io.MultiWriter(f, os.Stdout) + + router := gin.Default() + router.GET("/ping", func(c *gin.Context) { + c.String(200, "pong") + }) + + r.Run(":8080") +} +``` + ### Model binding and validation To bind a request body into a type, use model binding. We currently support binding of JSON, XML and standard form values (foo=bar&boo=baz).