Skip to content
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

[shell-operator] Fix logrus leak #674

Merged
merged 1 commit into from
Oct 23, 2024
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
21 changes: 12 additions & 9 deletions pkg/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,10 @@ func RunAndLogLines(cmd *exec.Cmd, logLabels map[string]string) (*CmdUsage, erro

logEntry.Debugf("Executing command '%s' in '%s' dir", strings.Join(cmd.Args, " "), cmd.Dir)

if app.LogProxyHookJSON {
plo := &proxyJSONLogger{stdoutLogEntry, make([]byte, 0)}
ple := &proxyJSONLogger{stderrLogEntry, make([]byte, 0)}
cmd.Stdout = plo
cmd.Stderr = io.MultiWriter(ple, stdErr)
} else {
cmd.Stdout = stdoutLogEntry.Writer()
cmd.Stderr = io.MultiWriter(stderrLogEntry.Writer(), stdErr)
}
plo := &proxyJSONLogger{stdoutLogEntry, make([]byte, 0), app.LogProxyHookJSON}
ple := &proxyJSONLogger{stderrLogEntry, make([]byte, 0), app.LogProxyHookJSON}
cmd.Stdout = plo
cmd.Stderr = io.MultiWriter(ple, stdErr)

err := cmd.Run()
if err != nil {
Expand Down Expand Up @@ -78,11 +73,19 @@ type proxyJSONLogger struct {
*log.Entry

buf []byte

logProxyHookJSON bool
}

func (pj *proxyJSONLogger) Write(p []byte) (n int, err error) {
pj.buf = append(pj.buf, p...)

if !pj.logProxyHookJSON {
pj.Entry.Log(log.InfoLevel, strings.TrimSpace(string(pj.buf)))

return len(p), nil
}

var line interface{}
err = json.Unmarshal(pj.buf, &line)
if err != nil {
Expand Down
Loading