Skip to content

Commit

Permalink
Fixes WithName() and kvpair.
Browse files Browse the repository at this point in the history
  • Loading branch information
jortel committed May 2, 2023
1 parent 30638a4 commit 9308694
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
9 changes: 7 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ import (
)

func main() {
log := logr.WithName("Test")
log := logr.WithName("Test", "and", "fields")

log.Info("HELLO", "name", "jeff")

err := liberr.New("This failed.", "name", "elmer")
log.Error(err, "Test this error message.")

err = liberr.Wrap(err, "This is bad.")
log.Error(err, "Test this error message (2).")

err = liberr.Wrap(err, "Wrapped again.")
log.Error(err, "Test this error message (3).")
err = errors.New("Plain error.")

err = errors.New("plain error")
log.Error(err, "")
}
2 changes: 1 addition & 1 deletion error/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (e *Error) append(kvpair []interface{}) {
// Build caused-by.
func (e *Error) causedBy(error, caused string) string {
return fmt.Sprintf(
"%s caused by: '%s'",
"%s | caused by: '%s'",
error,
caused)
}
45 changes: 28 additions & 17 deletions logr/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,15 @@ type Sink struct {
//
// WithName returns a named logger.
func WithName(name string, kvpair ...interface{}) logr.Logger {
return logr.New(&Sink{name: name})
return logr.New(
&Sink{
name: name,
fields: fields(kvpair),
})
}

//
// Init builds the delegate logger.
func (s *Sink) Init(_ logr.RuntimeInfo) {
s.delegate = log.New()
v := os.Getenv(EnvDevelopment)
Expand Down Expand Up @@ -64,8 +70,9 @@ func (s *Sink) Init(_ logr.RuntimeInfo) {
//
// Info logs at info.
func (s *Sink) Info(_ int, message string, kvpair ...interface{}) {
fields := s.asFields(kvpair...)
entry := s.delegate.WithFields(fields)
fields := fields(kvpair)
entry := s.delegate.WithFields(s.fields)
entry = entry.WithFields(fields)
entry.Info(s.named(message))
}

Expand All @@ -85,15 +92,17 @@ func (s *Sink) Error(err error, message string, kvpair ...interface{}) {
kvpair = context
}
if s.structured {
fields := s.asFields(kvpair...)
fields := fields(kvpair)
fields["error"] = xErr.Error()
fields["stack"] = xErr.Stack()
fields["logger"] = s.name
entry := s.delegate.WithFields(fields)
entry := s.delegate.WithFields(s.fields)
entry = entry.WithFields(fields)
entry.Error(s.named(message))
} else {
fields := s.asFields(kvpair...)
entry := s.delegate.WithFields(fields)
fields := fields(kvpair)
entry := s.delegate.WithFields(s.fields)
entry = entry.WithFields(fields)
if message != "" {
entry.Error(s.named(message), "\n", xErr.Error(), xErr.Stack())
} else {
Expand Down Expand Up @@ -128,11 +137,21 @@ func (s *Sink) WithName(name string) logr.LogSink {
func (s *Sink) WithValues(kvpair ...interface{}) logr.LogSink {
return &Sink{
name: s.name,
fields: s.asFields(kvpair),
fields: fields(kvpair),
}
}

func (s *Sink) asFields(kvpair ...interface{}) log.Fields {
func (s *Sink) named(message string) (m string) {
if s.name != "" {
m = "[" + s.name + "] "
}
m = m + message
return
}

//
// fields returns fields for kvpair.
func fields(kvpair []interface{}) log.Fields {
fields := log.Fields{}
for i := range kvpair {
if i%2 != 0 {
Expand All @@ -142,11 +161,3 @@ func (s *Sink) asFields(kvpair ...interface{}) log.Fields {
}
return fields
}

func (s *Sink) named(message string) (m string) {
if s.name != "" {
m = "[" + s.name + "] "
}
m = m + message
return
}

0 comments on commit 9308694

Please sign in to comment.