github.com/quickfeed/quickfeed@v0.0.0-20240507093252-ed8ca812a09c/internal/qtest/logger_helper.go (about)

     1  package qtest
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"go.uber.org/zap"
     8  	"go.uber.org/zap/zapcore"
     9  )
    10  
    11  func Logger(t *testing.T) *zap.SugaredLogger {
    12  	t.Helper()
    13  	if os.Getenv("LOG") == "" {
    14  		return zap.NewNop().Sugar()
    15  	}
    16  	cfg := zap.NewDevelopmentConfig()
    17  	// add colorization
    18  	cfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
    19  	// we only want stack trace enabled for panic level and above
    20  	logger, err := cfg.Build(zap.AddStacktrace(zapcore.PanicLevel))
    21  	if err != nil {
    22  		t.Fatalf("cannot initialize logger: %v", err)
    23  	}
    24  	return logger.Sugar()
    25  }