Skip to content

Commit 55af7c4

Browse files
authored
Merge pull request #49 from dispatchrun/logging-improvements
run: improve log output
2 parents 6004058 + 4f6146a commit 55af7c4

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

cli/log.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import (
1212
)
1313

1414
var (
15-
logTimeStyle = lipgloss.NewStyle().Foreground(grayColor)
16-
logAttrStyle = lipgloss.NewStyle().Foreground(grayColor)
15+
logTimeStyle = lipgloss.NewStyle().Foreground(grayColor)
16+
logAttrKeyStyle = lipgloss.NewStyle().Foreground(grayColor)
17+
logAttrValStyle = lipgloss.NewStyle().Foreground(defaultColor)
1718

1819
logDebugStyle = lipgloss.NewStyle().Foreground(defaultColor)
1920
logInfoStyle = lipgloss.NewStyle().Foreground(defaultColor)
@@ -40,18 +41,20 @@ func (h *slogHandler) Handle(ctx context.Context, record slog.Record) error {
4041

4142
var b bytes.Buffer
4243
b.WriteString(logTimeStyle.Render(record.Time.Format("2006-01-02 15:04:05.000")))
43-
b.WriteByte(' ')
44-
b.WriteString(levelString(record.Level))
44+
if record.Level >= slog.LevelWarn {
45+
b.WriteByte(' ')
46+
b.WriteString(levelString(record.Level))
47+
}
4548
b.WriteByte(' ')
4649
b.WriteString(record.Message)
4750
record.Attrs(func(attr slog.Attr) bool {
4851
b.WriteByte(' ')
49-
b.WriteString(logAttrStyle.Render(attr.String()))
52+
writeAttr(&b, attr)
5053
return true
5154
})
5255
for _, attr := range h.attrs {
5356
b.WriteByte(' ')
54-
b.WriteString(logAttrStyle.Render(attr.String()))
57+
writeAttr(&b, attr)
5558
}
5659
b.WriteByte('\n')
5760

@@ -74,6 +77,11 @@ func levelString(level slog.Level) string {
7477
}
7578
}
7679

80+
func writeAttr(b *bytes.Buffer, attr slog.Attr) {
81+
b.WriteString(logAttrKeyStyle.Render(attr.Key + "="))
82+
b.WriteString(logAttrValStyle.Render(attr.Value.String()))
83+
}
84+
7785
func (h *slogHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
7886
parent := h
7987
if parent.parent != nil {

cli/run.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,13 @@ func invoke(ctx context.Context, client *http.Client, url, requestID string, bri
439439
return fmt.Errorf("invalid response from Dispatch API: %v", err)
440440
}
441441
logger.Debug("parsed request", "function", runRequest.Function, "dispatch_id", runRequest.DispatchId)
442-
switch runRequest.Directive.(type) {
442+
switch d := runRequest.Directive.(type) {
443443
case *sdkv1.RunRequest_Input:
444-
logger.Info("calling function", "function", runRequest.Function)
444+
if Verbose {
445+
logger.Info("calling function", "function", runRequest.Function, "input", anyString(d.Input))
446+
} else {
447+
logger.Info("calling function", "function", runRequest.Function)
448+
}
445449
case *sdkv1.RunRequest_PollResult:
446450
logger.Info("resuming function", "function", runRequest.Function)
447451
}
@@ -497,11 +501,13 @@ func invoke(ctx context.Context, client *http.Client, url, requestID string, bri
497501
case *sdkv1.RunResponse_Exit:
498502
if d.Exit.TailCall != nil {
499503
logger.Info("function tail-called", "function", runRequest.Function, "tail_call", d.Exit.TailCall.Function)
504+
} else if Verbose && d.Exit.Result != nil {
505+
logger.Info("function call succeeded", "function", runRequest.Function, "output", anyString(d.Exit.Result.Output))
500506
} else {
501507
logger.Info("function call succeeded", "function", runRequest.Function)
502508
}
503509
case *sdkv1.RunResponse_Poll:
504-
logger.Info("function yielded", "function", runRequest.Function, "calls", len(d.Poll.Calls))
510+
logger.Info("function yielded", "function", runRequest.Function)
505511
}
506512
default:
507513
err := runResponse.GetExit().GetResult().GetError()

0 commit comments

Comments
 (0)