当前位置:网站首页>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" "
边栏推荐
- 2022.07.24 (lc_6125_equal row and column pairs)
- Lu MENGZHENG's "Fu of broken kiln"
- Ecological profile of pytorch
- 【7】 Layer display and annotation
- 2022.07.24(LC_6126_设计食物评分系统)
- Microsoft azure and Analysys jointly released the report "Enterprise Cloud native platform driven digital transformation"
- 基于JEECG制作一个通用的级联字典选择控件-DictCascadeUniversal
- Moving Chinese figure liushenglan
- flinkcdc可以一起导mongodb数据库中的多张表吗?
- 吕蒙正《破窑赋》
猜你喜欢

交换机链路聚合详解【华为eNSP】

A method to prevent SYN flooding attacks -- syn cookies

JS 将伪数组转换成数组

919. Complete binary tree inserter: simple BFS application problem

零基础学习CANoe Panel(14)——二极管( LED Control )和液晶屏(LCD Control)

2.1.2 application of machine learning
![[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

Kyligence 入选 Gartner 2022 数据管理技术成熟度曲线报告

【AI4Code】《CodeBERT: A Pre-Trained Model for Programming and Natural Languages》 EMNLP 2020

Experimental reproduction of image classification (reasoning only) based on caffe resnet-50 network
随机推荐
pytorch环境配置及基础知识
【5】 Page and print settings
Perf performance debugging
Jenkins configuration pipeline
WPF project introduction 1 - Design and development of simple login page
Build a series of vision transformer practices, and finally meet, Timm library!
Detailed explanation of flex box
2022.07.24 (lc_6125_equal row and column pairs)
SSTI template injection vulnerability summary [bjdctf2020]cookie is so stable
请问一下,使用数据集成从postgreSQL导数据到Mysql数据库,有部分数据的字段中出现emoj
推荐系统-协同过滤在Spark中的实现
Pytorch environment configuration and basic knowledge
【11】 Production and adjustment of vector and grid data Legends
【4】 Layout view and layout toolbar usage
Script set random user_ agent
Leetcode 1184. distance between bus stops
LeetCode 1184. 公交站间的距离
Kyligence 入选 Gartner 2022 数据管理技术成熟度曲线报告
Azure Devops (XIV) use azure's private nuget warehouse
Azure Devops(十四) 使用Azure的私有Nuget仓库