开始一个gin框架的web项目
一、goland创建项目
1、创建项目
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pl1kkHvI-1595235494094)(/Users/bobwang/Library/Application Support/typora-user-images/image-20200717111649445.png)]
2、开始时的文件目录
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-duqJdOrg-1595235494097)(/Users/bobwang/Library/Application Support/typora-user-images/image-20200717111940388.png)]
3、新建main.go文件
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yG4QYQ57-1595235494099)(/Users/bobwang/Library/Application Support/typora-user-images/image-20200717112114397.png)]
二、安装gin框架
1.下载并安装 gin:
$ go get -u github.com/gin-gonic/gin
2.将 gin 引入到代码中:
import "github.com/gin-gonic/gin"
3.(可选)如果使用诸如 http.StatusOK
之类的常量,则需要引入 net/http
包:
import "net/http"
三、hello demo
package main
import (
"github.com/gin-gonic/gin"
"log"
"net/http"
)
type user struct {
Name string `json:"name"`
Age int
}
func sayHi(c *gin.Context) {
c.JSON(http.StatusOK,gin.H{
"message":"hello gin",
})
}
// json返回结构体
func sayBye(c *gin.Context) {
c.JSON(http.StatusOK,user{"bob",22})
}
func main() {
r := gin.Default()
r.GET("/hello",sayHi)
r.GET("/bye",sayBye)
err :=r.Run()
if err !=nil{
log.Fatal("gin run wrong")
}
}
启动默认8080端口
四、日志的记录
1、官方的方式
func main() {
// log
gin.DisableConsoleColor()
f, _ := os.Create("logs/gin.log")
gin.DefaultWriter = io.MultiWriter(f,os.Stdout)
r := gin.Default()
r.GET("/hello",sayHi)
r.GET("/bye",sayBye)
err :=r.Run()
if err !=nil{
log.Fatal("gin run wrong")
}
}
2、使用zap接收gin框架默认的日志并配置日志归档
// 安装 lumberjack,zap
go get -u github.com/natefinch/lumberjack
go get -u go.uber.org/zap
注册中间件的操作在router.SetupRouter()
中:
package router
import (
"fmt"
"gin_demo/logger"
"github.com/gin-gonic/gin"
"time"
)
func SetupRouter() *gin.Engine {
r := gin.New()
// 使用zap日志库
r.Use(logger.GinLogger(logger.Logger), logger.GinRecovery(logger.Logger, true))
// 路由组
mainGroup := r.Group("/api")
{
// Example ping request.
mainGroup.GET("/hello", func(c *gin.Context) {
c.String(200, "hello world !!! "+fmt.Sprint(time.Now().Format("2006-01-2 15:04:05")))
})
// Example when panic happen.
mainGroup.GET("/panic", func(c *gin.Context) {
panic("An unexpected error happen!")
})
mainGroup.GET("/hi", func(c *gin.Context) {
c.String(200, "hi123")
})
}
r.GET("/ping", func(c *gin.Context) {
c.String(200, "pong")
})
return r
}
zap库的设置操作在logger.go
package logger
import (
"gin_demo/config" // 配置文件
"github.com/gin-gonic/gin"
"github.com/natefinch/lumberjack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"net"
"net/http"
"net/http/httputil"
"os"
"runtime/debug"
"strings"
"time"
)
var Logger *zap.Logger
// InitLogger 初始化Logger
func InitLogger(cfg *config.LogConfig) (err error) {
writeSyncer := getLogWriter(cfg.Filename, cfg.MaxSize, cfg.MaxBackups, cfg.MaxAge)
encoder := getEncoder()
// for human operators.
var l = new(zapcore.Level)
err = l.UnmarshalText([]byte(cfg.Level))
if err != nil {
return
}
// 打印到控制台和日志文件
core := zapcore.NewCore(encoder,
zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stderr), writeSyncer),
l)
Logger = zap.New(core, zap.AddCaller())
return
}
func getEncoder() zapcore.Encoder {
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
encoderConfig.TimeKey = "time"
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
encoderConfig.EncodeDuration = zapcore.SecondsDurationEncoder
encoderConfig.EncodeCaller = zapcore.ShortCallerEncoder
return zapcore.NewJSONEncoder(encoderConfig)
}
func getLogWriter(filename string, maxSize, maxBackup, maxAge int) zapcore.WriteSyncer {
lumberJackLogger := &lumberjack.Logger{
Filename: filename,
MaxSize: maxSize,
MaxBackups: maxBackup,
MaxAge: maxAge,
}
return zapcore.AddSync(lumberJackLogger)
}
// GinLogger 接收gin框架默认的日志
func GinLogger(logger *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
path := c.Request.URL.Path
query := c.Request.URL.RawQuery
c.Next()
cost := time.Since(start)
logger.Info(path,
zap.Int("status", c.Writer.Status()),
zap.String("method", c.Request.Method),
zap.String("path", path),
zap.String("query", query),
zap.String("ip", c.ClientIP()),
zap.String("user-agent", c.Request.UserAgent()),
zap.String("errors", c.Errors.ByType(gin.ErrorTypePrivate).String()),
zap.Duration("cost", cost),
)
}
}
// GinRecovery recover掉项目可能出现的panic,并使用zap记录相关日志
func GinRecovery(logger *zap.Logger, stack bool) gin.HandlerFunc {
return func(c *gin.Context) {
defer func() {
if err := recover(); err != nil {
// Check for a broken connection, as it is not really a
// condition that warrants a panic stack trace.
var brokenPipe bool
if ne, ok := err.(*net.OpError); ok {
if se, ok := ne.Err.(*os.SyscallError); ok {
if strings.Contains(strings.ToLower(se.Error()), "broken pipe") || strings.Contains(strings.ToLower(se.Error()), "connection reset by peer") {
brokenPipe = true
}
}
}
httpRequest, _ := httputil.DumpRequest(c.Request, false)
if brokenPipe {
logger.Error(c.Request.URL.Path,
zap.Any("error", err),
zap.String("request", string(httpRequest)),
)
// If the connection is dead, we can't write a status to it.
c.Error(err.(error)) // nolint: errcheck
c.Abort()
return
}
if stack {
logger.Error("[Recovery from panic]",
zap.Any("error", err),
zap.String("request", string(httpRequest)),
zap.String("stack", string(debug.Stack())),
)
} else {
logger.Error("[Recovery from panic]",
zap.Any("error", err),
zap.String("request", string(httpRequest)),
)
}
c.AbortWithStatus(http.StatusInternalServerError)
}
}()
c.Next()
}
}
配置文件config.go
package config
type LogConfig struct {
Level string `json:"level"`
Filename string `json:"filename"`
MaxSize int `json:"maxsize"`
MaxAge int `json:"max_age"`
MaxBackups int `json:"max_backups"`
}
项目启动文件main.go
package main
import (
"fmt"
"gin_demo/config"
"gin_demo/logger"
"gin_demo/router"
"github.com/gin-gonic/gin"
"log"
"net/http"
)
type user struct {
Name string `json:"name"`
Age int
}
func sayHi(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "hello gin",
})
}
func sayBye(c *gin.Context) {
c.JSON(http.StatusOK, user{"bob", 22})
}
func main() {
// log
// 这只日志的配置内容
logConf := config.LogConfig{Level: "info", Filename: "logs/zap_gin.log", MaxSize: 1, MaxAge: 30, MaxBackups: 5}
// 初始化
if err := logger.InitLogger(&logConf); err != nil {
fmt.Printf("init logger failed, err:%v\n", err)
return
}
r := gin.New()
r = router.SetupRouter()
err := r.Run(":8888")
if err != nil {
log.Fatal("gin run wrong")
}
}
日志输出格式
func getEncoder() zapcore.Encoder {
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
encoderConfig.TimeKey = "time"
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
encoderConfig.EncodeDuration = zapcore.SecondsDurationEncoder
encoderConfig.EncodeCaller = zapcore.ShortCallerEncoder
return zapcore.NewJSONEncoder(encoderConfig)
}
zapcore.NewJSONEncoder(encoderConfig)
2020-07-20T16:47:44.436+0800 INFO logger/logger.go:69 /hello {"status": 200, "method": "GET", "path": "/hello", "query": "", "ip": "::1", "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36", "errors": "", "cost": 0.000189596}
zapcore.NewConsoleEncoder(encoderConfig)
2020-07-20T16:47:44.436+0800 INFO logger/logger.go:69 /hello {"status": 200, "method": "GET", "path": "/hello", "query": "", "ip": "::1", "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36", "errors": "", "cost": 0.000189596}
3、 报错err的详情记录 “ c.Error(err)”
单单上面的设置还不能记录错误的详细信息,在zap.String("errors", c.Errors.ByType(gin.ErrorTypePrivate).String())
中的c.Errors还是为nil,若要记录,需要在代码中抛出的err
位置,使用c.Error(err)
,才能被最终捕获错误,例如在router.go中增加一个路由
r.GET("/sentPic", func(c *gin.Context) {
res, err := sendEmotionPic(pb.NewEmotionServerClient(conn), data, imgFile.Filename) // sendEmotionPic为gprc调用服务
if err != nil {
//此时的err需要被context捕获才能被zap记录
c.Error(err) // 重点
c.JSON(http.StatusInternalServerError, gin.H{
"status": 501,
"message": err.Error(),
"msg": "图片未识别出表情",
"data": res,
})
return
}
c.JSON(http.StatusOK, gin.H{
"status": 200,
// "message": fmt.Sprintf("'%s' recognized!", imgFile.Filename),
"msg": "successful",
"data": res,
})
}