-
Notifications
You must be signed in to change notification settings - Fork 594
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
Logrus buffer issue #1511
Merged
Merged
Logrus buffer issue #1511
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
594abee
Reset Log writer after unit test
stippi2 fd35a45
Add log.Writer() to be used instead of log.Entry().Writer()
stippi2 b3e15ac
Switch all cmds to log.Writer(), adapt step-generator
stippi2 eac39b3
Cleanup, test for logrus issue
stippi2 9030f71
More cleanup
stippi2 401078e
Unit tests for logrusWriter
stippi2 5fe2036
Merge branch 'master' into logrus-buffer-issue
stippi2 435d6ab
Align log-level with underlying tool
stippi2 6a78ee5
Merge remote-tracking branch 'origin/logrus-buffer-issue' into logrus…
stippi2 875060c
Merge branch 'master' of github.com:SAP/jenkins-library into logrus-b…
stippi2 d997907
Merge branch 'master' into logrus-buffer-issue
daniel-kurzynski 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
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
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
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
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,68 @@ | ||
package log | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
type logTarget interface { | ||
Info(args ...interface{}) | ||
Warn(args ...interface{}) | ||
Error(args ...interface{}) | ||
} | ||
|
||
// logrusWriter can be used as the destination for a tool's std output and forwards | ||
// chunks between linebreaks to the logrus framework. This works around a problem | ||
// with using Entry().Writer() directly, since that doesn't support chunks | ||
// larger than 64K without linebreaks. | ||
// Implementation copied from https://github.com/sirupsen/logrus/issues/564 | ||
type logrusWriter struct { | ||
logger logTarget | ||
buffer bytes.Buffer | ||
mutex sync.Mutex | ||
} | ||
|
||
func (w *logrusWriter) Write(buffer []byte) (int, error) { | ||
w.mutex.Lock() | ||
defer w.mutex.Unlock() | ||
|
||
origLen := len(buffer) | ||
for { | ||
if len(buffer) == 0 { | ||
return origLen, nil | ||
} | ||
linebreakIndex := bytes.IndexByte(buffer, '\n') | ||
if linebreakIndex < 0 { | ||
w.buffer.Write(buffer) | ||
return origLen, nil | ||
} | ||
|
||
w.buffer.Write(buffer[:linebreakIndex]) | ||
w.alwaysFlush() | ||
buffer = buffer[linebreakIndex+1:] | ||
} | ||
} | ||
|
||
func (w *logrusWriter) alwaysFlush() { | ||
message := w.buffer.String() | ||
w.buffer.Reset() | ||
// Align level with underlying tool (like maven or npm) | ||
// This is to avoid confusion when maven or npm print errors or warnings which piper would print as "info" | ||
if strings.Contains(message, "ERROR") || strings.Contains(message, "ERR!") { | ||
w.logger.Error(message) | ||
} else if strings.Contains(message, "WARN") { | ||
w.logger.Warn(message) | ||
} else { | ||
w.logger.Info(message) | ||
} | ||
} | ||
|
||
func (w *logrusWriter) Flush() { | ||
w.mutex.Lock() | ||
defer w.mutex.Unlock() | ||
|
||
if w.buffer.Len() != 0 { | ||
w.alwaysFlush() | ||
} | ||
} |
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.
@stippi2 would it make sense to provide a convenience method as part of command.go like
c.StderrToLog()
and similarlyc.StdoutToLog()
. I know it potentially spoils the interface but could reduce the risk of someone falling into usinglog.Entry().Writer()
. What do you think?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.
This is what auto-completion currently gives:
![image](https://user-images.githubusercontent.com/59561325/81159313-21e26c80-8f89-11ea-9d11-5fda675da084.png)
... but of course you would have to know to look in log in the first place. I think your proposal makes sense, but what do you think about providing a function returning a command instance, which is already fully configured instead (or in addition)?
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.
@stippi2 good to see that auto-completion proposes the right things;-). Up to now we do already pre-configure the redirect as part of the construction of command.
The methods would more be for scenarios were i.e. Stdout needs temporary redirection perhaps to capture command output and then afterwards reset it and send it back to the logger.
I'd also be fine to do it in an agile manner, continue as is and optimize based on future demand. Still fairly easy to extend and change that later on.
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.
I personally would prefer not keep the interfaces small. In case somebody would like to use another writer etc ... it can simply be provide at c.stderr(MY_WRITER). Less benefit added IMO with additional methods ...