Skip to content

Commit

Permalink
tail: add more log types and support pipe to stdout/stderr
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Winther <[email protected]>
  • Loading branch information
jippi committed Nov 19, 2018
1 parent 45b5aba commit a3065a9
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 17 deletions.
11 changes: 4 additions & 7 deletions command/tail/color_log_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package tail

import (
"bytes"
"fmt"
"io"
"strings"
"os"

"github.com/alecthomas/chroma"
"github.com/alecthomas/chroma/formatters"
"github.com/alecthomas/chroma/lexers"
"github.com/alecthomas/chroma/styles"
log "github.com/sirupsen/logrus"
)

type colorLogWriter struct {
Expand All @@ -18,7 +18,6 @@ type colorLogWriter struct {

func (w colorLogWriter) Write(p []byte) (n int, err error) {
s := string(p)
s = strings.Trim(s, "\n")

if string(s[0]) == "{" {
buffer := bytes.NewBufferString("")
Expand All @@ -27,11 +26,9 @@ func (w colorLogWriter) Write(p []byte) (n int, err error) {
}

if w.Type == "stdout" {
log.Info(s)
} else if w.Type == "stderr" {
log.Error(s)
fmt.Fprintf(os.Stdout, s)
} else {
log.Warn(s)
fmt.Fprintf(os.Stderr, s)
}

return len(p), nil
Expand Down
22 changes: 22 additions & 0 deletions command/tail/raw_log_writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package tail

import (
"fmt"
"os"
)

type rawLogWriter struct {
Type string
}

func (w rawLogWriter) Write(p []byte) (n int, err error) {
s := string(p)

if w.Type == "stdout" {
fmt.Fprintf(os.Stdout, s)
} else {
fmt.Fprintf(os.Stderr, s)
}

return len(p), nil
}
10 changes: 4 additions & 6 deletions command/tail/simple_log_writer.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package tail

import (
"fmt"
"os"
"strings"

log "github.com/sirupsen/logrus"
)

type simpleLogWriter struct {
Expand All @@ -15,11 +15,9 @@ func (w simpleLogWriter) Write(p []byte) (n int, err error) {
s = strings.Trim(s, "\n")

if w.Type == "stdout" {
log.Info(s)
} else if w.Type == "stderr" {
log.Error(s)
fmt.Fprintf(os.Stdout, s)
} else {
log.Warn(s)
fmt.Fprintf(os.Stderr, s)
}

return len(p), nil
Expand Down
21 changes: 17 additions & 4 deletions command/tail/tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ func Run(c *cli.Context) error {
if c.BoolT("stdout") {
wg.Add(1)
logger := log.WithField("log_type", "stdout")
go Tail("stdout", taskName, alloc, nomadClient, &wg, logger)
go Tail(getWriter("stdout", c.String("writer")), "stderr", taskName, alloc, nomadClient, &wg, logger)
}

if c.BoolT("stderr") {
wg.Add(1)
logger := log.WithField("log_type", "stderr")
go Tail("stderr", taskName, alloc, nomadClient, &wg, logger)
go Tail(getWriter("stderr", c.String("writer")), "stdout", taskName, alloc, nomadClient, &wg, logger)
}

go func() {
Expand All @@ -76,7 +76,20 @@ func Run(c *cli.Context) error {
return nil
}

func Tail(logType, task string, alloc *api.Allocation, client *api.Client, wg *sync.WaitGroup, logger *log.Entry) {
func getWriter(target, kind string) io.Writer {
switch kind {
case "color":
return colorLogWriter{Type: target}
case "simple":
return simpleLogWriter{Type: target}
case "raw":
return rawLogWriter{Type: target}
default:
panic("Invalid log ")
}
}

func Tail(wr io.Writer, logType, task string, alloc *api.Allocation, client *api.Client, wg *sync.WaitGroup, logger *log.Entry) {
var err error
var r io.ReadCloser
var readErr error
Expand All @@ -97,7 +110,7 @@ func Tail(logType, task string, alloc *api.Allocation, client *api.Client, wg *s
}

defer r.Close()
_, err = io.Copy(colorLogWriter{Type: logType}, r)
_, err = io.Copy(wr, r)
if err != nil {
logger.Error(fmt.Sprintf("error following logs: %s", err))
return
Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ func main() {
Name: "stdout",
Usage: "(optional, default: true) tail stdout from nomad",
},
cli.StringFlag{
Name: "writer",
Value: "color",
Usage: "(optional, default: color) writer type (raw, color, simple)",
},
},
Action: func(c *cli.Context) error {
if err := tail.Run(c); err != nil {
Expand Down

0 comments on commit a3065a9

Please sign in to comment.