Skip to content

Commit

Permalink
feat: expose FTL logger through the Go SDK (#824)
Browse files Browse the repository at this point in the history
@wesbillman can you let product eng know that a logger is now available
please
  • Loading branch information
alecthomas authored Jan 20, 2024
1 parent 11cf7e1 commit 0107cc8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"kind": "build",
"isDefault": true
},
"detail": "cd /Users/alec/dev/ftl; go build ./..."
"detail": "cd ${workspaceFolder}; go build ./..."
}
]
}
8 changes: 4 additions & 4 deletions backend/common/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ func New(level Level, sink Sink) *Logger {
}

func (l Logger) Scope(scope string) *Logger {
return l.Sub(map[string]string{scopeKey: scope})
return l.Attrs(map[string]string{scopeKey: scope})
}

// Sub creates a new logger with the given attributes.
func (l Logger) Sub(attributes map[string]string) *Logger {
attr := map[string]string{}
// Attrs creates a new logger with the given attributes.
func (l Logger) Attrs(attributes map[string]string) *Logger {
attr := make(map[string]string, len(l.attributes)+len(attributes))
maps.Copy(attr, l.attributes)
maps.Copy(attr, attributes)
l.attributes = attr
Expand Down
2 changes: 1 addition & 1 deletion backend/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ func (s *Service) getDeploymentLogger(ctx context.Context, deploymentName model.
attrs["request"] = requestName.String()
}

return log.FromContext(ctx).AddSink(s.deploymentLogsSink).Sub(attrs)
return log.FromContext(ctx).AddSink(s.deploymentLogsSink).Attrs(attrs)
}

// Periodically sync the routing table from the DB.
Expand Down
6 changes: 3 additions & 3 deletions backend/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func Start(ctx context.Context, config Config) error {
client := rpc.Dial(ftlv1connect.NewVerbServiceClient, config.ControllerEndpoint.String(), log.Error)
ctx = rpc.ContextWithClient(ctx, client)

logger := log.FromContext(ctx).Sub(map[string]string{"runner": config.Key.String()})
logger := log.FromContext(ctx).Attrs(map[string]string{"runner": config.Key.String()})
logger.Infof("Starting FTL Runner")
logger.Infof("Deployment directory: %s", config.DeploymentDir)
err = os.MkdirAll(config.DeploymentDir, 0700)
Expand Down Expand Up @@ -208,7 +208,7 @@ func (s *Service) Deploy(ctx context.Context, req *connect.Request[ftlv1.DeployR
return nil, fmt.Errorf("%s: %w", "failed to download artefacts", err)
}

verbCtx := log.ContextWithLogger(ctx, deploymentLogger.Sub(map[string]string{"module": module.Name}))
verbCtx := log.ContextWithLogger(ctx, deploymentLogger.Attrs(map[string]string{"module": module.Name}))
deployment, cmdCtx, err := plugin.Spawn(
unstoppable.Context(verbCtx),
log.Info,
Expand Down Expand Up @@ -385,5 +385,5 @@ func (s *Service) getDeploymentLogger(ctx context.Context, deploymentName model.
}

sink := newDeploymentLogsSink(s.deploymentLogQueue)
return log.FromContext(ctx).AddSink(sink).Sub(attrs)
return log.FromContext(ctx).AddSink(sink).Attrs(attrs)
}
16 changes: 16 additions & 0 deletions go-runtime/sdk/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package sdk

import (
"context"

"github.com/TBD54566975/ftl/backend/common/log"
)

// Logger is a levelled printf-style logger with support for structured
// attributes.
type Logger = log.Logger

// FromContext retrieves the current logger from the Context.
func FromContext(ctx context.Context) *Logger {
return log.FromContext(ctx)
}

0 comments on commit 0107cc8

Please sign in to comment.