GODT-175: Add option to attach logs for bug reports

This commit is contained in:
Alexander Bilyak
2021-10-11 13:24:41 +00:00
committed by Jakub
parent b259de238e
commit e49d2e1be7
11 changed files with 248 additions and 38 deletions

View File

@ -26,7 +26,7 @@ import (
"github.com/sirupsen/logrus"
)
func clearLogs(logDir string, maxLogs int) error {
func clearLogs(logDir string, maxLogs int, maxCrashes int) error {
files, err := ioutil.ReadDir(logDir)
if err != nil {
return err
@ -36,8 +36,8 @@ func clearLogs(logDir string, maxLogs int) error {
var crashesWithPrefix []string
for _, file := range files {
if matchLogName(file.Name()) {
if matchStackTraceName(file.Name()) {
if MatchLogName(file.Name()) {
if MatchStackTraceName(file.Name()) {
crashesWithPrefix = append(crashesWithPrefix, file.Name())
} else {
logsWithPrefix = append(logsWithPrefix, file.Name())
@ -46,7 +46,7 @@ func clearLogs(logDir string, maxLogs int) error {
// Older versions of Bridge stored logs in subfolders for each version.
// That also has to be cleared and the functionality can be removed after some time.
if file.IsDir() {
if err := clearLogs(filepath.Join(logDir, file.Name()), maxLogs); err != nil {
if err := clearLogs(filepath.Join(logDir, file.Name()), maxLogs, maxCrashes); err != nil {
return err
}
} else {
@ -56,7 +56,7 @@ func clearLogs(logDir string, maxLogs int) error {
}
removeOldLogs(logDir, logsWithPrefix, maxLogs)
removeOldLogs(logDir, crashesWithPrefix, maxLogs)
removeOldLogs(logDir, crashesWithPrefix, maxCrashes)
return nil
}
@ -76,10 +76,10 @@ func removeOldLogs(logDir string, filenames []string, maxLogs int) {
func removeLog(logDir, filename string) {
// We need to be sure to delete only log files.
// Directory with logs can also contain other files.
if !matchLogName(filename) {
if !MatchLogName(filename) {
return
}
if err := os.RemoveAll(filepath.Join(logDir, filename)); err != nil {
logrus.WithError(err).Error("Failed to remove old logs")
if err := os.Remove(filepath.Join(logDir, filename)); err != nil {
logrus.WithError(err).Error("Failed to remove", filepath.Join(logDir, filename))
}
}

View File

@ -57,6 +57,6 @@ func getStackTraceName(version, revision string) string {
return fmt.Sprintf("v%v_%v_crash_%v.log", version, revision, time.Now().Unix())
}
func matchStackTraceName(name string) bool {
func MatchStackTraceName(name string) bool {
return regexp.MustCompile(`^v.*_crash_.*\.log$`).MatchString(name)
}

View File

@ -31,12 +31,17 @@ import (
)
const (
// MaxLogSize defines the maximum log size we should permit.
// Zendesk has a file size limit of 20MB. When the last N log files are zipped,
// it should fit under 20MB. So here we permit up to 10MB (most files are a few hundred kB).
MaxLogSize = 10 * 2 << 20
// MaxLogSize defines the maximum log size we should permit: 5 MB
//
// The Zendesk limit for an attachement is 50MB and this is what will
// be allowed via the API. However, if that fails for some reason, the
// fallback is sending the report via email, which has a limit of 10mb
// total or 7MB per file. Since we can produce up to 6 logs, and we
// compress all the files (avarage compression - 80%), we need to have
// a limit of 30MB total before compression, hence 5MB per log file.
MaxLogSize = 5 * 1024 * 1024
// MaxLogs defines how many old log files should be kept.
// MaxLogs defines how many log files should be kept.
MaxLogs = 3
)
@ -56,7 +61,8 @@ func Init(logsPath string) error {
})
rotator, err := NewRotator(MaxLogSize, func() (io.WriteCloser, error) {
if err := clearLogs(logsPath, MaxLogs); err != nil {
// Leaving MaxLogs-1 since new log file will be opened right away.
if err := clearLogs(logsPath, MaxLogs-1, MaxLogs); err != nil {
return nil, err
}
@ -91,6 +97,6 @@ func getLogName(version, revision string) string {
return fmt.Sprintf("v%v_%v_%v.log", version, revision, time.Now().Unix())
}
func matchLogName(name string) bool {
func MatchLogName(name string) bool {
return regexp.MustCompile(`^v.*\.log$`).MatchString(name)
}

View File

@ -36,7 +36,7 @@ func TestClearLogs(t *testing.T) {
require.NoError(t, ioutil.WriteFile(filepath.Join(dir, "v2_12.log"), []byte("Hello"), 0755))
require.NoError(t, ioutil.WriteFile(filepath.Join(dir, "v2_13.log"), []byte("Hello"), 0755))
require.NoError(t, clearLogs(dir, 3))
require.NoError(t, clearLogs(dir, 3, 0))
checkFileNames(t, dir, []string{
"other.log",
"v1_11.log",