diff --git a/maintenance/terminationlog/termlog.go b/maintenance/terminationlog/termlog.go index 4924854c3..0a7d62811 100644 --- a/maintenance/terminationlog/termlog.go +++ b/maintenance/terminationlog/termlog.go @@ -9,16 +9,23 @@ package terminationlog import ( "fmt" + "github.com/pkg/errors" "github.com/rs/zerolog/log" "os" ) var logFile *os.File +const LogFileLimit = 4096 // bytes; + +type stackTracer interface { + StackTrace() errors.StackTrace +} + // Fatalf implements log Fatalf interface func Fatalf(format string, v ...interface{}) { if logFile != nil { - fmt.Fprintf(logFile, format, v...) + fmt.Fprint(logFile, limitLogFileOutput(fmt.Sprintf(format, v...))) } log.Fatal().Msg(fmt.Sprintf(format, v...)) @@ -27,7 +34,7 @@ func Fatalf(format string, v ...interface{}) { // Fatal implements log Fatal interface func Fatal(v ...interface{}) { if logFile != nil { - fmt.Fprint(logFile, v...) + fmt.Fprint(logFile, limitLogFileOutput(fmt.Sprint(v...))) } log.Fatal().Msg(fmt.Sprint(v...)) @@ -37,3 +44,13 @@ func Fatal(v ...interface{}) { func Fatalln(v ...interface{}) { Fatal(v...) } + +func limitLogFileOutput(s string) string { + sb := []byte(s) + limit := len(sb) + if limit > LogFileLimit { + limit = LogFileLimit + } + + return string(sb[:limit]) +} diff --git a/maintenance/terminationlog/termlog_test.go b/maintenance/terminationlog/termlog_test.go new file mode 100644 index 000000000..eaec1b0f6 --- /dev/null +++ b/maintenance/terminationlog/termlog_test.go @@ -0,0 +1,15 @@ +package terminationlog + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestTrimLogFileOutput(t *testing.T) { + b := make([]byte, 5000) + assert.Len(t, limitLogFileOutput(string(b)), 4096) + b = make([]byte, 2000) + assert.Len(t, limitLogFileOutput(string(b)), 2000) + b = make([]byte, 4096) + assert.Len(t, limitLogFileOutput(string(b)), 4096) +}