Golang GinWeb之自定义日志格式和输出方式/启禁日志颜色的方法是什么

发布时间:2021-10-23 16:24:30 作者:iii
来源:亿速云 阅读:505

这篇文章主要介绍“Golang GinWeb之自定义日志格式和输出方式/启禁日志颜色的方法是什么”,在日常操作中,相信很多人在Golang GinWeb之自定义日志格式和输出方式/启禁日志颜色的方法是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Golang GinWeb之自定义日志格式和输出方式/启禁日志颜色的方法是什么”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

记录日志到文件

利用io.MultiWriter多写出器可以实现日志记录到文件的同时也输出到控制台

package main  import (   "github.com/gin-gonic/gin"   "io"   "os" )  func main() {   // Disable Console Color, you don't need console color when writing the logs to file.   // 禁用控制台日志颜色,日志写到文件的时候,不需要打开控制台日志颜色   gin.DisableConsoleColor()   // Logging to a file.  新建日志文件,得到文件结构,文件结构实现了写出器Writer接口   f, _ := os.Create("gin.log")   //io.MultiWriter(多写出器方法)创建一个写出器, 将传入的多个写出器追加为一个写出器数组, 得到的写出器实现了Writer接口, 它会将需要写出的数据写出到每个写出器, 就像Unix命令tee,会将数据写入文件的同时打印到标准输出   //配置Gin默认日志写出器为得到的多写出器   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的LoggerWithFormatter方法实例化一个日志器Logger中间件,并带有指定的日志格式

package main  import (   "fmt"   "github.com/gin-gonic/gin"   "time" )  func main() {   router := gin.New()    // LoggerWithFormatter middleware will write the logs to gin.DefaultWriter   // By default gin.DefaultWriter = os.Stdout   // type LogFormatter func(params LogFormatterParams) string 这里的LogFormatterParams是一个格式化日志参数的结构体   router.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {     // your custom format     // 127.0.0.1 - [Sun, 22 Nov 2020 17:09:53 CST] "GET /ping HTTP/1.1 200 56.113µs "curl/7.64.1" "     return fmt.Sprintf("%s - [%s] \"%s %s %s %d %s \"%s\" %s\"\n",       param.ClientIP,                       //请求客户端的IP地址       param.TimeStamp.Format(time.RFC1123), //请求时间       param.Method,                         //请求方法       param.Path,                           //路由路径       param.Request.Proto,                  //请求协议       param.StatusCode,                     //http响应码       param.Latency,                        //请求到响应的延时       param.Request.UserAgent(),            //客户端代理程序       param.ErrorMessage,                   //如果有错误,也打印错误信息     )   }))   router.Use(gin.Recovery())    router.GET("/ping", func(c *gin.Context) {     c.String(200, "pong")   })    router.Run(":8080") } //模拟请求测试: curl http://localhost:8080/ping

打开/禁用日志颜色

package main  import (   "github.com/gin-gonic/gin" )  func main() {   // 默认输出到控制台的日志颜色是根据您使用的虚拟终端TTY来着色的   // Disable log's color 禁用日志颜色   gin.DisableConsoleColor()    // 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") }  //模拟请求测试: curl http://localhost:8080/ping

到此,关于“Golang GinWeb之自定义日志格式和输出方式/启禁日志颜色的方法是什么”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. golang会不会取代php
  2. Golang中怎么实现一个HTTP代理服务器

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

golang ginweb

上一篇:Golang GinWeb框架之文件上传/程序panic崩溃后自定义处理方式是什么

下一篇:如何从JVM heap dump里查找没有关闭文件的引用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》