-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prefix each output line with command in logs (#4)
- Loading branch information
Showing
4 changed files
with
112 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package service | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"fmt" | ||
"io" | ||
) | ||
|
||
const prefixCommandMaxlen = 16 | ||
const prefixCutCommandSuffix = "..." | ||
|
||
// LogPrefixer implements `io.writer` interface and adds prefix to each output line. | ||
type LogPrefixer struct { | ||
writer io.Writer | ||
prefix []byte | ||
} | ||
|
||
// NewLogPrefixer initializes log prefixer. | ||
func NewLogPrefixer(writer io.Writer, command string) *LogPrefixer { | ||
logPrefixer := &LogPrefixer{writer: writer} | ||
logPrefixer.prefix = logPrefixer.prefixForCommand(command) | ||
return logPrefixer | ||
} | ||
|
||
func (p *LogPrefixer) Write(data []byte) (int, error) { | ||
reader := bufio.NewReader(bytes.NewReader(data)) | ||
|
||
var line []byte | ||
var err error | ||
var bytesWritten int = 0 | ||
|
||
for { | ||
line, err = reader.ReadBytes('\n') | ||
|
||
// There can be data to write in `line` even if `io.EOF` error is returned. | ||
// Exit immediately only in case of unexpected error. | ||
if err != nil && err != io.EOF { | ||
return bytesWritten, err | ||
} | ||
|
||
if len(line) > 0 { | ||
_, writeErr := p.writer.Write(p.prefix) | ||
if writeErr != nil { | ||
return bytesWritten, writeErr | ||
} | ||
|
||
n, writeErr := p.writer.Write(line) | ||
bytesWritten += n | ||
if writeErr != nil { | ||
return bytesWritten, writeErr | ||
} | ||
} | ||
|
||
if err == io.EOF { | ||
break | ||
} | ||
} | ||
|
||
return bytesWritten, nil | ||
} | ||
|
||
func (p *LogPrefixer) prefixForCommand(command string) []byte { | ||
if len(command) > prefixCommandMaxlen { | ||
command = command[:prefixCommandMaxlen] | ||
command += prefixCutCommandSuffix | ||
} | ||
|
||
prefix := fmt.Sprintf("{%s} ", command) | ||
return []byte(prefix) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package service | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLogPrefixer_Write(t *testing.T) { | ||
out := bytes.NewBuffer(nil) | ||
prefixer := NewLogPrefixer(out, "du /var/lib/monitoring") | ||
|
||
n, err := prefixer.Write([]byte("first line of the output\n")) | ||
require.NoError(t, err) | ||
assert.Equal(t, 25, n) | ||
|
||
n, err = prefixer.Write([]byte("second line of the output\n")) | ||
require.NoError(t, err) | ||
assert.Equal(t, 26, n) | ||
|
||
expectedOutput := | ||
"{du /var/lib/moni...} first line of the output\n" + | ||
"{du /var/lib/moni...} second line of the output\n" | ||
assert.Equal(t, expectedOutput, out.String()) | ||
} | ||
|
||
func TestLogPrefixer_prefixForCommand(t *testing.T) { | ||
prefixer := &LogPrefixer{} | ||
|
||
assert.Equal(t, []byte("{ls -la} "), prefixer.prefixForCommand("ls -la")) | ||
assert.Equal(t, []byte("{cat /var/lib/pid} "), prefixer.prefixForCommand("cat /var/lib/pid")) | ||
assert.Equal(t, []byte("{du /var/lib/moni...} "), prefixer.prefixForCommand("du /var/lib/monitoring")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters