Skip to content

Commit

Permalink
feat(dingo): introduce slog and log levels
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanMaidurov committed Nov 22, 2024
1 parent 48cea9f commit 7d87f79
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
10 changes: 7 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```
Expand Down Expand Up @@ -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.
16 changes: 9 additions & 7 deletions dingo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dingo
import (
"errors"
"fmt"
"log"
"log/slog"
"reflect"
"strings"
)
Expand All @@ -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)

Check failure on line 30 in dingo.go

View workflow job for this annotation

GitHub Actions / lint

only cuddled expressions if assigning variable or using from line above (wsl)
}

func EnableInjectionTracing() {
injectionTracing = true
slog.SetLogLoggerLevel(slog.LevelDebug)

Check failure on line 35 in dingo.go

View workflow job for this annotation

GitHub Actions / lint

only cuddled expressions if assigning variable or using from line above (wsl)
}

type (
Expand Down Expand Up @@ -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))

Check failure on line 387 in dingo.go

View workflow job for this annotation

GitHub Actions / lint

expressions should not be cuddled with blocks (wsl)
panic("detected circular dependency")
}
}
Expand All @@ -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))
}
}

Expand Down Expand Up @@ -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())
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"log"
"log/slog"

"flamingo.me/dingo"
"flamingo.me/dingo/example/application"
Expand All @@ -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{}
Expand Down
3 changes: 2 additions & 1 deletion miniexample/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"log"
"log/slog"

"flamingo.me/dingo"
"flamingo.me/dingo/miniexample/logger"
Expand All @@ -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{}
Expand Down

0 comments on commit 7d87f79

Please sign in to comment.