当前位置:主页 > 查看内容

Golang GinWeb框架3-自定义日志格式和输出方式/启禁日志颜色

发布时间:2021-09-30 00:00| 位朋友查看

简介:简介 本文接着上文(Golang GinWeb框架2-文件上传/程序panic崩溃后自定义处理方式)继续探索GinWeb框架 记录日志到文件 利用io.MultiWriter多写出器可以实现日志记录到文件的同时也输出到控制台 packagemain import( github.com/gin-gonic/gin io os ) funcmai……

 简介

本文接着上文(Golang GinWeb框架2-文件上传/程序panic崩溃后自定义处理方式)继续探索GinWeb框架


记录日志到文件

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

  1. package main 
  2.  
  3. import ( 
  4.   "github.com/gin-gonic/gin" 
  5.   "io" 
  6.   "os" 
  7.  
  8. func main() { 
  9.   // Disable Console Color, you don't need console color when writing the logs to file. 
  10.   // 禁用控制台日志颜色,日志写到文件的时候,不需要打开控制台日志颜色 
  11.   gin.DisableConsoleColor() 
  12.   // Logging to a file.  新建日志文件,得到文件结构,文件结构实现了写出器Writer接口 
  13.   f, _ := os.Create("gin.log"
  14.   //io.MultiWriter(多写出器方法)创建一个写出器, 将传入的多个写出器追加为一个写出器数组, 得到的写出器实现了Writer接口, 它会将需要写出的数据写出到每个写出器, 就像Unix命令tee,会将数据写入文件的同时打印到标准输出 
  15.   //配置Gin默认日志写出器为得到的多写出器 
  16.   gin.DefaultWriter = io.MultiWriter(f) 
  17.   // Use the following code if you need to write the logs to file and console at the same time
  18.   // 使用下面的代码,将日志写入文件的同时,也输出到控制台 
  19.   // gin.DefaultWriter = io.MultiWriter(f, os.Stdout) 
  20.  
  21.   router := gin.Default() 
  22.   router.GET("/ping", func(c *gin.Context) { 
  23.     c.String(200, "pong"
  24.   }) 
  25.  
  26.   router.Run(":8080"

自定义日志格式

利用Gin的LoggerWithFormatter方法实例化一个日志器Logger中间件,并带有指定的日志格式

  1. package main 
  2.  
  3. import ( 
  4.   "fmt" 
  5.   "github.com/gin-gonic/gin" 
  6.   "time" 
  7.  
  8. func main() { 
  9.   router := gin.New() 
  10.  
  11.   // LoggerWithFormatter middleware will write the logs to gin.DefaultWriter 
  12.   // By default gin.DefaultWriter = os.Stdout 
  13.   // type LogFormatter func(params LogFormatterParams) string 这里的LogFormatterParams是一个格式化日志参数的结构体 
  14.   router.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string { 
  15.     // your custom format 
  16.     // 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" " 
  17.     return fmt.Sprintf("%s - [%s] \"%s %s %s %d %s \"%s\" %s\"\n"
  18.       param.ClientIP,                       //请求客户端的IP地址 
  19.       param.TimeStamp.Format(time.RFC1123), //请求时间 
  20.       param.Method,                         //请求方法 
  21.       param.Path,                           //路由路径 
  22.       param.Request.Proto,                  //请求协议 
  23.       param.StatusCode,                     //http响应码 
  24.       param.Latency,                        //请求到响应的延时 
  25.       param.Request.UserAgent(),            //客户端代理程序 
  26.       param.ErrorMessage,                   //如果有错误,也打印错误信息 
  27.     ) 
  28.   })) 
  29.   router.Use(gin.Recovery()) 
  30.  
  31.   router.GET("/ping", func(c *gin.Context) { 
  32.     c.String(200, "pong"
  33.   }) 
  34.  
  35.   router.Run(":8080"
  36. //模拟请求测试: curl http://localhost:8080/ping 

打开/禁用日志颜色

  • gin.DisableConsoleColor() 禁用日志颜色
  • gin.ForceConsoleColor() 强制开启日志颜色, 采用虚拟终端TTY颜色方案
  1. package main 
  2.  
  3. import ( 
  4.   "github.com/gin-gonic/gin" 
  5.  
  6. func main() { 
  7.   // 默认输出到控制台的日志颜色是根据您使用的虚拟终端TTY来着色的 
  8.   // Disable log's color 禁用日志颜色 
  9.   gin.DisableConsoleColor() 
  10.  
  11.   // Force log's color 强制开启日志颜色 
  12.   //gin.ForceConsoleColor() 
  13.  
  14.   // Creates a gin router with default middleware: 
  15.   // logger and recovery (crash-free) middleware 
  16.   router := gin.Default() 
  17.  
  18.   router.GET("/ping", func(c *gin.Context) { 
  19.     c.String(200, "pong"
  20.   }) 
  21.  
  22.   router.Run(":8080"
  23.  
  24. //模拟请求测试: curl http://localhost:8080/ping 

参考文档

Gin官方仓库:https://github.com/gin-gonic/gin


本文转载自网络,原文链接:https://mp.weixin.qq.com/s/ITd_asZeczmNlY05dosA-w
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!
上一篇:层概述 - 函数计算 下一篇:没有了

推荐图文


随机推荐