ssm-browse: added structed log view
This commit is contained in:
parent
9752bb41bc
commit
b3d0fbfe29
11 changed files with 546 additions and 0 deletions
44
internal/slog-view/services/logreader/logreader.go
Normal file
44
internal/slog-view/services/logreader/logreader.go
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
package logreader
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"github.com/lmika/awstools/internal/slog-view/models"
|
||||
"github.com/pkg/errors"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
}
|
||||
|
||||
func NewService() *Service {
|
||||
return &Service{}
|
||||
}
|
||||
|
||||
func (s *Service) Open(filename string) (*models.LogFile, error) {
|
||||
f, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "cannot open file: %v", filename)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var lines []models.LogLine
|
||||
scanner := bufio.NewScanner(f)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
|
||||
var data interface{}
|
||||
if err := json.Unmarshal([]byte(line), &data); err != nil {
|
||||
log.Println("invalid json line: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
lines = append(lines, models.LogLine{JSON: data})
|
||||
}
|
||||
if scanner.Err() != nil {
|
||||
return nil, errors.Wrapf(err, "unable to scan file: %v", filename)
|
||||
}
|
||||
|
||||
return &models.LogFile{Lines: lines}, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue