github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/kubernetes/status/resource/logfile.go (about)

     1  /*
     2  Copyright 2020 The Skaffold Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package resource
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"io"
    23  
    24  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/logfile"
    25  )
    26  
    27  // withLogFile returns a multiwriter that writes both to a file and a buffer, with the buffer being written to the provided output buffer in case of error
    28  func withLogFile(container string, out io.Writer, l []string, muted bool) (io.Writer, func([]string), error) {
    29  	if !muted || len(l) <= maxLogLines {
    30  		return out, func([]string) {}, nil
    31  	}
    32  	file, err := logfile.Create("statuscheck", container+".log")
    33  	if err != nil {
    34  		return out, func([]string) {}, fmt.Errorf("unable to create log file for statuscheck step: %w", err)
    35  	}
    36  
    37  	// Print logs to a memory buffer and to a file.
    38  	var buf bytes.Buffer
    39  	w := io.MultiWriter(file, &buf)
    40  
    41  	// After the status check updates finishes, close the log file.
    42  	return w, func(lines []string) {
    43  		file.Close()
    44  		// Write last few lines to out
    45  		for _, l := range lines {
    46  			out.Write([]byte(l))
    47  		}
    48  		fmt.Fprintf(out, "%s %s Full logs at %s\n", tab, tab, file.Name())
    49  	}, err
    50  }