Skip to content

Commit

Permalink
add date formatting for older logs
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Nov 25, 2017
1 parent 8f792f9 commit 4b2b961
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
41 changes: 31 additions & 10 deletions internal/logs/text/text.go
Expand Up @@ -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?

Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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"
}
}
9 changes: 4 additions & 5 deletions platform/lambda/logs.go
Expand Up @@ -2,6 +2,7 @@ package lambda

import (
"encoding/json"
"fmt"
"io"
"os"
"strings"
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4b2b961

Please sign in to comment.