Skip to content

Commit

Permalink
Merge pull request #11 from pamburus/feature/time-format
Browse files Browse the repository at this point in the history
add time-format option
  • Loading branch information
aybabtme authored Jun 6, 2017
2 parents 65b9b2c + b5a2164 commit c4d8c0e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
9 changes: 8 additions & 1 deletion cmd/humanlog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,20 @@ func newApp() *cli.App {
Usage: "use black as the base foreground color (for terminals with light backgrounds)",
}

timeFormat := cli.StringFlag{
Name: "time-format",
Usage: "output time format, see https://golang.org/pkg/time/ for details",
Value: humanlog.DefaultOptions.TimeFormat,
}

app := cli.NewApp()
app.Author = "Antoine Grondin"
app.Email = "antoine@digitalocean.com"
app.Name = "humanlog"
app.Version = version
app.Usage = "reads structured logs from stdin, makes them pretty on stdout!"

app.Flags = []cli.Flag{skipFlag, keepFlag, sortLongest, skipUnchanged, truncates, truncateLength, lightBg}
app.Flags = []cli.Flag{skipFlag, keepFlag, sortLongest, skipUnchanged, truncates, truncateLength, lightBg, timeFormat}

app.Action = func(c *cli.Context) error {

Expand All @@ -92,6 +98,7 @@ func newApp() *cli.App {
opts.Truncates = c.BoolT(truncates.Name)
opts.TruncateLength = c.Int(truncateLength.Name)
opts.LightBg = c.BoolT(lightBg.Name)
opts.TimeFormat = c.String(timeFormat.Name)

switch {
case c.IsSet(skipFlag.Name) && c.IsSet(keepFlag.Name):
Expand Down
4 changes: 4 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package humanlog

import (
"time"

"github.com/kr/logfmt"
)

Expand All @@ -19,6 +21,7 @@ var DefaultOptions = &HandlerOptions{
TruncateLength: 15,
KeyRGB: RGB{1, 108, 89},
ValRGB: RGB{125, 125, 125},
TimeFormat: time.Stamp,
}

type RGB struct{ R, G, B uint8 }
Expand All @@ -35,6 +38,7 @@ type HandlerOptions struct {
TruncateLength int
KeyRGB RGB
ValRGB RGB
TimeFormat string
}

func (h *HandlerOptions) shouldShowKey(key string) bool {
Expand Down
2 changes: 1 addition & 1 deletion json_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (h *JSONHandler) Prettify(skipUnchanged bool) []byte {
}

_, _ = fmt.Fprintf(h.out, "%s |%s| %s\t %s",
rgbterm.FgString(h.Time.Format(time.Stamp), 99, 99, 99),
rgbterm.FgString(h.Time.Format(h.Opts.TimeFormat), 99, 99, 99),
level,
msg,
strings.Join(h.joinKVs(skipUnchanged, "="), "\t "),
Expand Down
2 changes: 1 addition & 1 deletion logrus_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (h *LogrusHandler) Prettify(skipUnchanged bool) []byte {
}

_, _ = fmt.Fprintf(h.out, "%s |%s| %s\t %s",
rgbterm.FgString(h.Time.Format(time.Stamp), 99, 99, 99),
rgbterm.FgString(h.Time.Format(h.Opts.TimeFormat), 99, 99, 99),
level,
msg,
strings.Join(h.joinKVs(skipUnchanged, "="), "\t "),
Expand Down

0 comments on commit c4d8c0e

Please sign in to comment.