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

Extract limited stack trace frames from error #310

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
21 changes: 19 additions & 2 deletions maintenance/terminationlog/termlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...))
Expand All @@ -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...))
Expand All @@ -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])
}
15 changes: 15 additions & 0 deletions maintenance/terminationlog/termlog_test.go
Original file line number Diff line number Diff line change
@@ -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)
}