Skip to content

Commit

Permalink
change pipe writer for buffer (#674)
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Okhlopkov <[email protected]>
Co-authored-by: Pavel Okhlopkov <[email protected]>
  • Loading branch information
ldmonster and Pavel Okhlopkov authored Oct 23, 2024
1 parent 054615d commit e4f317f
Showing 1 changed file with 12 additions and 9 deletions.
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

0 comments on commit e4f317f

Please sign in to comment.