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

Revert "fix: remove obsolete blank prints (GoogleCloudPlatform#144)" #157

Merged
merged 2 commits into from
Aug 23, 2022
Merged
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
18 changes: 18 additions & 0 deletions funcframework/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ func wrapFunction(fn *registry.RegisteredFunction) (http.Handler, error) {

func wrapHTTPFunction(fn func(http.ResponseWriter, *http.Request)) (http.Handler, error) {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if os.Getenv("K_SERVICE") != "" {
// Force flush of logs after every function trigger when running on GCF.
defer fmt.Println()
defer fmt.Fprintln(os.Stderr)
}
defer recoverPanic(w, "user function execution")
fn(w, r)
}), nil
Expand All @@ -185,6 +190,12 @@ func wrapEventFunction(fn interface{}) (http.Handler, error) {
return nil, err
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if os.Getenv("K_SERVICE") != "" {
// Force flush of logs after every function trigger when running on GCF.
defer fmt.Println()
defer fmt.Fprintln(os.Stderr)
}

if shouldConvertCloudEventToBackgroundRequest(r) {
if err := convertCloudEventToBackgroundRequest(r); err != nil {
writeHTTPErrorResponse(w, http.StatusBadRequest, crashStatus, fmt.Sprintf("error converting CloudEvent to Background Event: %v", err))
Expand Down Expand Up @@ -289,6 +300,13 @@ func writeHTTPErrorResponse(w http.ResponseWriter, statusCode int, status, msg s
}
fmt.Fprint(os.Stderr, msg)

// Flush stdout and stderr when running on GCF. This must be done before writing
// the HTTP response in order for all logs to appear in GCF.
if os.Getenv("K_SERVICE") != "" {
fmt.Println()
fmt.Fprintln(os.Stderr)
}

w.Header().Set(functionStatusHeader, status)
w.WriteHeader(statusCode)
fmt.Fprint(w, msg)
Expand Down