You've already forked golang-saas-starter-kit
mirror of
https://github.com/raseels-repos/golang-saas-starter-kit.git
synced 2025-06-23 00:37:48 +02:00
Imported github.com/ardanlabs/service as base example project
This commit is contained in:
49
example-project/internal/mid/logger.go
Normal file
49
example-project/internal/mid/logger.go
Normal file
@ -0,0 +1,49 @@
|
||||
package mid
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/web"
|
||||
"go.opencensus.io/trace"
|
||||
)
|
||||
|
||||
// Logger writes some information about the request to the logs in the
|
||||
// format: TraceID : (200) GET /foo -> IP ADDR (latency)
|
||||
func Logger(log *log.Logger) web.Middleware {
|
||||
|
||||
// This is the actual middleware function to be executed.
|
||||
f := func(before web.Handler) web.Handler {
|
||||
|
||||
// Create the handler that will be attached in the middleware chain.
|
||||
h := func(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
|
||||
ctx, span := trace.StartSpan(ctx, "internal.mid.Logger")
|
||||
defer span.End()
|
||||
|
||||
// If the context is missing this value, request the service
|
||||
// to be shutdown gracefully.
|
||||
v, ok := ctx.Value(web.KeyValues).(*web.Values)
|
||||
if !ok {
|
||||
return web.NewShutdownError("web value missing from context")
|
||||
}
|
||||
|
||||
err := before(ctx, w, r, params)
|
||||
|
||||
log.Printf("%s : (%d) : %s %s -> %s (%s)\n",
|
||||
v.TraceID,
|
||||
v.StatusCode,
|
||||
r.Method, r.URL.Path,
|
||||
r.RemoteAddr, time.Since(v.Now),
|
||||
)
|
||||
|
||||
// Return the error so it can be handled further up the chain.
|
||||
return err
|
||||
}
|
||||
|
||||
return h
|
||||
}
|
||||
|
||||
return f
|
||||
}
|
Reference in New Issue
Block a user