[MM-14890] Logs endpoint always returns an empty line (#10559)
* fixed new line being returned in logs, improved the test * improved the EOL check * Fixed var shadowning
Этот коммит содержится в:
коммит произвёл
Miguel de la Cruz
родитель
84a59ddb39
Коммит
6cc36ab176
@@ -198,6 +198,9 @@ func TestGetLogs(t *testing.T) {
|
||||
t.Log(len(logs))
|
||||
t.Fatal("wrong length")
|
||||
}
|
||||
for i := 10; i < 20; i++ {
|
||||
assert.Containsf(t, logs[i-10], fmt.Sprintf(`"msg":"%d"`, i), "Log line doesn't contain correct message")
|
||||
}
|
||||
|
||||
logs, resp = th.SystemAdminClient.GetLogs(1, 10)
|
||||
CheckNoError(t, resp)
|
||||
|
||||
16
app/admin.go
16
app/admin.go
@@ -51,7 +51,8 @@ func (a *App) GetLogsSkipSend(page, perPage int) ([]string, *model.AppError) {
|
||||
var lines []string
|
||||
|
||||
if *a.Config().LogSettings.EnableFile {
|
||||
file, err := os.Open(utils.GetLogFileLocation(*a.Config().LogSettings.FileLocation))
|
||||
logFile := utils.GetLogFileLocation(*a.Config().LogSettings.FileLocation)
|
||||
file, err := os.Open(logFile)
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("getLogs", "api.admin.file_read_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
@@ -61,7 +62,17 @@ func (a *App) GetLogsSkipSend(page, perPage int) ([]string, *model.AppError) {
|
||||
var newLine = []byte{'\n'}
|
||||
var lineCount int
|
||||
const searchPos = -1
|
||||
lineEndPos, err := file.Seek(0, io.SeekEnd)
|
||||
b := make([]byte, 1)
|
||||
var endOffset int64 = 0
|
||||
|
||||
// if the file exists and it's last byte is '\n' - skip it
|
||||
var stat os.FileInfo
|
||||
if stat, err = os.Stat(logFile); err == nil {
|
||||
if _, err = file.ReadAt(b, stat.Size()-1); err == nil && b[0] == newLine[0] {
|
||||
endOffset = -1
|
||||
}
|
||||
}
|
||||
lineEndPos, err := file.Seek(endOffset, io.SeekEnd)
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("getLogs", "api.admin.file_read_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
@@ -71,7 +82,6 @@ func (a *App) GetLogsSkipSend(page, perPage int) ([]string, *model.AppError) {
|
||||
return nil, model.NewAppError("getLogs", "api.admin.file_read_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
b := make([]byte, 1)
|
||||
_, err = file.ReadAt(b, pos)
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("getLogs", "api.admin.file_read_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
|
||||
Ссылка в новой задаче
Block a user