Skip to content

Commit

Permalink
Display date before logs message, closes #190
Browse files Browse the repository at this point in the history
  • Loading branch information
brmzkw committed Feb 29, 2024
1 parent 8a06bb7 commit 547594d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## v3.9.0 (unreleased)

* Display date in `koyeb service logs` and `koyeb instance logs`
- https://github.com/koyeb/koyeb-cli/issues/190

## v3.8.1

* Better help message for `koyeb cp`
Expand Down
26 changes: 21 additions & 5 deletions pkg/koyeb/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,26 @@ type LogLine struct {
}

type LogLineResult struct {
Msg string `json:"msg"`
CreatedAt string `json:"created_at"`
Msg string `json:"msg"`
}

// WatchLogsEntry is an entry returned by WatchLogsQuery.Execute()
type WatchLogsEntry struct {
Msg string
Err error
Date time.Time
Msg string
Err error
}

// ParseTime parses a time string contained in the field result.created_at of
// the endpoint /v1/streams/logs/tail. In case of error, a zero time is returned.
func (query *WatchLogsQuery) ParseTime(date string) time.Time {
layout := "2006-01-02T15:04:05.999999999Z"
parsed, err := time.Parse(layout, date)
if err != nil {
return time.Time{}
}
return parsed
}

func (query *WatchLogsQuery) Execute() (chan WatchLogsEntry, error) {
Expand Down Expand Up @@ -139,7 +152,7 @@ func (query *WatchLogsQuery) Execute() (chan WatchLogsEntry, error) {
Solution: "Try again in a few seconds",
}}
} else {
logs <- WatchLogsEntry{Msg: msg.Result.Msg}
logs <- WatchLogsEntry{Msg: msg.Result.Msg, Date: query.ParseTime(msg.Result.CreatedAt)}
}
}
}()
Expand Down Expand Up @@ -190,7 +203,10 @@ func (query *WatchLogsQuery) PrintAll() error {
if log.Err != nil {
return log.Err
}
fmt.Println(log.Msg)
layout := "2006-01-02 15:04:05"
date := log.Date.Format(layout)
zone, _ := log.Date.Zone()
fmt.Printf("[%s %s] %s\n", date, zone, log.Msg)
}
return nil
}

0 comments on commit 547594d

Please sign in to comment.