mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-11-29 05:11:04 +00:00
Adjust logger types (#3859)
This commit is contained in:
parent
2fa9432ef8
commit
ba858d67cc
7 changed files with 23 additions and 12 deletions
|
@ -33,7 +33,9 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (r *Runner) createLogger(_logger zerolog.Logger, uploads *sync.WaitGroup, workflow *rpc.Workflow) pipeline.Logger {
|
func (r *Runner) createLogger(_logger zerolog.Logger, uploads *sync.WaitGroup, workflow *rpc.Workflow) pipeline.Logger {
|
||||||
return func(step *backend.Step, rc io.Reader) error {
|
return func(step *backend.Step, rc io.ReadCloser) error {
|
||||||
|
defer rc.Close()
|
||||||
|
|
||||||
logger := _logger.With().
|
logger := _logger.With().
|
||||||
Str("image", step.Image).
|
Str("image", step.Image).
|
||||||
Str("workflow_id", workflow.ID).
|
Str("workflow_id", workflow.ID).
|
||||||
|
|
|
@ -277,7 +277,7 @@ func convertPathForWindows(path string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
const maxLogLineLength = 1024 * 1024 // 1mb
|
const maxLogLineLength = 1024 * 1024 // 1mb
|
||||||
var defaultLogger = pipeline.Logger(func(step *backendTypes.Step, rc io.Reader) error {
|
var defaultLogger = pipeline.Logger(func(step *backendTypes.Step, rc io.ReadCloser) error {
|
||||||
logWriter := NewLineWriter(step.Name, step.UUID)
|
logWriter := NewLineWriter(step.Name, step.UUID)
|
||||||
return pipelineLog.CopyLineByLine(logWriter, rc, maxLogLineLength)
|
return pipelineLog.CopyLineByLine(logWriter, rc, maxLogLineLength)
|
||||||
})
|
})
|
||||||
|
|
|
@ -40,7 +40,7 @@ type LineWriter struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewLineWriter returns a new line reader.
|
// NewLineWriter returns a new line reader.
|
||||||
func NewLineWriter(peer rpc.Peer, stepUUID string, secret ...string) io.WriteCloser {
|
func NewLineWriter(peer rpc.Peer, stepUUID string, secret ...string) io.Writer {
|
||||||
lw := &LineWriter{
|
lw := &LineWriter{
|
||||||
peer: peer,
|
peer: peer,
|
||||||
stepUUID: stepUUID,
|
stepUUID: stepUUID,
|
||||||
|
@ -73,7 +73,3 @@ func (w *LineWriter) Write(p []byte) (n int, err error) {
|
||||||
|
|
||||||
return len(data), nil
|
return len(data), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *LineWriter) Close() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
|
@ -31,7 +31,6 @@ func TestLineWriter(t *testing.T) {
|
||||||
|
|
||||||
secrets := []string{"world"}
|
secrets := []string{"world"}
|
||||||
lw := log.NewLineWriter(peer, "e9ea76a5-44a1-4059-9c4a-6956c478b26d", secrets...)
|
lw := log.NewLineWriter(peer, "e9ea76a5-44a1-4059-9c4a-6956c478b26d", secrets...)
|
||||||
defer lw.Close()
|
|
||||||
|
|
||||||
_, err := lw.Write([]byte("hello world\n"))
|
_, err := lw.Write([]byte("hello world\n"))
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
|
@ -20,7 +20,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
func writeChunks(dst io.WriteCloser, data []byte, size int) error {
|
func writeChunks(dst io.Writer, data []byte, size int) error {
|
||||||
if len(data) <= size {
|
if len(data) <= size {
|
||||||
_, err := dst.Write(data)
|
_, err := dst.Write(data)
|
||||||
return err
|
return err
|
||||||
|
@ -41,9 +41,8 @@ func writeChunks(dst io.WriteCloser, data []byte, size int) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func CopyLineByLine(dst io.WriteCloser, src io.Reader, maxSize int) error {
|
func CopyLineByLine(dst io.Writer, src io.Reader, maxSize int) error {
|
||||||
r := bufio.NewReader(src)
|
r := bufio.NewReader(src)
|
||||||
defer dst.Close()
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
// TODO: read til newline or maxSize directly
|
// TODO: read til newline or maxSize directly
|
||||||
|
|
|
@ -144,3 +144,18 @@ func TestCopyLineByLineSizeLimit(t *testing.T) {
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStringReader(t *testing.T) {
|
||||||
|
r := io.NopCloser(strings.NewReader("123\n4567\n890"))
|
||||||
|
|
||||||
|
testWriter := &testWriter{
|
||||||
|
Mutex: &sync.Mutex{},
|
||||||
|
writes: make([]string, 0),
|
||||||
|
}
|
||||||
|
|
||||||
|
err := log.CopyLineByLine(testWriter, r, 1024)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
writes := testWriter.GetWrites()
|
||||||
|
assert.Lenf(t, writes, 3, "expected 3 writes, got: %v", writes)
|
||||||
|
}
|
||||||
|
|
|
@ -21,4 +21,4 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Logger handles the process logging.
|
// Logger handles the process logging.
|
||||||
type Logger func(*backend.Step, io.Reader) error
|
type Logger func(*backend.Step, io.ReadCloser) error
|
||||||
|
|
Loading…
Reference in a new issue