自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Golang GinWeb框架3-自定義日志格式和輸出方式/啟禁日志顏色

開發(fā) 前端
本文接著上文(Golang GinWeb框架2-文件上傳/程序panic崩潰后自定義處理方式)繼續(xù)探索GinWeb框架

 簡介

本文接著上文(Golang GinWeb框架2-文件上傳/程序panic崩潰后自定義處理方式)繼續(xù)探索GinWeb框架


記錄日志到文件

利用io.MultiWriter多寫出器可以實(shí)現(xiàn)日志記錄到文件的同時(shí)也輸出到控制臺(tái)

  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.   // 禁用控制臺(tái)日志顏色,日志寫到文件的時(shí)候,不需要打開控制臺(tái)日志顏色 
  11.   gin.DisableConsoleColor() 
  12.   // Logging to a file.  新建日志文件,得到文件結(jié)構(gòu),文件結(jié)構(gòu)實(shí)現(xiàn)了寫出器Writer接口 
  13.   f, _ := os.Create("gin.log"
  14.   //io.MultiWriter(多寫出器方法)創(chuàng)建一個(gè)寫出器, 將傳入的多個(gè)寫出器追加為一個(gè)寫出器數(shù)組, 得到的寫出器實(shí)現(xiàn)了Writer接口, 它會(huì)將需要寫出的數(shù)據(jù)寫出到每個(gè)寫出器, 就像Unix命令tee,會(huì)將數(shù)據(jù)寫入文件的同時(shí)打印到標(biāo)準(zhǔn)輸出 
  15.   //配置Gin默認(rèn)日志寫出器為得到的多寫出器 
  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.   // 使用下面的代碼,將日志寫入文件的同時(shí),也輸出到控制臺(tái) 
  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方法實(shí)例化一個(gè)日志器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是一個(gè)格式化日志參數(shù)的結(jié)構(gòu)體 
  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,                       //請(qǐng)求客戶端的IP地址 
  19.       param.TimeStamp.Format(time.RFC1123), //請(qǐng)求時(shí)間 
  20.       param.Method,                         //請(qǐng)求方法 
  21.       param.Path,                           //路由路徑 
  22.       param.Request.Proto,                  //請(qǐng)求協(xié)議 
  23.       param.StatusCode,                     //http響應(yīng)碼 
  24.       param.Latency,                        //請(qǐng)求到響應(yīng)的延時(shí) 
  25.       param.Request.UserAgent(),            //客戶端代理程序 
  26.       param.ErrorMessage,                   //如果有錯(cuò)誤,也打印錯(cuò)誤信息 
  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. //模擬請(qǐng)求測試: curl http://localhost:8080/ping 

打開/禁用日志顏色

  • gin.DisableConsoleColor() 禁用日志顏色
  • gin.ForceConsoleColor() 強(qiáng)制開啟日志顏色, 采用虛擬終端TTY顏色方案
  1. package main 
  2.  
  3. import ( 
  4.   "github.com/gin-gonic/gin" 
  5.  
  6. func main() { 
  7.   // 默認(rèn)輸出到控制臺(tái)的日志顏色是根據(jù)您使用的虛擬終端TTY來著色的 
  8.   // Disable log's color 禁用日志顏色 
  9.   gin.DisableConsoleColor() 
  10.  
  11.   // Force log's color 強(qiáng)制開啟日志顏色 
  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. //模擬請(qǐng)求測試: curl http://localhost:8080/ping 

參考文檔

Gin官方倉庫:https://github.com/gin-gonic/gin

 

 

責(zé)任編輯:姜華 來源: 云原生云
相關(guān)推薦

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#自定義異常處理

2020-12-03 09:28:05

Golang GinW

2021-05-28 08:58:41

Golang網(wǎng)卡metrics

2011-04-11 13:14:58

AjaxWEB服務(wù)

2020-12-02 11:18:28

Golang GinW

2023-10-31 09:10:39

2025-01-06 10:38:04

2009-06-25 14:53:35

自定義UI組件JSF框架

2009-11-05 10:38:05

Visual Stud

2022-09-20 07:01:50

對(duì)象初始化代碼

2021-01-14 19:04:36

框架數(shù)據(jù)庫mybatis

2024-11-18 09:18:21

Gin框架驗(yàn)證器
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)