Skip to content

fix: remove trailing newline from log entries on last decode iteration #4226

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

Merged
merged 3 commits into from
May 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions cmd/nerdctl/container/container_logs_test.go
Original file line number Diff line number Diff line change
@@ -394,6 +394,34 @@ func TestLogsWithDetails(t *testing.T) {
testCase.Run(t)
}

func TestLogsFollowNoExtraneousLineFeed(t *testing.T) {
testCase := nerdtest.Setup()
// This test verifies that `nerdctl logs -f` does not add extraneous line feeds
testCase.Require = require.Not(require.Windows)

testCase.Setup = func(data test.Data, helpers test.Helpers) {
// Create a container that outputs a message without a trailing newline
helpers.Ensure("run", "-d", "--name", data.Identifier(), testutil.CommonImage,
"sh", "-c", "printf 'Hello without newline'")
}

testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rm", "-f", data.Identifier())
}

testCase.Command = func(data test.Data, helpers test.Helpers) test.TestableCommand {
// Use logs -f to follow the logs
// Arbitrary, but we need to wait until the logs show up
time.Sleep(3 * time.Second)
return helpers.Command("logs", "-f", data.Identifier())
}

// Verify that the output is exactly "Hello without newline" without any additional line feeds
testCase.Expected = test.Expects(0, nil, expect.Equals("Hello without newline"))

testCase.Run(t)
}

func TestLogsWithStartContainer(t *testing.T) {
testCase := nerdtest.Setup()

3 changes: 2 additions & 1 deletion pkg/logging/json_logger_test.go
Original file line number Diff line number Diff line change
@@ -73,6 +73,7 @@ func TestReadRotatedJSONLog(t *testing.T) {
time.Sleep(1 * time.Millisecond)
logData, _ := json.Marshal(log)
file.Write(logData)
file.Write([]byte("\n"))

if line == 5 {
file.Close()
@@ -104,7 +105,7 @@ func TestReadRotatedJSONLog(t *testing.T) {
close(containerStopped)

if expectedStdout != stdoutBuf.String() {
t.Errorf("expected: %s, acoutal: %s", expectedStdout, stdoutBuf.String())
t.Errorf("expected: %s, actual: %s", expectedStdout, stdoutBuf.String())
}
}

2 changes: 1 addition & 1 deletion pkg/logging/jsonfile/jsonfile.go
Original file line number Diff line number Diff line change
@@ -54,7 +54,7 @@ func Encode(stdout <-chan string, stderr <-chan string, writer io.Writer) error
Stream: name,
}
for logEntry := range dataChan {
e.Log = logEntry + "\n"
e.Log = logEntry
e.Time = time.Now().UTC()
encMu.Lock()
encErr := enc.Encode(e)
2 changes: 1 addition & 1 deletion pkg/logging/logging.go
Original file line number Diff line number Diff line change
@@ -255,7 +255,7 @@ func loggingProcessAdapter(ctx context.Context, driver Driver, dataStore, addres
var s string
s, err = r.ReadString('\n')
if len(s) > 0 {
dataChan <- strings.TrimSuffix(s, "\n")
dataChan <- s
}

if err != nil && err != io.EOF {