exception.go
package exception
import (
"fmt"
"github.com/gin-gonic/gin"
"runtime/debug"
"strings"
"time"
"why/util"
)
func ThrowPanic(dir string) gin.HandlerFunc{
return func(c *gin.Context) {
defer func() {
if err := recover(); err != nil {
DebugStack := ""
for _, v := range strings.Split(string(debug.Stack()), "\n") {
DebugStack += v + "\n"
}
date := time.Now().Format("2006-01-02")
dateTime := time.Now().Format("2006-01-02 15:04:05")
file := dir + date + ".err"
util.WriteWithIo(file, dateTime+"\n")
util.WriteWithIo(file, fmt.Sprintf("%v\r\n", err))
util.WriteWithIo(file, DebugStack)
}
}()
c.Next()
}
}
関連するコードをmain.go
errorLogConfig := make(map[string]string)
errLogSection := "error"
errorLogConfig = config.GetConfigEntrance("log", errLogSection)
errorLogdir := errorLogConfig[errLogSection + "_dir"]
server := gin.New()
server.Use(exception.ThrowPanic(errorLogdir))
手動でのジンのルーティング方法でスロー
panic(111)
結果を参照してください。
2020-01-15 16:42:17
111
goroutine 3 [running]:
runtime/debug.Stack(0xc0001a3470, 0x158f560, 0x173c7e0)
/usr/local/go/src/runtime/debug/stack.go:24 +0x9d
why/exception.ThrowPanic.func1.1(0xc00002a1bc, 0x21)
/Users/why/Desktop/go/src/why/exception/exception.go:17 +0x65
panic(0x158f560, 0x173c7e0)
/usr/local/go/src/runtime/panic.go:679 +0x1b2
main.main.func2(0xc000270d00)
/Users/why/Desktop/go/main.go:68 +0x131
github.com/gin-gonic/gin.(*Context).Next(0xc000270d00)
/Users/why/Desktop/go/src/github.com/gin-gonic/gin/context.go:147 +0x3b
why/exception.ThrowPanic.func1(0xc000270d00)
/Users/why/Desktop/go/src/why/exception/exception.go:29 +0x63
github.com/gin-gonic/gin.(*Context).Next(0xc000270d00)
/Users/why/Desktop/go/src/github.com/gin-gonic/gin/context.go:147 +0x3b
why/log.LoggerMiddleware.func1(0xc000270d00)
/Users/why/Desktop/go/src/why/log/log.go:83 +0x72c
github.com/gin-gonic/gin.(*Context).Next(0xc000270d00)
/Users/why/Desktop/go/src/github.com/gin-gonic/gin/context.go:147 +0x3b
why/trace.OpenTracing.func4(0xc000270d00)
/Users/why/Desktop/go/src/why/trace/trace.go:96 +0x641
github.com/gin-gonic/gin.(*Context).Next(0xc000270d00)
/Users/why/Desktop/go/src/github.com/gin-gonic/gin/context.go:147 +0x3b
github.com/gin-gonic/gin.RecoveryWithWriter.func1(0xc000270d00)
/Users/why/Desktop/go/src/github.com/gin-gonic/gin/recovery.go:83 +0x64
github.com/gin-gonic/gin.(*Context).Next(0xc000270d00)
/Users/why/Desktop/go/src/github.com/gin-gonic/gin/context.go:147 +0x3b
github.com/gin-gonic/gin.(*Engine).handleHTTPRequest(0xc000274640, 0xc000270d00)
/Users/why/Desktop/go/src/github.com/gin-gonic/gin/gin.go:411 +0x66d
github.com/gin-gonic/gin.(*Engine).ServeHTTP(0xc000274640, 0x1755fc0, 0xc0002be1c0, 0xc0002a4100)
/Users/why/Desktop/go/src/github.com/gin-gonic/gin/gin.go:369 +0x14e
net/http.serverHandler.ServeHTTP(0xc0002be0e0, 0x1755fc0, 0xc0002be1c0, 0xc0002a4100)
/usr/local/go/src/net/http/server.go:2802 +0xa4
net/http.(*conn).serve(0xc000276460, 0x17579c0, 0xc000030380)
/usr/local/go/src/net/http/server.go:1890 +0x875
created by net/http.(*Server).Serve
/usr/local/go/src/net/http/server.go:2928 +0x384
PS:ツールに関するチュートリアルで使用されるコードは、独自のパッケージをリンク
コンフィギュレーションを読み込みconfig.GetConfigEntrance