- Notifications
You must be signed in to change notification settings - Fork949
add iologger for debugging purposes#15
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,45 @@ | ||
package log | ||
import ( | ||
"io" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
// IOLogger is a wrapper around io.Reader and io.Writer that can be used | ||
// to log the data being read and written from the underlying streams | ||
type IOLogger struct { | ||
reader io.Reader | ||
writer io.Writer | ||
logger *log.Logger | ||
} | ||
// NewIOLogger creates a new IOLogger instance | ||
func NewIOLogger(r io.Reader, w io.Writer, logger *log.Logger) *IOLogger { | ||
return &IOLogger{ | ||
reader: r, | ||
writer: w, | ||
logger: logger, | ||
} | ||
} | ||
// Read reads data from the underlying io.Reader and logs it. | ||
func (l *IOLogger) Read(p []byte) (n int, err error) { | ||
if l.reader == nil { | ||
return 0, io.EOF | ||
} | ||
n, err = l.reader.Read(p) | ||
if n > 0 { | ||
l.logger.Infof("[stdin]: received %d bytes: %s", n, string(p[:n])) | ||
} | ||
return n, err | ||
} | ||
// Write writes data to the underlying io.Writer and logs it. | ||
func (l *IOLogger) Write(p []byte) (n int, err error) { | ||
if l.writer == nil { | ||
return 0, io.ErrClosedPipe | ||
} | ||
l.logger.Infof("[stdout]: sending %d bytes: %s", len(p), string(p)) | ||
return l.writer.Write(p) | ||
} |
This file contains hidden or 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,65 @@ | ||
package log | ||
import ( | ||
"bytes" | ||
"strings" | ||
"testing" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
func TestLoggedReadWriter(t *testing.T) { | ||
t.Run("Read method logs and passes data", func(t *testing.T) { | ||
// Setup | ||
inputData := "test input data" | ||
reader := strings.NewReader(inputData) | ||
// Create logger with buffer to capture output | ||
var logBuffer bytes.Buffer | ||
logger := log.New() | ||
logger.SetOutput(&logBuffer) | ||
logger.SetFormatter(&log.TextFormatter{ | ||
DisableTimestamp: true, | ||
}) | ||
lrw := NewIOLogger(reader, nil, logger) | ||
// Test Read | ||
buf := make([]byte, 100) | ||
n, err := lrw.Read(buf) | ||
// Assertions | ||
assert.NoError(t, err) | ||
assert.Equal(t, len(inputData), n) | ||
assert.Equal(t, inputData, string(buf[:n])) | ||
assert.Contains(t, logBuffer.String(), "[stdin]") | ||
assert.Contains(t, logBuffer.String(), inputData) | ||
}) | ||
t.Run("Write method logs and passes data", func(t *testing.T) { | ||
// Setup | ||
outputData := "test output data" | ||
var writeBuffer bytes.Buffer | ||
// Create logger with buffer to capture output | ||
var logBuffer bytes.Buffer | ||
logger := log.New() | ||
logger.SetOutput(&logBuffer) | ||
logger.SetFormatter(&log.TextFormatter{ | ||
DisableTimestamp: true, | ||
}) | ||
lrw := NewIOLogger(nil, &writeBuffer, logger) | ||
// Test Write | ||
n, err := lrw.Write([]byte(outputData)) | ||
// Assertions | ||
assert.NoError(t, err) | ||
assert.Equal(t, len(outputData), n) | ||
assert.Equal(t, outputData, writeBuffer.String()) | ||
assert.Contains(t, logBuffer.String(), "[stdout]") | ||
assert.Contains(t, logBuffer.String(), outputData) | ||
}) | ||
} |
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.