当前位置:网站首页>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" "
边栏推荐
- [rust] reference and borrowing, string slice type (& STR) - rust language foundation 12
- Spirng @Conditional 条件注解的使用
- [fluent -- example] case 1: comprehensive example of basic components and layout components
- 【11】 Production and adjustment of vector and grid data Legends
- [high concurrency] deeply analyze the execution process of worker threads in the thread pool through the source code
- I want to ask whether DMS has the function of regularly backing up a database?
- Crawler crawls dynamic website
- SSTI template injection vulnerability summary [bjdctf2020]cookie is so stable
- More accurate and efficient segmentation of organs-at-risk in radiotherapy with Convolutional Neural
- SSTI 模板注入漏洞总结之[BJDCTF2020]Cookie is so stable
猜你喜欢

Selenium use -- installation and testing

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

阿里云技术专家秦隆:可靠性保障必备——云上如何进行混沌工程?

2022.07.24(LC_6124_第一个出现两次的字母)

What is ci/cd?

The first scratch crawler

零基础学习CANoe Panel(13)—— 滑条(TrackBar )

Communication bus protocol I: UART

485通讯( 详解 )

Pytorch advanced training skills
随机推荐
启牛开的证券账户安全吗?是怎么开账户的
【AI4Code】《GraphCodeBERT: Pre-Training Code Representations With DataFlow》 ICLR 2021
mysql有 flush privileges 吗
Detailed explanation of switch link aggregation [Huawei ENSP]
Ecological profile of pytorch
Ansible
Maskgae: masked graph modeling meets graph autoencoders
Introduction to the scratch crawler framework
Moving Chinese figure liushenglan
基于Caffe ResNet-50网络实现图片分类(仅推理)的实验复现
弹性盒子(Flex Box)详解
Jenkins configuration pipeline
零基础学习CANoe Panel(16)—— Clock Control/Panel Control/Start Stop Control/Tab Control
[advanced C language] dynamic memory management
想要白嫖正则大全是吧?这一次给你个够!
Lu MENGZHENG's "Fu of broken kiln"
Pytorch main module
Keeping MySQL highly available
Intval MD5 bypass [wustctf2020] plain
【运维、实施精品】月薪10k+的技术岗位面试技巧