-
-
Notifications
You must be signed in to change notification settings - Fork 10
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 rotated logs support #5
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we really need all of this? How about this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But it will only prefix only first line of the command output.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would probably work if command produces output line by line, means internally calling
Write
for every line. Which is not the case even forls -l
, just tried.So all these complications are to prefix each line, no matter how many of them are contained in one
data
portion.