Golang GinWeb框架3-自定義日志格式和輸出方式/啟禁日志顏色
作者:曉兵XB 
  本文接著上文(Golang GinWeb框架2-文件上傳/程序panic崩潰后自定義處理方式)繼續(xù)探索GinWeb框架
 簡介
本文接著上文(Golang GinWeb框架2-文件上傳/程序panic崩潰后自定義處理方式)繼續(xù)探索GinWeb框架

記錄日志到文件
利用io.MultiWriter多寫出器可以實(shí)現(xiàn)日志記錄到文件的同時(shí)也輸出到控制臺(tái)
- 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.
 - // 禁用控制臺(tái)日志顏色,日志寫到文件的時(shí)候,不需要打開控制臺(tái)日志顏色
 - gin.DisableConsoleColor()
 - // Logging to a file. 新建日志文件,得到文件結(jié)構(gòu),文件結(jié)構(gòu)實(shí)現(xiàn)了寫出器Writer接口
 - f, _ := os.Create("gin.log")
 - //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)輸出
 - //配置Gin默認(rèn)日志寫出器為得到的多寫出器
 - gin.DefaultWriter = io.MultiWriter(f)
 - // Use the following code if you need to write the logs to file and console at the same time.
 - // 使用下面的代碼,將日志寫入文件的同時(shí),也輸出到控制臺(tái)
 - // 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方法實(shí)例化一個(gè)日志器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是一個(gè)格式化日志參數(shù)的結(jié)構(gòu)體
 - 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, //請(qǐng)求客戶端的IP地址
 - param.TimeStamp.Format(time.RFC1123), //請(qǐng)求時(shí)間
 - param.Method, //請(qǐng)求方法
 - param.Path, //路由路徑
 - param.Request.Proto, //請(qǐng)求協(xié)議
 - param.StatusCode, //http響應(yīng)碼
 - param.Latency, //請(qǐng)求到響應(yīng)的延時(shí)
 - param.Request.UserAgent(), //客戶端代理程序
 - param.ErrorMessage, //如果有錯(cuò)誤,也打印錯(cuò)誤信息
 - )
 - }))
 - router.Use(gin.Recovery())
 - router.GET("/ping", func(c *gin.Context) {
 - c.String(200, "pong")
 - })
 - router.Run(":8080")
 - }
 - //模擬請(qǐng)求測試: curl http://localhost:8080/ping
 
打開/禁用日志顏色
- gin.DisableConsoleColor() 禁用日志顏色
 - gin.ForceConsoleColor() 強(qiáng)制開啟日志顏色, 采用虛擬終端TTY顏色方案
 
- package main
 - import (
 - "github.com/gin-gonic/gin"
 - )
 - func main() {
 - // 默認(rèn)輸出到控制臺(tái)的日志顏色是根據(jù)您使用的虛擬終端TTY來著色的
 - // Disable log's color 禁用日志顏色
 - gin.DisableConsoleColor()
 - // Force log's color 強(qiáng)制開啟日志顏色
 - //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")
 - }
 - //模擬請(qǐng)求測試: curl http://localhost:8080/ping
 
參考文檔
Gin官方倉庫:https://github.com/gin-gonic/gin
責(zé)任編輯:姜華 
                    來源:
                    云原生云
 















 
 
 







 
 
 
 