diff --git a/internal/logs/text/text.go b/internal/logs/text/text.go index 5c7caca0..d083b868 100644 --- a/internal/logs/text/text.go +++ b/internal/logs/text/text.go @@ -17,7 +17,6 @@ import ( // TODO: rename since it's specific to log querying ATM // TODO: output larger timestamp when older // TODO: option to output UTC -// TODO: option to output expanded fields // TODO: option to truncate // TODO: move to apex/log? @@ -61,16 +60,9 @@ type Handler struct { func New(w io.Writer) *Handler { return &Handler{ Writer: w, - layout: "3:04:05pm", } } -// WithFormat sets the date format. -func (h *Handler) WithFormat(s string) *Handler { - h.layout = s - return h -} - // WithExpandedFields sets the expanded field state. func (h *Handler) WithExpandedFields(v bool) *Handler { h.expand = v @@ -96,7 +88,7 @@ func (h *Handler) handleExpanded(e *log.Entry) error { h.mu.Lock() defer h.mu.Unlock() - ts := e.Timestamp.Local().Format(h.layout) + ts := formatDate(e.Timestamp.Local()) fmt.Fprintf(h.Writer, " %s %s %s\n", colors.Gray(ts), color(level), e.Message) for _, name := range names { @@ -125,7 +117,7 @@ func (h *Handler) handleInline(e *log.Entry) error { h.mu.Lock() defer h.mu.Unlock() - ts := e.Timestamp.Local().Format(h.layout) + ts := formatDate(e.Timestamp.Local()) fmt.Fprintf(h.Writer, " %s %s %s", colors.Gray(ts), color(level), e.Message) for _, name := range names { @@ -158,3 +150,32 @@ func value(name string, v interface{}) interface{} { return v } } + +// day duration. +var day = time.Hour * 24 + +// formatDate formats t relative to now. +func formatDate(t time.Time) string { + switch d := time.Now().Sub(t); { + case d >= day*7: + return t.Format(`Jan 2` + dateSuffix(t) + ` 3:04:05pm`) + case d >= day: + return t.Format(`2` + dateSuffix(t) + ` 3:04:05pm`) + default: + return t.Format(`3:04:05pm`) + } +} + +// dateSuffix returns the date suffix for t. +func dateSuffix(t time.Time) string { + switch t.Day() { + case 1, 21, 31: + return "st" + case 2, 22: + return "nd" + case 3, 23: + return "rd" + default: + return "th" + } +} diff --git a/platform/lambda/logs.go b/platform/lambda/logs.go index e0cc384a..c3ddd564 100644 --- a/platform/lambda/logs.go +++ b/platform/lambda/logs.go @@ -2,6 +2,7 @@ package lambda import ( "encoding/json" + "fmt" "io" "os" "strings" @@ -15,6 +16,7 @@ import ( "github.com/aws/aws-sdk-go/service/cloudwatchlogs" "github.com/tj/go/term" + "github.com/apex/up/internal/colors" "github.com/apex/up/internal/logs/parser" "github.com/apex/up/internal/logs/text" "github.com/apex/up/internal/util" @@ -123,11 +125,8 @@ func (l *Logs) start() { continue } - // plain text log - handler.HandleLog(&log.Entry{ - Level: log.InfoLevel, - Message: strings.TrimRight(l.Message, " \n"), - }) + // lambda textual logs + fmt.Printf(" %s\n", colors.Gray(line)) } // TODO: refactor interface to delegate