Logging
This commit is contained in:
commit
5a0f51fd49
3 changed files with 57 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
vendor/
|
||||
go.sum
|
||||
14
go.mod
Normal file
14
go.mod
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
module git.keks.cloud/kekskurse/utils
|
||||
|
||||
go 1.23.1
|
||||
|
||||
require (
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/rs/zerolog v1.33.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.19 // indirect
|
||||
golang.org/x/sys v0.12.0 // indirect
|
||||
)
|
||||
41
web/middelware/logging.go
Normal file
41
web/middelware/logging.go
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
package middelware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type Logging struct {
|
||||
log zerolog.Logger
|
||||
}
|
||||
|
||||
func NewCLILogger() Logging {
|
||||
log := log.Logger.With().Str("pkg", "middelware").Logger().Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
||||
l := Logging{
|
||||
log: log,
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
func (l Logging) Middelware(someThing string) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
fn := func(w http.ResponseWriter, r *http.Request) {
|
||||
// Logic here
|
||||
uid := uuid.NewString()
|
||||
|
||||
start := time.Now()
|
||||
// Call the next handler
|
||||
next.ServeHTTP(w, r)
|
||||
elapsed := time.Since(start)
|
||||
|
||||
log.Info().Str("method", r.Method).Str("URI", r.URL.String()).Str("uuid", uid).Int64("request time ms", elapsed.Milliseconds()).Msg("Get Request")
|
||||
}
|
||||
|
||||
return http.HandlerFunc(fn)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue