-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
107 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package common | ||
|
||
// @author Ritchie | ||
// @version 1.0, 2021/10/5 | ||
// @since 1.0.0 | ||
|
||
var ( | ||
REQUEST_ID = "request_id" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package http_status | ||
|
||
// @author Ritchie | ||
// @version 2.0, 2021/10/5 | ||
// @since 2.0.0 | ||
|
||
type HttpStatus struct { | ||
Msg interface{} | ||
Code int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package customize_router | ||
|
||
import ( | ||
"github.com/Chronostasys/centralog/centralog" | ||
"github.com/nick887/customize_router/custom" | ||
"github.com/nick887/customize_router/log" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// @author Ritchie | ||
// @version 1.0, 2021/10/5 | ||
// @since 1.0.0 | ||
|
||
|
||
func InitCustomRouter(logOpt *centralog.LogOptions,cr *custom.CustmoRouter) { | ||
err := centralog.InitLoggerWithOpt(zap.NewProductionConfig(), logOpt) | ||
if err != nil { | ||
panic(err) | ||
} | ||
cr.Use(log.Log()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package log | ||
|
||
import ( | ||
"fmt" | ||
"github.com/Chronostasys/centralog/centralog" | ||
"github.com/gin-gonic/gin" | ||
"github.com/nick887/customize_router/common" | ||
"time" | ||
"github.com/google/uuid" | ||
) | ||
|
||
// @author Ritchie | ||
// @version 1.0, 2021/10/5 | ||
// @since 1.0.0 | ||
|
||
|
||
func Log() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
// request id | ||
requestId := c.GetHeader(common.REQUEST_ID) | ||
var id string | ||
if requestId == "" { | ||
id = uuid.New().String() | ||
c.Header(common.REQUEST_ID, id) | ||
} else { | ||
id = c.GetHeader(common.REQUEST_ID) | ||
} | ||
method := c.Request.Method | ||
url := c.Request.URL.RequestURI() | ||
c.Set(centralog.IDKey, id) | ||
defer centralog.Sync() | ||
start := time.Now() | ||
c.Next() | ||
centralog.Info(method). | ||
Any("elapsed", | ||
fmt.Sprintf("%f", float64(time.Since(start).Microseconds())/1000)+"ms"). | ||
Any("url", url). | ||
Any("ip", c.ClientIP()). | ||
CtxID(c). | ||
Log() | ||
} | ||
} |