当前位置:网站首页>go : gin 自定义日志输出格式
go : gin 自定义日志输出格式
2022-07-25 12:27:00 【IT工作者】
本文实验gin 框架 自定义日志格式
代码:
package main
import (
"fmt"
"time"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.New()
// LoggerWithFormatter middleware will write the logs to gin.DefaultWrite
// By default gin.DefaultWriter = os.Stdout
router.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
// your custom format
return fmt.Sprintf("%s - [%s] \"%s %s %s %d %s \"%s\" %s\"\n",
param.ClientIP,
param.TimeStamp.Format(time.RFC1123),
param.Method,
param.Path,
param.Request.Proto,
param.StatusCode,
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")
}运行结果:
[GIN-debug] GET /ping --> main.main.func2 (3 handlers)
[GIN-debug] Listening and serving HTTP on :8080
::1 - [Thu, 05 July 2022 19:28:05 CST] "GET / HTTP/1.1 404 5.113µs "curl/7.54.0" "
::1 - [Thu, 05 July 2022 19:28:05 CST] "GET / HTTP/1.1 404 1.029µs "curl/7.54.0" "
::1 - [Thu, 05 July 2022 19:28:06 CST] "GET / HTTP/1.1 404 1.19µs "curl/7.54.0" "
::1 - [Thu, 05 July 2022 19:28:10 CST] "GET / HTTP/1.1 404 1.565µs "curl/7.54.0" "
::1 - [Thu, 05 July 2022 19:29:08 CST] "GET /ping HTTP/1.1 200 157.996µs "curl/7.54.0" "
::1 - [Thu, 05 July 2022 19:29:09 CST] "GET /ping HTTP/1.1 200 68.985µs "curl/7.54.0" "
边栏推荐
- 业务可视化-让你的流程图'Run'起来(3.分支选择&跨语言分布式运行节点)
- 公安部:国际社会普遍认为中国是世界上最安全的国家之一
- LeetCode 1184. 公交站间的距离
- 深度学习MEMC插帧论文列表paper list
- 2022.07.24 (lc_6126_design food scoring system)
- Spirng @Conditional 条件注解的使用
- SSTI template injection vulnerability summary [bjdctf2020]cookie is so stable
- What is ci/cd?
- clickhouse笔记03-- Grafana 接入ClickHouse
- 2022.07.24 (lc_6125_equal row and column pairs)
猜你喜欢

Kyligence was selected into Gartner 2022 data management technology maturity curve report

Moving Chinese figure liushenglan

什么是CI/CD?

Build a series of vision transformer practices, and finally meet, Timm library!

PyTorch进阶训练技巧

Azure Devops(十四) 使用Azure的私有Nuget仓库
![[ROS advanced chapter] Lecture 9 programming optimization of URDF and use of xacro](/img/a2/9b676d0f1b33cc7d413cee6c52d76d.png)
[ROS advanced chapter] Lecture 9 programming optimization of URDF and use of xacro

Interviewer: "classmate, have you ever done a real landing project?"

【AI4Code】《Contrastive Code Representation Learning》 (EMNLP 2021)

感动中国人物刘盛兰
随机推荐
Detailed explanation of switch link aggregation [Huawei ENSP]
PyTorch主要模块
艰辛的旅程
2022.07.24 (lc_6125_equal row and column pairs)
想要白嫖正则大全是吧?这一次给你个够!
Maskgae: masked graph modeling meets graph autoencoders
Selenium uses -- XPath and analog input and analog click collaboration
PyTorch进阶训练技巧
Build a series of vision transformer practices, and finally meet, Timm library!
状态(State)模式
2022.07.24 (lc_6124_the first letter that appears twice)
我在源头SQLServer里面登记绝对删除的数据,传到MaxComputer,在数据清洗的时候写绝对
Does MySQL have flush privileges
Introduction to the scratch crawler framework
"Autobiography of Franklin" cultivation
Kyligence 入选 Gartner 2022 数据管理技术成熟度曲线报告
[shutter -- layout] stacked layout (stack and positioned)
【问题解决】org.apache.ibatis.exceptions.PersistenceException: Error building SqlSession.1 字节的 UTF-8 序列的字
Pytorch advanced training skills
I register the absolutely deleted data in the source sqlserver, send it to maxcomputer, and write the absolute data when cleaning the data