From 7d87f790ab6f92a42cd629646e2f4a20c79f8280 Mon Sep 17 00:00:00 2001 From: Ivan Maidurov Date: Fri, 22 Nov 2024 11:14:24 +0100 Subject: [PATCH] feat(dingo): introduce slog and log levels --- Readme.md | 10 +++++++--- dingo.go | 16 +++++++++------- example/main.go | 3 ++- miniexample/main.go | 3 ++- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/Readme.md b/Readme.md index b10722f..3d8b869 100644 --- a/Readme.md +++ b/Readme.md @@ -426,16 +426,16 @@ type ( ) func (t *TplInterceptor) Render(context web.Context, name string, data interface{}) io.Reader { - log.Println("Before Rendering", name) + slog.Debug("Before Rendering", name) start := time.Now() r := t.Engine.Render(context, name, data) - log.Println("After Rendering", time.Since(start)) + slog.Debug("After Rendering", time.Since(start)) return r } func (f *FunctionInterceptor) Name() string { funcname := f.Function.Name() - log.Println("Function", funcname, "used") + slog.Debug("Function", funcname, "used") return funcname } ``` @@ -479,3 +479,7 @@ https://gocover.io/github.com/i-love-flamingo/dingo Dingo has a wrapper for `func(*Injector)` called `ModuleFunc`. It is possible to wrap a function with the `ModuleFunc` to become a `Module`. This is similar to the `http` Packages `HandlerFunc` mechanism and allows to save code and easier set up small projects. + +## Troubleshooting +1. To trace possible circular injections Dingo has function `EnableCircularTracing()`, which also switches slog to DEBUG level. This makes execution very heavy in terms of memory, so should be used only for debug purposes. +2. To trace possible injection issues, like when Dingo tries to inject dependency into unexported field and fails, and user does not know where this happens, Dingo has `EnableInjectionTracing()`, which is also sets slog level to DEBUG. diff --git a/dingo.go b/dingo.go index 953fc6e..241b83f 100644 --- a/dingo.go +++ b/dingo.go @@ -3,7 +3,7 @@ package dingo import ( "errors" "fmt" - "log" + "log/slog" "reflect" "strings" ) @@ -27,10 +27,12 @@ var ( // this is super expensive (memory wise), so it should only be used for debugging purposes func EnableCircularTracing() { traceCircular = make([]circularTraceEntry, 0) + slog.SetLogLoggerLevel(slog.LevelDebug) } func EnableInjectionTracing() { injectionTracing = true + slog.SetLogLoggerLevel(slog.LevelDebug) } type ( @@ -380,9 +382,9 @@ func (injector *Injector) createInstanceOfAnnotatedType(t reflect.Type, annotati for _, ct := range circularTrace { if ct.typ == t && ct.annotation == annotation { for _, ct := range circularTrace { - log.Println(ct.typ.PkgPath() + "#" + ct.typ.Name() + ": " + ct.annotation) + slog.Debug(fmt.Sprintf("%s#%s: %s", ct.typ.PkgPath(), ct.typ.Name(), ct.annotation)) } - log.Println(t.PkgPath() + "#" + t.Name() + ": " + annotation) + slog.Debug(fmt.Sprintf("%s#%s: %s", t.PkgPath(), t.Name(), annotation)) panic("detected circular dependency") } } @@ -396,9 +398,9 @@ func (injector *Injector) createInstanceOfAnnotatedType(t reflect.Type, annotati if injectionTracing { if t.PkgPath() == "" || t.Name() == "" { - log.Println("INJECTING: " + t.String()) + slog.Debug(fmt.Sprintf("INJECTING: %s", t.String())) } else { - log.Println("INJECTING: " + t.PkgPath() + "#" + t.Name() + " \"" + annotation + "\"") + slog.Debug(fmt.Sprintf("INJECTING: %s#%s \"%s\"", t.PkgPath(), t.Name(), annotation)) } } @@ -759,7 +761,7 @@ func (injector *Injector) requestInjection(object interface{}, circularTrace []c } if field.Kind() != reflect.Ptr && field.Kind() != reflect.Interface && instance.Kind() == reflect.Ptr { if injectionTracing { - log.Println("SETTING FIELD: \"" + currentFieldName + "\" of type: \"" + ctype.Field(fieldIndex).Type.String() + "\"") + slog.Debug(fmt.Sprintf("SETTING FIELD: %s of type \"%s\"", currentFieldName, ctype.Field(fieldIndex).Type.String())) } field.Set(instance.Elem()) @@ -769,7 +771,7 @@ func (injector *Injector) requestInjection(object interface{}, circularTrace []c } if injectionTracing { - log.Println("SETTING FIELD: \"" + currentFieldName + "\" of type: \"" + ctype.Field(fieldIndex).Type.String() + "\"") + slog.Debug(fmt.Sprintf("SETTING FIELD: %s of type \"%s\"", currentFieldName, ctype.Field(fieldIndex).Type.String())) } field.Set(instance) diff --git a/example/main.go b/example/main.go index 53a31af..a429630 100644 --- a/example/main.go +++ b/example/main.go @@ -2,6 +2,7 @@ package main import ( "log" + "log/slog" "flamingo.me/dingo" "flamingo.me/dingo/example/application" @@ -16,7 +17,7 @@ var _ application.TransactionLog = new(stdloggerTransactionLog) // Log a message with the configure prefix func (s *stdloggerTransactionLog) Log(id, message string) { - log.Println(s.prefix, id, message) + slog.Info(s.prefix, id, message) } type defaultModule struct{} diff --git a/miniexample/main.go b/miniexample/main.go index cb0ff63..fa77adb 100644 --- a/miniexample/main.go +++ b/miniexample/main.go @@ -2,6 +2,7 @@ package main import ( "log" + "log/slog" "flamingo.me/dingo" "flamingo.me/dingo/miniexample/logger" @@ -11,7 +12,7 @@ type stdLogger struct{} // Log logs a message func (s *stdLogger) Log(message string) { - log.Println(message) + slog.Info(message) } type loggerModule struct{}