refactor(Logger)!: harmonize naming 4

This commit is contained in:
2024-10-15 13:31:00 +02:00
parent b40f588537
commit aacb529fb6
6 changed files with 6 additions and 6 deletions

48
logger/logger.go Normal file
View File

@@ -0,0 +1,48 @@
package Logger
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"sync"
)
// Use this in the init() function to initialize the size of the buffered channel
const logbuffersize int32 = 200
var DEBUG bool = true
var (
loggerInstance Logger
loggeronce sync.Once
loggerlogonce sync.Once
)
func Create(instance Logger) {
loggeronce.Do(func() {
loggerInstance = instance
loggerInstance.init()
})
}
func LoggerInstance() Logger {
return loggerInstance
}
func PrintJson[T any](entity *T) string {
typename := reflect.TypeFor[T]().Name()
outputStringJson, err := json.MarshalIndent((*entity), "", " ")
if err != nil {
return "Error parsing json data"
} else {
return typename + ":\n" + string(outputStringJson) + "\n"
}
}
func ExampleLogMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
LoggerInstance().WriteDebug(fmt.Sprintf("\tRequest from: %s to: Host: %s URL: %s \tWith HEADERS: %s \tWith BODY: %s", r.RemoteAddr, r.Host, r.URL, r.Header, r.Body))
next.ServeHTTP(w, r)
})
}