Prefix Streaming stdout & stderr in Go

4 minute read

If you are writing code in Go and are executing a lot of (remote) commands, you may want to indent all of their output, prefix the loglines with hostnames, or mark anything that was thrown to stderr red, so you can spot errors more easily.

For this purpose I wrote Logstreamer.

You pass 3 arguments to NewLogstreamer():

  • Your *log.Logger
  • Your desired prefix ("stdout" and "stderr" prefixed have special meaning)
  • If the lines should be recorded true or false. This is useful if you want to retrieve any errors.

This returns an interface that you can point exec.Command's cmd.Stderr and cmd.Stdout to. All bytes that are written to it are split by newline and then prefixed to your specification.

Test

$ cd src/pkg/logstreamer/
$ go test

Here I issue two local commands, ls -al and ls nonexisting:

screen shot 2013-07-02 at 2 48 33 pm

Over at Transloadit we use it to prefix streaming remote command output. Servers stream command output over SSH back to me, and every line is prefixed with a date, their hostname & marked red in case they wrote to stderr. Makes it really easy to spot errors & their origin.

The project is hosted on GitHub, but here's a snippet if you want to dive right in.

package logstreamer

import (
	"bytes"
	"io"
	"os"
	"log"
)

type Logstreamer struct {
	Logger    *log.Logger
	buf       *bytes.Buffer
	readLines string
	// If prefix == stdout, colors green
	// If prefix == stderr, colors red
	// Else, prefix is taken as-is, and prepended to anything
	// you throw at Write()
	prefix string
	// if true, saves output in memory
	record  bool
	persist string

	// Adds color to stdout & stderr if terminal supports it
	colorOkay  string
	colorFail  string
	colorReset string
}

func NewLogstreamer(logger *log.Logger, prefix string, record bool) *Logstreamer {
	streamer := &Logstreamer{
		Logger:     logger,
		buf:        bytes.NewBuffer([]byte("")),
		prefix:     prefix,
		record:     record,
		persist:    "",
		colorOkay:  "",
		colorFail:  "",
		colorReset: "",
	}

	if os.Getenv("TERM") == "xterm" {
		streamer.colorOkay  = "\x1b[32m"
		streamer.colorFail  = "\x1b[31m"
		streamer.colorReset = "\x1b[0m"
	}

	return streamer
}

func (l *Logstreamer) Write(p []byte) (n int, err error) {
	if n, err = l.buf.Write(p); err != nil {
		return
	}

	err = l.OutputLines()
	return
}

func (l *Logstreamer) Close() error {
	l.Flush()
	l.buf = bytes.NewBuffer([]byte(""))
	return nil
}

func (l *Logstreamer) Flush() error {
	var p []byte
	if _, err := l.buf.Read(p); err != nil {
		return err
	}

	l.out(string(p))
	return nil
}

func (l *Logstreamer) OutputLines() (err error) {
	for {
		line, err := l.buf.ReadString('\n')
		if err == io.EOF {
			break
		}
		if err != nil {
			return err
		}

		l.readLines += line
		l.out(line)
	}

	return nil
}

func (l *Logstreamer) ResetReadLines() {
	l.readLines = ""
}

func (l *Logstreamer) ReadLines() string {
	return l.readLines
}

func (l *Logstreamer) FlushRecord() string {
	buffer := l.persist
	l.persist = ""
	return buffer
}

func (l *Logstreamer) out(str string) (err error) {
	if l.record == true {
		l.persist = l.persist + str
	}

	if l.prefix == "stdout" {
		str = l.colorOkay + l.prefix + l.colorReset + " " + str
	} else if l.prefix == "stderr" {
		str = l.colorFail + l.prefix + l.colorReset + " " + str
	} else {
		str = l.prefix + str
	}

	l.Logger.Print(str)

	return nil
}
package logstreamer

import (
	"log"
	"os"
	"os/exec"
	"testing"
	"fmt"
)

func TestLogstreamerOk(t *testing.T) {
	// Create a logger (your app probably already has one)
	logger := log.New(os.Stdout, "--> ", log.Ldate|log.Ltime)

	// Setup a streamer that we'll pipe cmd.Stdout to
	logStreamerOut := NewLogstreamer(logger, "stdout", false)
	// Setup a streamer that we'll pipe cmd.Stderr to.
	// We want to record/buffer anything that's written to this (3rd argument true)
	logStreamerErr := NewLogstreamer(logger, "stderr", true)

	// Execute something that succeeds
	cmd := exec.Command(
		"ls",
		"-al",
	)
	cmd.Stderr = logStreamerErr
	cmd.Stdout = logStreamerOut

	// Reset any error we recorded
	logStreamerErr.FlushRecord()

	// Execute command
	err := cmd.Start()

	// Failed to spawn?
	if err != nil {
		t.Fatal("ERROR could not spawn command.", err.Error())
	}

	// Failed to execute?
	err = cmd.Wait()
	if err != nil {
		t.Fatal("ERROR command finished with error. ", err.Error(), logStreamerErr.FlushRecord())
	}
}

func TestLogstreamerErr(t *testing.T) {
	// Create a logger (your app probably already has one)
	logger := log.New(os.Stdout, "--> ", log.Ldate|log.Ltime)

	// Setup a streamer that we'll pipe cmd.Stdout to
	logStreamerOut := NewLogstreamer(logger, "stdout", false)
	// Setup a streamer that we'll pipe cmd.Stderr to.
	// We want to record/buffer anything that's written to this (3rd argument true)
	logStreamerErr := NewLogstreamer(logger, "stderr", true)

	// Execute something that succeeds
	cmd := exec.Command(
		"ls",
		"nonexisting",
	)
	cmd.Stderr = logStreamerErr
	cmd.Stdout = logStreamerOut

	// Reset any error we recorded
	logStreamerErr.FlushRecord()

	// Execute command
	err := cmd.Start()

	// Failed to spawn?
	if err != nil {
		logger.Print("ERROR could not spawn command. ")
	}

	// Failed to execute?
	err = cmd.Wait()
	if err != nil {
		fmt.Printf("Good. command finished with %s. %s. \n", err.Error(), logStreamerErr.FlushRecord())
	} else {
		t.Fatal("This command should have failed")
	}
}

Leave a Comment Right Here