package main

import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"time"
) func main() {
router := gin.New()
// LoggerWithFormatter middleware will write the logs to gin.DefaultWriter
// 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, // 客户端IP
param.TimeStamp.Format(time.RFC3339), // 请求时间
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.POST("/string", func(context *gin.Context) {
context.JSON(http.StatusOK, "OK")
})
router.Run("192.168.110.20:3000")
}

  

日志输出格式:

/*
192.168.110.20 - [2021-10-27T10:42:28+08:00] "GET /ping HTTP/1.1 200 563.7µs "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36" "
192.168.110.20 - [2021-10-27T10:42:57+08:00] "GET /ping HTTP/1.1 200 0s "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0" "
192.168.110.20 - [2021-10-27T10:42:58+08:00] "GET /favicon.ico HTTP/1.1 404 0s "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0" "
192.168.110.20 - [2021-10-27T10:44:00+08:00] "POST /string HTTP/1.1 200 592.2µs "PostmanRuntime/7.26.8" "
192.168.110.20 - [2021-10-27T10:44:24+08:00] "GET /ping HTTP/1.1 200 0s "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36 Edg/95.0.1020.30" "
192.168.110.20 - [2021-10-27T10:44:37+08:00] "GET /favicon.ico HTTP/1.1 404 0s "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36 Edg/95.0.1020.30" "
*/

  

最新文章

  1. windows下面配置apache+http
  2. onTouch与onClick冲突解决方法
  3. 第一天 django
  4. android使用Webview上传图片
  5. ASIHTTPRequest实现https双向认证请求
  6. jquery and js 判断一个元素是否存在
  7. .net概述1
  8. 使用安卓中的TextToSpeech控件实现朗读文字
  9. Python 列表生成式、生成器、迭代器
  10. 看到当年自己学SQL Server 的笔记
  11. 工作日志2014-06-10(实现C语言解析XML获得查询关键字)
  12. linux 定时执行任务
  13. 如何通过java反射将数据库表生成实体类?
  14. php笔记(二)PHP类和对象之Static静态关键字
  15. BZOJ 2038: [2009国家集训队]小Z的袜子(hose)&&莫对算法
  16. js在(FF)中长字段溢出(自动换行)
  17. Maven学习 使用Nexus搭建Maven私服(转)
  18. Ubuntu 16.04下sublime text3安装
  19. Solr7.1--- 单机Linux环境搭建
  20. django----常用功能

热门文章

  1. AcWing09. 分组背包问题
  2. JAVA遍历某个文件夹下所有文件listFiles() 实现按照名称升序排序
  3. JAVA判断IP是否是内网IP
  4. 【LeetCode】415. Add Strings 解题报告(Python)
  5. 【LeetCode】985. Sum of Even Numbers After Queries 解题报告(C++)
  6. 【LeetCode】419. Battleships in a Board 解题报告(Python & C++)
  7. 【剑指Offer】二叉树的镜像 解题报告(Python)
  8. Now冥想:崩溃服务和性能服务助力提升应用质量
  9. Service有多个实现类,它怎么知道该注入哪个ServiceImpl类
  10. Java中的构造方法「注意事项」