go:gin输出日志文件方式
作者:大鹏blog
这篇文章主要介绍了go:gin输出日志文件方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
背景
我们运行服务端程序,日志直接会打印,本文介绍如何将服务端日志输出到日志文件
代码
package main import ( "io" "os" "github.com/gin-gonic/gin" ) func main() { // Logging to a file. f, _ := os.Create("gin.log") gin.DefaultWriter = io.MultiWriter(f) // Use the following code if you need to write the logs to file and console at the same time. // gin.DefaultWriter = io.MultiWriter(f, os.Stdout) router := gin.Default() router.GET("/ping", func(c *gin.Context) { c.String(200, "pong") }) router.Run(":8080") }
运行程序之后服务端没有任何信息输出
再看当前路径下。
创建出来一个gin.log的文件
查看日志文件内容
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)[GIN-debug] GET /ping --> main.main.func1 (3 handlers)
[GIN-debug] Listening and serving HTTP on :8080
[GIN] 2019/12/06 - 09:49:58 |[97;42m 200 [0m| 47.188µs | ::1 |[97;44m GET [0m /ping
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。