From 9dd8189e02da67b8f4dfeb7e0ee77a4d5bd83e5c Mon Sep 17 00:00:00 2001 From: fernandoalonso Date: Mon, 11 Nov 2024 18:54:54 +0000 Subject: [PATCH 1/5] put the level to debug --- go/core/logger/logger.go | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/go/core/logger/logger.go b/go/core/logger/logger.go index 9ab8d36c2..ec83fa10b 100644 --- a/go/core/logger/logger.go +++ b/go/core/logger/logger.go @@ -26,10 +26,11 @@ import ( func init() { // TODO: Remove this. The main program should be responsible for configuring logging. // This is just a convenience during development. - h := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{ - Level: slog.LevelDebug, - })) - slog.SetDefault(h) + baseHandler := slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{}) + debugHandler := &DebugOnlyHandler{ + h: baseHandler, + } + slog.SetDefault(slog.New(debugHandler)) } var loggerKey = base.NewContextKey[*slog.Logger]() @@ -42,3 +43,24 @@ func FromContext(ctx context.Context) *slog.Logger { } return slog.Default() } + +// DebugOnlyHandler is a custom handler that only logs DEBUG messages. +type DebugOnlyHandler struct { + h slog.Handler +} + +func (dDebugOnlyHandler) Enabled(ctx context.Context, level slog.Level) bool { + return level == slog.LevelDebug +} + +func (d DebugOnlyHandler) Handle(ctx context.Context, r slog.Record) error { + return d.h.Handle(ctx, r) +} + +func (dDebugOnlyHandler) WithAttrs(attrs []slog.Attr) slog.Handler { + return &DebugOnlyHandler{h: d.h.WithAttrs(attrs)} +} + +func (d *DebugOnlyHandler) WithGroup(name string) slog.Handler { + return &DebugOnlyHandler{h: d.h.WithGroup(name)} +} From 2bda8920a2a82a8120c245d56e2eeaafa626b60c Mon Sep 17 00:00:00 2001 From: fernandoalonso Date: Mon, 11 Nov 2024 21:26:03 +0000 Subject: [PATCH 2/5] allow multiple levels of logs --- go/core/logger/logger.go | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/go/core/logger/logger.go b/go/core/logger/logger.go index ec83fa10b..a69dad474 100644 --- a/go/core/logger/logger.go +++ b/go/core/logger/logger.go @@ -27,8 +27,9 @@ func init() { // TODO: Remove this. The main program should be responsible for configuring logging. // This is just a convenience during development. baseHandler := slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{}) - debugHandler := &DebugOnlyHandler{ - h: baseHandler, + debugHandler := &LevelFilterHandler{ + h: baseHandler, + level: slog.LevelDebug, } slog.SetDefault(slog.New(debugHandler)) } @@ -44,23 +45,31 @@ func FromContext(ctx context.Context) *slog.Logger { return slog.Default() } -// DebugOnlyHandler is a custom handler that only logs DEBUG messages. -type DebugOnlyHandler struct { - h slog.Handler +// LevelFilterHandler is a custom handler that only logs DEBUG messages. +type LevelFilterHandler struct { + level slog.Level + h slog.Handler } -func (dDebugOnlyHandler) Enabled(ctx context.Context, level slog.Level) bool { - return level == slog.LevelDebug +func (h *LevelFilterHandler) Enabled(ctx context.Context, level slog.Level) bool { + // Display the message if its level is greater than or equal to the configured level + return level <= h.level } -func (d DebugOnlyHandler) Handle(ctx context.Context, r slog.Record) error { - return d.h.Handle(ctx, r) +func (h *LevelFilterHandler) Handle(ctx context.Context, r slog.Record) error { + return h.h.Handle(ctx, r) } -func (dDebugOnlyHandler) WithAttrs(attrs []slog.Attr) slog.Handler { - return &DebugOnlyHandler{h: d.h.WithAttrs(attrs)} +func (h *LevelFilterHandler) WithAttrs(attrs []slog.Attr) slog.Handler { + return &LevelFilterHandler{ + level: h.level, + h: h.h.WithAttrs(attrs), + } } -func (d *DebugOnlyHandler) WithGroup(name string) slog.Handler { - return &DebugOnlyHandler{h: d.h.WithGroup(name)} -} +func (h *LevelFilterHandler) WithGroup(name string) slog.Handler { + return &LevelFilterHandler{ + level: h.level, + h: h.h.WithGroup(name), + } +} \ No newline at end of file From aee20f74faf7918c16022cf137945a974a8f35f2 Mon Sep 17 00:00:00 2001 From: fernandoalonso Date: Mon, 11 Nov 2024 21:33:50 +0000 Subject: [PATCH 3/5] change no essentials to debug --- go/core/logger/logger.go | 4 ++-- go/genkit/servers.go | 6 +++--- go/internal/registry/registry.go | 2 +- go/plugins/googlecloud/googlecloud_test.go | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/go/core/logger/logger.go b/go/core/logger/logger.go index a69dad474..67a21ffdd 100644 --- a/go/core/logger/logger.go +++ b/go/core/logger/logger.go @@ -53,7 +53,7 @@ type LevelFilterHandler struct { func (h *LevelFilterHandler) Enabled(ctx context.Context, level slog.Level) bool { // Display the message if its level is greater than or equal to the configured level - return level <= h.level + return level >= h.level } func (h *LevelFilterHandler) Handle(ctx context.Context, r slog.Record) error { @@ -72,4 +72,4 @@ func (h *LevelFilterHandler) WithGroup(name string) slog.Handler { level: h.level, h: h.h.WithGroup(name), } -} \ No newline at end of file +} diff --git a/go/genkit/servers.go b/go/genkit/servers.go index 5611ee75f..ca795bca4 100644 --- a/go/genkit/servers.go +++ b/go/genkit/servers.go @@ -158,7 +158,7 @@ func findProjectRoot() (string, error) { // // To construct a server with additional routes, use [NewFlowServeMux]. func startFlowServer(addr string, flows []string, errCh chan<- error) *http.Server { - slog.Info("starting flow server") + slog.Debug("starting flow server") addr = serverAddress(addr, "PORT", "127.0.0.1:3400") mux := NewFlowServeMux(flows) return startServer(addr, mux, errCh) @@ -182,7 +182,7 @@ func startServer(addr string, handler http.Handler, errCh chan<- error) *http.Se } go func() { - slog.Info("server listening", "addr", addr) + slog.Debug("server listening", "addr", addr) if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { errCh <- fmt.Errorf("server error on %s: %w", addr, err) } @@ -205,7 +205,7 @@ func shutdownServers(servers []*http.Server) error { if err := srv.Shutdown(ctx); err != nil { slog.Error("server shutdown failed", "addr", srv.Addr, "err", err) } else { - slog.Info("server shutdown successfully", "addr", srv.Addr) + slog.Debug("server shutdown successfully", "addr", srv.Addr) } }(server) } diff --git a/go/internal/registry/registry.go b/go/internal/registry/registry.go index c57fb7331..841a42277 100644 --- a/go/internal/registry/registry.go +++ b/go/internal/registry/registry.go @@ -101,7 +101,7 @@ func (r *Registry) RegisterAction(typ atype.ActionType, a action.Action) { } a.SetTracingState(r.tstate) r.actions[key] = a - slog.Info("RegisterAction", + slog.Debug("RegisterAction", "type", typ, "name", a.Name()) } diff --git a/go/plugins/googlecloud/googlecloud_test.go b/go/plugins/googlecloud/googlecloud_test.go index 111aa7c7d..22a46b28b 100644 --- a/go/plugins/googlecloud/googlecloud_test.go +++ b/go/plugins/googlecloud/googlecloud_test.go @@ -74,7 +74,7 @@ func TestGCP(t *testing.T) { if err := setLogHandler(*projectID, slog.LevelInfo); err != nil { t.Fatal(err) } - slog.Info("testing GCP logging", + slog.Debug("testing GCP logging", "binaryName", os.Args[0], "goVersion", runtime.Version()) // Allow time to export. From 12c99acfaeeeb60c433ee407d1f0799375a10800 Mon Sep 17 00:00:00 2001 From: fernandoalonso Date: Tue, 12 Nov 2024 21:17:57 +0000 Subject: [PATCH 4/5] add level filter logger to another file --- go/core/logger/level_filter.go | 50 ++++++++++++++++++++++++++++++++++ go/core/logger/logger.go | 29 -------------------- 2 files changed, 50 insertions(+), 29 deletions(-) create mode 100644 go/core/logger/level_filter.go diff --git a/go/core/logger/level_filter.go b/go/core/logger/level_filter.go new file mode 100644 index 000000000..20f8d6fea --- /dev/null +++ b/go/core/logger/level_filter.go @@ -0,0 +1,50 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Package logger provides a context-scoped slog.Logger. +package logger + +import ( + "context" + "log/slog" +) + +// LevelFilterHandler is a custom handler that only logs DEBUG messages. +type LevelFilterHandler struct { + level slog.Level + h slog.Handler +} + +func (h *LevelFilterHandler) Enabled(ctx context.Context, level slog.Level) bool { + // Display the message if its level is greater than or equal to the configured level + return level >= h.level +} + +func (h *LevelFilterHandler) Handle(ctx context.Context, r slog.Record) error { + return h.h.Handle(ctx, r) +} + +func (h *LevelFilterHandler) WithAttrs(attrs []slog.Attr) slog.Handler { + return &LevelFilterHandler{ + level: h.level, + h: h.h.WithAttrs(attrs), + } +} + +func (h *LevelFilterHandler) WithGroup(name string) slog.Handler { + return &LevelFilterHandler{ + level: h.level, + h: h.h.WithGroup(name), + } +} diff --git a/go/core/logger/logger.go b/go/core/logger/logger.go index 67a21ffdd..de396c97c 100644 --- a/go/core/logger/logger.go +++ b/go/core/logger/logger.go @@ -44,32 +44,3 @@ func FromContext(ctx context.Context) *slog.Logger { } return slog.Default() } - -// LevelFilterHandler is a custom handler that only logs DEBUG messages. -type LevelFilterHandler struct { - level slog.Level - h slog.Handler -} - -func (h *LevelFilterHandler) Enabled(ctx context.Context, level slog.Level) bool { - // Display the message if its level is greater than or equal to the configured level - return level >= h.level -} - -func (h *LevelFilterHandler) Handle(ctx context.Context, r slog.Record) error { - return h.h.Handle(ctx, r) -} - -func (h *LevelFilterHandler) WithAttrs(attrs []slog.Attr) slog.Handler { - return &LevelFilterHandler{ - level: h.level, - h: h.h.WithAttrs(attrs), - } -} - -func (h *LevelFilterHandler) WithGroup(name string) slog.Handler { - return &LevelFilterHandler{ - level: h.level, - h: h.h.WithGroup(name), - } -} From 1857ae897e115f804db30c3428dd2034611b7a4c Mon Sep 17 00:00:00 2001 From: fernandoalonso Date: Thu, 14 Nov 2024 03:47:19 +0000 Subject: [PATCH 5/5] resolve TODO: the main program should be responsable of the level log --- go/core/logger/logger.go | 12 ------------ go/genkit/genkit.go | 13 +++++++++++++ go/internal/doc-snippets/flows.go | 3 ++- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/go/core/logger/logger.go b/go/core/logger/logger.go index de396c97c..6d7d83cd7 100644 --- a/go/core/logger/logger.go +++ b/go/core/logger/logger.go @@ -18,22 +18,10 @@ package logger import ( "context" "log/slog" - "os" "github.com/firebase/genkit/go/internal/base" ) -func init() { - // TODO: Remove this. The main program should be responsible for configuring logging. - // This is just a convenience during development. - baseHandler := slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{}) - debugHandler := &LevelFilterHandler{ - h: baseHandler, - level: slog.LevelDebug, - } - slog.SetDefault(slog.New(debugHandler)) -} - var loggerKey = base.NewContextKey[*slog.Logger]() // FromContext returns the Logger in ctx, or the default Logger diff --git a/go/genkit/genkit.go b/go/genkit/genkit.go index d2d75cd3c..26ffb3a9b 100644 --- a/go/genkit/genkit.go +++ b/go/genkit/genkit.go @@ -37,6 +37,9 @@ type Options struct { // The names of flows to serve. // If empty, all registered flows are served. Flows []string + // LogLevel sets the logging level for the application. + // If empty, defaults to slog.LevelInfo. + LogLevel slog.Level } // Init initializes Genkit. @@ -59,6 +62,16 @@ func Init(ctx context.Context, opts *Options) error { if opts == nil { opts = &Options{} } + // Set default log level if not provided + if opts.LogLevel == 0 { + opts.LogLevel = slog.LevelInfo + } + + // Configure the logger + h := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{ + Level: opts.LogLevel, + })) + slog.SetDefault(h) registry.Global.Freeze() var mu sync.Mutex diff --git a/go/internal/doc-snippets/flows.go b/go/internal/doc-snippets/flows.go index b91f02e36..0a509b514 100644 --- a/go/internal/doc-snippets/flows.go +++ b/go/internal/doc-snippets/flows.go @@ -19,6 +19,7 @@ import ( "errors" "fmt" "log" + "log/slog" "net/http" "strings" @@ -168,7 +169,7 @@ func f4() { func deploy(ctx context.Context) { // [START init] if err := genkit.Init(ctx, - &genkit.Options{FlowAddr: ":3400"}, // Add this parameter. + &genkit.Options{FlowAddr: ":3400", LogLevel: slog.LevelDebug}, // Add this parameter. ); err != nil { log.Fatal(err) }