Skip to content

Commit

Permalink
Add prettylog time format (rs#645)
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno authored and mAdkins committed Mar 2, 2024
1 parent b07abc7 commit 7850fe2
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions cmd/prettylog/prettylog.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package main
import (
"bufio"
"errors"
"flag"
"fmt"
"io"
"os"
"time"

"github.com/rs/zerolog"
)
Expand All @@ -15,9 +17,7 @@ func isInputFromPipe() bool {
return fileInfo.Mode()&os.ModeCharDevice == 0
}

func processInput(reader io.Reader) error {
writer := zerolog.NewConsoleWriter()

func processInput(reader io.Reader, writer io.Writer) error {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
bytesToWrite := scanner.Bytes()
Expand All @@ -35,19 +35,39 @@ func processInput(reader io.Reader) error {
}

func main() {
timeFormats := map[string]string{
"default": time.Kitchen,
"full": time.RFC1123,
}

timeFormatFlag := flag.String(
"time-format",
"default",
"Time format, either 'default' or 'full'",
)

flag.Parse()

timeFormat, ok := timeFormats[*timeFormatFlag]
if !ok {
panic("Invalid time-format provided")
}

writer := zerolog.NewConsoleWriter()
writer.TimeFormat = timeFormat

if isInputFromPipe() {
_ = processInput(os.Stdin)
} else if len(os.Args) > 1 {
for _, filename := range os.Args[1:] {
_ = processInput(os.Stdin, writer)
} else if flag.NArg() >= 1 {
for _, filename := range flag.Args() {
// Scan each line from filename and write it into writer
reader, err := os.Open(filename)
if err != nil {
fmt.Printf("%s open: %v", filename, err)
os.Exit(1)
}

if err := processInput(reader); err != nil {
if err := processInput(reader, writer); err != nil {
fmt.Printf("%s scan: %v", filename, err)
os.Exit(1)
}
Expand Down

0 comments on commit 7850fe2

Please sign in to comment.