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

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

 简介

本文接着上文(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

 

 

责任编辑:姜华 来源: 云原生云
相关推荐

2020-11-26 10:08:17

Golang GinW

2023-07-28 09:26:43

GolangZap

2009-07-07 14:32:47

JDK日志Formatter

2009-07-07 14:00:25

JDK日志Handler

2020-11-25 09:18:15

Golang GinW

2020-12-10 10:22:48

GinWeb中间件HTTPS

2020-12-08 12:05:48

Golang GinW框架HTTPS

2020-11-23 10:48:39

Golang GinW

2009-08-05 18:01:20

C#自定义异常处理

2021-05-28 08:58:41

Golang网卡metrics

2020-12-03 09:28:05

Golang GinW

2011-04-11 13:14:58

AjaxWEB服务

2023-10-31 09:10:39

2020-12-02 11:18:28

Golang GinW

2009-06-25 14:53:35

自定义UI组件JSF框架

2018-07-12 16:22:45

Linux命令行文本颜色

2022-09-20 07:01:50

对象初始化代码

2021-01-14 19:04:36

框架数据库mybatis

2009-11-05 10:38:05

Visual Stud

2010-02-25 11:23:29

WCF返回自定义格式
点赞
收藏

51CTO技术栈公众号