Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
sst.aws.Function: fix log issue
Browse files Browse the repository at this point in the history
  • Loading branch information
thdxr committed Oct 14, 2024
1 parent 64f15ea commit 8944236
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions cmd/sst/mosaic/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -230,11 +231,13 @@ func Start(
evts := bus.Subscribe(&FunctionLogEvent{}, &FunctionInvokedEvent{}, &FunctionResponseEvent{}, &FunctionErrorEvent{}, &FunctionBuildEvent{})
logs := map[string]*os.File{}

getLog := func(functionID string) *os.File {
log, ok := logs[functionID]
getLog := func(functionID string, requestID string) *os.File {
log, ok := logs[requestID]
if !ok {
log, _ = os.Create(p.PathLog("lambda-" + functionID))
logs[functionID] = log
path := p.PathLog("lambda/" + functionID + "/" + requestID)
os.MkdirAll(filepath.Dir(path), 0755)
log, _ = os.Create(path)
logs[requestID] = log
}
return log
}
Expand All @@ -243,15 +246,21 @@ func Start(
for evt := range evts {
switch evt := evt.(type) {
case *FunctionInvokedEvent:
getLog(evt.FunctionID).WriteString("invocation " + evt.RequestID + "\n")
log := getLog(evt.FunctionID, evt.RequestID)
log.WriteString("invocation " + evt.RequestID + "\n")
log.WriteString(string(evt.Input))
log.WriteString("\n")
case *FunctionLogEvent:
getLog(evt.FunctionID).WriteString(evt.Line + "\n")
getLog(evt.FunctionID, evt.RequestID).WriteString(evt.Line + "\n")
case *FunctionResponseEvent:
getLog(evt.FunctionID).WriteString("response " + evt.RequestID + "\n")
log := getLog(evt.FunctionID, evt.RequestID)
log.WriteString("response " + evt.RequestID + "\n")
log.WriteString(string(evt.Output))
log.WriteString("\n")
delete(logs, evt.RequestID)
case *FunctionErrorEvent:
getLog(evt.FunctionID).WriteString(evt.ErrorType + ": " + evt.ErrorMessage + "\n")
case *FunctionBuildEvent:
getLog(evt.FunctionID).WriteString(evt.Errors[0] + "\n")
getLog(evt.FunctionID, evt.RequestID).WriteString(evt.ErrorType + ": " + evt.ErrorMessage + "\n")
delete(logs, evt.RequestID)
}
}
}
Expand Down

0 comments on commit 8944236

Please sign in to comment.