in internal/log/log.go [79:132]
func ResetLogFiles(paths []string, fileSizeMaxMB int, fileSizeKeepKB int, logger *Logger) {
var fileSize int
for _, path := range paths {
switch path {
case "stdout":
fallthrough
case "stderr":
continue
}
file, err := os.Open(path)
if err != nil {
logger.Error("failed to open existing log file",
zap.String("file", path),
zap.Error(err))
continue
}
// Get the file size
stat, err := file.Stat()
if err != nil {
logger.Error("failed to read the FileInfo structure of the log file",
zap.String("file", path),
zap.Error(err))
goto close
}
fileSize = int(stat.Size())
if fileSize > fileSizeMaxMB*1024*1024 {
logger.Debug("Size of log file is larger than maximum allowed. Resetting.",
zap.String("file", path),
zap.Int("current_size_MB", fileSize),
zap.Int("maximum_allowed_size_MB", fileSizeMaxMB))
buf := make([]byte, fileSizeKeepKB*1024)
start := stat.Size() - int64(fileSizeKeepKB*1024)
if _, err = file.ReadAt(buf, start); err != nil {
logger.Error("failed to read existing log file",
zap.String("file", path),
zap.Error(err))
} else if err = ioutil.WriteFile(path, buf, 0644); err != nil {
logger.Error("failed to reset log file",
zap.String("file", path),
zap.Error(err))
}
}
// Avoid possible leaks because of using `defer`
close:
file.Close()
}
}