diff --git a/logger.go b/logger.go index 9b9e5ec..5c2f1e9 100644 --- a/logger.go +++ b/logger.go @@ -115,12 +115,12 @@ func (l *Logger) WrapNew(middlewares ...MiddlewareInterface) LoggerInterface { for _, middleware := range middlewares { handler = middleware(handler) } - return &Logger{handler: handler} + return &Logger{handler: handler, ErrorHandler: l.ErrorHandler} } // NewNopLogger will create a new no operating logger that log nowhere func NewNopLogger() *Logger { - return &Logger{handler: NopHandler} + return NewLogger(NopHandler) } // NewLogger will return a new logger diff --git a/logger_test.go b/logger_test.go index 6da4ad6..753f750 100644 --- a/logger_test.go +++ b/logger_test.go @@ -7,6 +7,7 @@ import ( "log" "log/syslog" "os" + "reflect" "strings" "testing" "time" @@ -251,6 +252,41 @@ func TestNewLogger_WrapNew(t *testing.T) { assert.Equal(t, 4, i) } +func TestNewLogger_WrapNewErrorHandler(t *testing.T) { + mockHandlerCalled := false + mockHandler := func(entry logger.Entry) error { + mockHandlerCalled = true + assert.Equal(t, "l message", entry.Message) + assert.Equal(t, logger.DebugLevel, entry.Level) + assert.Equal(t, logger.Context{}, *entry.Context) + + return errors.New("my handler error") + } + errorHandlerCalled := false + l := logger.NewLogger(mockHandler) + l.ErrorHandler = func(err error, entry logger.Entry) { + errorHandlerCalled = true + assert.EqualError(t, err, "my handler error") + } + + l2 := l.WrapNew() + + ptr1 := reflect.ValueOf(l.ErrorHandler).Pointer() + ptr2 := reflect.ValueOf(l2.(*logger.Logger).ErrorHandler).Pointer() + assert.Equal(t, ptr1, ptr2) + + l.Debug("l message") + assert.True(t, mockHandlerCalled) + assert.True(t, errorHandlerCalled) + + mockHandlerCalled = false + errorHandlerCalled = false + + l2.Debug("l message") + assert.True(t, mockHandlerCalled) + assert.True(t, errorHandlerCalled) +} + // ===================================================================================================================== // ================================================= EXAMPLES ========================================================== // =====================================================================================================================