Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1174 set log level to debug in golang #1236

Open
wants to merge 6 commits into
base: next
Choose a base branch
from
Open
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
50 changes: 50 additions & 0 deletions go/core/logger/level_filter.go
Original file line number Diff line number Diff line change
@@ -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),
}
}
10 changes: 0 additions & 10 deletions go/core/logger/logger.go
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the changes in this file?

Copy link
Contributor Author

@alonsopec89 alonsopec89 Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just moved it to a new file

Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +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.
h := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: slog.LevelDebug,
}))
slog.SetDefault(h)
}

var loggerKey = base.NewContextKey[*slog.Logger]()

// FromContext returns the Logger in ctx, or the default Logger
Expand Down
13 changes: 13 additions & 0 deletions go/genkit/genkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions go/genkit/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,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)
Expand All @@ -181,7 +181,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)
}
Expand All @@ -204,7 +204,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)
}
Expand Down
3 changes: 2 additions & 1 deletion go/internal/doc-snippets/flows.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"errors"
"fmt"
"log"
"log/slog"
"net/http"
"strings"

Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion go/internal/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,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())
}
Expand Down
2 changes: 1 addition & 1 deletion go/plugins/googlecloud/googlecloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading