Skip to content

Instantly share code, notes, and snippets.

@Billcountry
Last active November 12, 2020 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Billcountry/52a92511aa919bfc5b0ee936dda275c7 to your computer and use it in GitHub Desktop.
Save Billcountry/52a92511aa919bfc5b0ee936dda275c7 to your computer and use it in GitHub Desktop.
Demo code to write logs to alt4.dev using gin gonic
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