Skip to content

Commit

Permalink
fix(util): make FatalOnError override log.Silence
Browse files Browse the repository at this point in the history
  • Loading branch information
ThinkChaos committed Nov 23, 2023
1 parent b386e22 commit 4e89b44
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func initConfig() {
util.FatalOnError("unable to load configuration: ", err)
}

log.ConfigureLogger(&cfg.Log)
log.Configure(&cfg.Log)

if len(cfg.Ports.HTTP) != 0 {
split := strings.Split(cfg.Ports.HTTP[0], ":")
Expand Down
2 changes: 1 addition & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func startServer(_ *cobra.Command, _ []string) error {
return fmt.Errorf("unable to load configuration: %w", err)
}

log.ConfigureLogger(&cfg.Log)
log.Configure(&cfg.Log)

signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)

Expand Down
30 changes: 20 additions & 10 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sync"
"sync/atomic"

"github.com/creasty/defaults"
"github.com/mattn/go-colorable"
"github.com/sirupsen/logrus"
prefixed "github.com/x-cray/logrus-prefixed-formatter"
Expand Down Expand Up @@ -49,22 +50,26 @@ type Config struct {
Timestamp bool `yaml:"timestamp" default:"true"`
}

// DefaultConfig returns a new Config initialized with default values.
func DefaultConfig() *Config {
cfg := new(Config)

defaults.MustSet(cfg)

return cfg
}

//nolint:gochecknoinits
func init() {
if !initDone.CompareAndSwap(false, true) {
return
}

logger = logrus.New()
newLogger := logrus.New()

defaultConfig := &Config{
Level: LevelInfo,
Format: FormatTypeText,
Privacy: false,
Timestamp: true,
}
ConfigureLogger(newLogger, DefaultConfig())

ConfigureLogger(defaultConfig)
logger = newLogger
}

// Log returns the global logger
Expand Down Expand Up @@ -94,8 +99,13 @@ func EscapeInput(input string) string {
return result
}

// ConfigureLogger applies configuration to the global logger
func ConfigureLogger(cfg *Config) {
// Configure applies configuration to the global logger.
func Configure(cfg *Config) {
ConfigureLogger(logger, cfg)
}

// Configure applies configuration to the given logger.
func ConfigureLogger(logger *logrus.Logger, cfg *Config) {
if level, err := logrus.ParseLevel(cfg.Level.String()); err != nil {
logger.Fatalf("invalid log level %s %v", cfg.Level, err)
} else {
Expand Down
10 changes: 9 additions & 1 deletion util/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package util
import (
"encoding/binary"
"fmt"
"io"
"net"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -159,7 +160,14 @@ func LogOnErrorWithEntry(logEntry *logrus.Entry, message string, err error) {
// FatalOnError logs the message only if error is not nil and exits the program execution
func FatalOnError(message string, err error) {
if err != nil {
log.Log().Fatal(message, err)
logger := log.Log()

// Make sure the error is printend even if the log has been silenced
if logger.Out == io.Discard {
log.ConfigureLogger(logger, log.DefaultConfig())
}

logger.Fatal(message, err)
}
}

Expand Down

0 comments on commit 4e89b44

Please sign in to comment.