diff --git a/cmd/main.go b/cmd/main.go index 5a9c542..44e9049 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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, "") } diff --git a/error/error.go b/error/error.go index 398a421..f6e5123 100644 --- a/error/error.go +++ b/error/error.go @@ -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) } diff --git a/logr/logger.go b/logr/logger.go index 9f7fd67..e4d9c1e 100644 --- a/logr/logger.go +++ b/logr/logger.go @@ -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) @@ -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)) } @@ -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 { @@ -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 { @@ -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 -}