-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Log kanx child out/err to parent stdout/err (#3183)
- Loading branch information
Showing
3 changed files
with
118 additions
and
5 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package kanx | ||
|
||
import ( | ||
"io" | ||
"sync" | ||
|
||
"github.com/kanisterio/kanister/pkg/field" | ||
"github.com/kanisterio/kanister/pkg/log" | ||
) | ||
|
||
var _ io.Writer = (*logWriter)(nil) | ||
|
||
type logWriter struct { | ||
logger log.Logger | ||
writer io.Writer | ||
fields field.M | ||
mutex *sync.Mutex | ||
} | ||
|
||
func newLogWriter(l log.Logger, w io.Writer) *logWriter { | ||
return &logWriter{ | ||
logger: l, | ||
writer: w, | ||
fields: nil, | ||
mutex: &sync.Mutex{}, | ||
} | ||
} | ||
|
||
func (lw *logWriter) SetFields(m field.M) { | ||
lw.mutex.Lock() | ||
defer lw.mutex.Unlock() | ||
lw.fields = m | ||
} | ||
|
||
func (lw *logWriter) Write(buf []byte) (int, error) { | ||
lw.mutex.Lock() | ||
f := lw.fields | ||
lw.mutex.Unlock() | ||
lw.logger.PrintTo(lw.writer, string(buf), f) | ||
return len(buf), nil | ||
} |
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,66 @@ | ||
package kanx | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
|
||
"gopkg.in/check.v1" | ||
|
||
"github.com/kanisterio/kanister/pkg/field" | ||
"github.com/kanisterio/kanister/pkg/log" | ||
) | ||
|
||
type LoggerSuite struct{} | ||
|
||
var _ = check.Suite(&LoggerSuite{}) | ||
|
||
type Log struct { | ||
File *string `json:"File,omitempty"` | ||
Function *string `json:"Function,omitempty"` | ||
Line *int `json:"Line,omitempty"` | ||
Level *string `json:"level,omitempty"` | ||
Msg *string `json:"msg,omitempty"` | ||
Time *string `json:"time,omitempty"` | ||
Boo *string `json:"boo,omitempty"` | ||
} | ||
|
||
func (s *LoggerSuite) TestLogger(c *check.C) { | ||
buf := bytes.NewBuffer(nil) | ||
msg := []byte("hello!") | ||
|
||
lw := newLogWriter(log.Info(), buf) | ||
|
||
n, err := lw.Write(msg) | ||
c.Assert(err, check.IsNil) | ||
c.Assert(n, check.Equals, len(msg)) | ||
|
||
l := Log{} | ||
err = json.Unmarshal(buf.Bytes(), &l) | ||
c.Assert(err, check.IsNil) | ||
|
||
c.Assert(l.File, check.NotNil) | ||
c.Assert(l.Function, check.NotNil) | ||
c.Assert(l.Line, check.Not(check.Equals), 0) | ||
c.Assert(l.Level, check.NotNil) | ||
c.Assert(*l.Msg, check.Equals, string(msg)) | ||
c.Assert(l.Time, check.NotNil) | ||
c.Assert(l.Boo, check.IsNil) | ||
|
||
buf.Reset() | ||
lw.SetFields(field.M{"boo": "far"}) | ||
n, err = lw.Write(msg) | ||
c.Assert(err, check.IsNil) | ||
c.Assert(n, check.Equals, len(msg)) | ||
|
||
l = Log{} | ||
err = json.Unmarshal(buf.Bytes(), &l) | ||
c.Assert(err, check.IsNil) | ||
|
||
c.Assert(l.File, check.NotNil) | ||
c.Assert(l.Function, check.NotNil) | ||
c.Assert(l.Line, check.Not(check.Equals), 0) | ||
c.Assert(l.Level, check.NotNil) | ||
c.Assert(*l.Msg, check.Equals, string(msg)) | ||
c.Assert(l.Time, check.NotNil) | ||
c.Assert(*l.Boo, check.Equals, "far") | ||
} |
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