Merge pull request #195 from hantmac/master

bug fix:fix read log file oom
This commit is contained in:
Marvin Zhang
2019-09-01 11:09:02 +08:00
committed by GitHub
2 changed files with 34 additions and 2 deletions

View File

@@ -34,13 +34,17 @@ func GetLocalLog(logPath string) (fileBytes []byte, err error) {
return nil, err
}
defer f.Close()
logBuf := make([]byte, 2048)
const bufLen = 2048
logBuf := make([]byte, bufLen)
off := int64(0)
if fi.Size() > int64(len(logBuf)) {
off = fi.Size() - int64(len(logBuf))
}
n, err := f.ReadAt(logBuf, off)
// 到文件结尾会有EOF的报错
//到文件结尾会有EOF标识
if err != nil && err.Error() != "EOF" {
log.Error(err.Error())
debug.PrintStack()

View File

@@ -2,9 +2,11 @@ package services
import (
"crawlab/config"
"fmt"
"github.com/apex/log"
. "github.com/smartystreets/goconvey/convey"
"github.com/spf13/viper"
"os"
"testing"
)
@@ -20,3 +22,29 @@ func TestDeleteLogPeriodically(t *testing.T) {
DeleteLogPeriodically()
})
}
func TestGetLocalLog(t *testing.T) {
//create a log file for test
logPath := "../logs/crawlab/test.log"
f, err := os.Create(logPath)
defer f.Close()
if err != nil {
fmt.Println(err.Error())
} else {
_, err = f.Write([]byte("This is for test"))
}
Convey("Test GetLocalLog", t, func() {
Convey("Test response", func() {
logStr, err := GetLocalLog(logPath)
log.Info(string(logStr))
fmt.Println(err)
So(err, ShouldEqual, nil)
})
})
//delete the test log file
os.Remove(logPath)
}