glogrus2 provides structured logging via logrus for Goji2.
Simple logging
package main
import(
"github.com/goji/glogrus2"
"goji.io"
"net/http"
"github.com/Sirupsen/logrus"
)
func main() {
router := goji.NewMux()
logr := logrus.New()
logr.Formatter = new(logrus.JSONFormatter)
goji.Use(glogrus.NewGlogrus(logr, "my-app-name"))
log.Fatal(http.ListenAndServe(":8080", router))
}
Logging with custom request id from http Context
package main
import(
"github.com/goji/glogrus2"
"goji.io"
"golang.org/x/net/context"
"net/http"
"github.com/Sirupsen/logrus"
)
func main() {
router := goji.NewMux()
logr := logrus.New()
logr.Formatter = new(logrus.JSONFormatter)
router.UseC(glogrus.NewGlogrusWithReqId(logr, "my-app-name", IdFromContext))
log.Fatal(http.ListenAndServe(":8080", router))
}
func IdFromContext(ctx context.Context) string {
return ctx.Value("requestIdKey")
}