Last active
November 12, 2020 15:14
-
-
Save Billcountry/52a92511aa919bfc5b0ee936dda275c7 to your computer and use it in GitHub Desktop.
Demo code to write logs to alt4.dev using gin gonic
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
package main | |
import ( | |
"fmt" | |
"github.com/alt4dev/go/log" | |
"github.com/gin-gonic/gin" | |
) | |
func loggingMiddleWare(ctx *gin.Context) { | |
// Start a log group and defer it's close at the end of the request | |
defer log.Claims{ | |
"path": ctx.Request.URL, | |
"method": ctx.Request.Method, | |
"user-agent": ctx.Request.UserAgent(), | |
}.Group(fmt.Sprintf("[%s] %s", ctx.Request.Method, ctx.Request.URL)).Close() | |
// Proceed with the logic to process the request | |
ctx.Next() | |
} | |
func main(){ | |
router := gin.Default() | |
router.Use(loggingMiddleWare) | |
// Set alt4 writer as default output for all logs | |
log.SetFlags(0) // Only logs the message. Alt4 discovers the rest. | |
log.SetOutput(service.SyncWriter) | |
gin.DefaultWriter = service.Writer | |
gin.DefaultErrorWriter = service.Writer | |
// ... stuff ... | |
if err := router.Run(); err != nil { | |
// You really need to see what you did wrong | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment