Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions tslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ const TSLOG core.Provide = "TS_LOG"
func ForRoot(h slog.Handler) core.Modules {
return func(module core.Module) core.Module {
module.NewProvider(core.ProviderOptions{
Name: TSLOG,
Value: slog.New(h),
Name: TSLOG,
Value: slog.New(h),
Status: core.PUBLIC,
})

return module
Expand Down
44 changes: 44 additions & 0 deletions tslog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,47 @@ func TestForRoot(t *testing.T) {
require.Nil(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
}

func TestInherit(t *testing.T) {
ctrlFnc := func(module core.Module) core.Controller {
ctrl := module.NewController("test")
logger := tslog.Inject(module)

ctrl.Get("", func(ctx core.Ctx) error {
logger.Info("Request processed",
"http", slog.Group("request",
"method", "GET",
"path", "/api/users",
"status", 200,
),
"duration_ms", 150,
)
return ctx.JSON(true)
})
return ctrl
}

parentMod := func(module core.Module) core.Module {
return module.New(core.NewModuleOptions{
Imports: []core.Modules{tslog.ForRoot(slog.NewJSONHandler(os.Stdout, nil))},
})
}

childMod := func() core.Module {
return core.NewModule(core.NewModuleOptions{
Imports: []core.Modules{parentMod},
Controllers: []core.Controllers{ctrlFnc},
})
}

app := core.CreateFactory(childMod)

testServer := httptest.NewServer(app.PrepareBeforeListen())
defer testServer.Close()

testClient := testServer.Client()

resp, err := testClient.Get(testServer.URL + "/test")
require.Nil(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
}
Loading