diff --git a/log.go b/log.go index a05b54b..792e8c1 100644 --- a/log.go +++ b/log.go @@ -19,12 +19,11 @@ const ( ) var global *Logger -var Factory WrapperFactoryFunc +var Factory WrapperFactoryFunc = NewLogrusWrapper(logrus.StandardLogger()) func init() { global = New() global.callerFrameToSkip = 3 - global.globalFactory = true } type Logger struct { @@ -34,13 +33,10 @@ type Logger struct { excludeRulesMutex sync.RWMutex factory WrapperFactoryFunc callerFrameToSkip int - - // if globalFactory is true, Logger.factory is ignored and Factory is used instead. - globalFactory bool } func New() *Logger { - return NewWithFactory(NewLogrusWrapper(logrus.StandardLogger())) + return NewWithFactory(nil) } func NewWithFactory(factory WrapperFactoryFunc) *Logger { @@ -148,8 +144,7 @@ func (l *Logger) Panic(ctx context.Context, format string, args ...interface{}) func (l *Logger) call(ctx context.Context, level Level, format string, args ...interface{}) { var entry Wrapper - - if l.globalFactory { + if l.factory == nil { entry = Factory() } else { entry = l.factory() diff --git a/log_test.go b/log_test.go index 0ce78ea..50434ee 100644 --- a/log_test.go +++ b/log_test.go @@ -102,7 +102,6 @@ func ExampleNewZapWrapper() { func TestErrorWithStackTrace(t *testing.T) { // Init the wrapper log.Factory = log.NewTestingWrapper(t) - log.UnregisterField(log.FieldSourceLine, log.FieldSourceFile) // Init the context ctx := context.Background() ctx = context.WithValue(ctx, fieldComponent, "rockbears/log") @@ -111,6 +110,17 @@ func TestErrorWithStackTrace(t *testing.T) { log.ErrorWithStackTrace(ctx, errors.WithStack(fmt.Errorf("this is an error"))) } +func TestErrorWithStackTraceAndFactory(t *testing.T) { + // Init the wrapper + logger := log.NewWithFactory(log.NewTestingWrapper(t)) + // Init the context + ctx := context.Background() + ctx = context.WithValue(ctx, fieldComponent, "rockbears/log") + ctx = context.WithValue(ctx, fieldAsset, "ExampleErrorWithStackTrace") + logger.ErrorWithStackTrace(ctx, fmt.Errorf("this is an error")) + logger.ErrorWithStackTrace(ctx, errors.WithStack(fmt.Errorf("this is an error"))) +} + func ExampleNewStdWrapperAndSkip() { // Init the wrapper log.Factory = log.NewStdWrapper(log.StdWrapperOptions{Level: log.LevelInfo, DisableTimestamp: true})