Skip to content

Commit

Permalink
revert reverts
Browse files Browse the repository at this point in the history
  • Loading branch information
wdbaruni committed Oct 7, 2024
1 parent 9a2d19f commit bc68e01
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
7 changes: 7 additions & 0 deletions cmd/util/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/bacalhau-project/bacalhau/cmd/util/hook"
"github.com/bacalhau-project/bacalhau/pkg/config"
"github.com/bacalhau-project/bacalhau/pkg/config/types"
"github.com/bacalhau-project/bacalhau/pkg/logger"
"github.com/bacalhau-project/bacalhau/pkg/repo"
"github.com/bacalhau-project/bacalhau/pkg/setup"
)
Expand Down Expand Up @@ -75,6 +76,12 @@ func SetupConfigType(cmd *cobra.Command) (*config.Config, error) {
if err != nil {
return nil, err
}

// We always apply the configured logging level. Logging mode on the other hand is only applied with serve cmd
if err = logger.ParseAndConfigureLoggingLevel(cfg.Get(types.LoggingLevelKey).(string)); err != nil {
return nil, fmt.Errorf("failed to configure logging: %w", err)
}

return cfg, nil
}

Expand Down
28 changes: 21 additions & 7 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"runtime/debug"
"strconv"
"strings"
"sync"
"time"

"github.com/rs/zerolog/pkgerrors"
Expand All @@ -34,14 +35,18 @@ const (
LogModeCmd LogMode = "cmd"
)

var (
logMu sync.Mutex
)

func ParseLogMode(s string) (LogMode, error) {
lm := []LogMode{LogModeDefault, LogModeJSON, LogModeCmd}
for _, logMode := range lm {
if strings.ToLower(s) == strings.ToLower(string(logMode)) {
return logMode, nil
}
}
return "Error", fmt.Errorf("%q is an invalid log-mode (valid modes: %q)", s, lm)
return "", fmt.Errorf("%q is an invalid log-mode (valid modes: %q)", s, lm)
}

func ParseLogLevel(s string) (zerolog.Level, error) {
Expand All @@ -62,12 +67,14 @@ func init() { //nolint:gochecknoinits
strings.HasSuffix(os.Args[0], ".test") ||
flag.Lookup("test.v") != nil ||
flag.Lookup("test.run") != nil {
configureLogging(zerolog.DebugLevel, defaultLogging())
ConfigureLoggingLevel(zerolog.DebugLevel)
configureLogging(defaultLogging())
return
}

// the default log level when not running a test is ERROR
configureLogging(zerolog.ErrorLevel, bufferLogs())
ConfigureLoggingLevel(zerolog.ErrorLevel)
configureLogging(bufferLogs())
}

func ErrOrDebug(err error) zerolog.Level {
Expand All @@ -87,7 +94,8 @@ type tTesting interface {
func ConfigureTestLogging(t tTesting) {
oldLogger := log.Logger
oldContextLogger := zerolog.DefaultContextLogger
configureLogging(zerolog.DebugLevel, zerolog.NewConsoleWriter(zerolog.ConsoleTestWriter(t), defaultLogFormat))
ConfigureLoggingLevel(zerolog.DebugLevel)
configureLogging(zerolog.NewConsoleWriter(zerolog.ConsoleTestWriter(t), defaultLogFormat))
t.Cleanup(func() {
log.Logger = oldLogger
zerolog.DefaultContextLogger = oldContextLogger
Expand Down Expand Up @@ -120,7 +128,9 @@ func ConfigureLogging(mode LogMode, level zerolog.Level) {
default:
logWriter = defaultLogging()
}
configureLogging(level, logWriter)

ConfigureLoggingLevel(level)
configureLogging(logWriter)
LogBufferedLogs(logWriter)
}

Expand All @@ -134,12 +144,16 @@ func ParseAndConfigureLoggingLevel(level string) error {
}

func ConfigureLoggingLevel(level zerolog.Level) {
logMu.Lock()
defer logMu.Unlock()
zerolog.SetGlobalLevel(level)
}

func configureLogging(level zerolog.Level, logWriter io.Writer) {
func configureLogging(logWriter io.Writer) {
logMu.Lock()
defer logMu.Unlock()

zerolog.TimeFieldFormat = time.RFC3339Nano
ConfigureLoggingLevel(level)

info, ok := debug.ReadBuildInfo()
if ok && info.Main.Path != "" {
Expand Down
3 changes: 2 additions & 1 deletion pkg/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func TestConfigureLogging(t *testing.T) {
})

var logging strings.Builder
configureLogging(zerolog.InfoLevel, zerolog.NewConsoleWriter(func(w *zerolog.ConsoleWriter) {
ConfigureLoggingLevel(zerolog.InfoLevel)
configureLogging(zerolog.NewConsoleWriter(func(w *zerolog.ConsoleWriter) {
defaultLogFormat(w)
w.Out = &logging
w.NoColor = true
Expand Down
4 changes: 0 additions & 4 deletions pkg/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/bacalhau-project/bacalhau/pkg/config"
"github.com/bacalhau-project/bacalhau/pkg/config/types"
"github.com/bacalhau-project/bacalhau/pkg/logger"
"github.com/bacalhau-project/bacalhau/pkg/repo/migrations"

"github.com/bacalhau-project/bacalhau/pkg/repo"
Expand All @@ -25,9 +24,6 @@ func SetupMigrationManager() (*repo.MigrationManager, error) {

// SetupBacalhauRepo ensures that a bacalhau repo and config exist and are initialized.
func SetupBacalhauRepo(cfg types.Bacalhau) (*repo.FsRepo, error) {
if err := logger.ParseAndConfigureLogging(cfg.Logging.Mode, cfg.Logging.Level); err != nil {
return nil, fmt.Errorf("failed to configure logging: %w", err)
}
migrationManger, err := SetupMigrationManager()
if err != nil {
return nil, fmt.Errorf("failed to create migration manager: %w", err)
Expand Down

0 comments on commit bc68e01

Please sign in to comment.