Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prettylog time format #645

Merged
merged 1 commit into from
Feb 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading