pre-dev: initial commit

This commit is contained in:
2025-08-11 10:25:54 +07:00
parent e0ecf1c906
commit ee9b4a9035
50 changed files with 1020 additions and 1 deletions
View File
@@ -0,0 +1,52 @@
package handlerloggermw
import (
"encoding/json"
"fmt"
"net/http"
"time"
l "github.com/karincake/apem/loggera"
lo "github.com/karincake/apem/loggero"
)
var Logger l.LoggerItf
type wrappedWriter struct {
http.ResponseWriter
statusCode int
}
func (w *wrappedWriter) WriteHeader(statusCode int) {
w.statusCode = statusCode
w.ResponseWriter.WriteHeader(statusCode)
}
func SetLog(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
wrapped := &wrappedWriter{
ResponseWriter: w,
statusCode: http.StatusOK,
}
next.ServeHTTP(wrapped, r)
Logger.Info().
String("scope", "request").
Int("status", wrapped.statusCode).
String("method", r.Method).
String("path", r.URL.Path).
String("query", r.URL.RawQuery).
String("duration", time.Since(start).String()).Send()
})
}
func WriteJson(data any) {
lo.I.Println("Showing additional info for the payload")
js, err := json.Marshal(data)
if err == nil {
fmt.Println(string(js))
} else {
fmt.Println("error converting data or result to json:", err)
}
}