mirror of https://github.com/0xERR0R/blocky.git
fix(util): make `FatalOnError` override `log.Silence`
This commit is contained in:
parent
b386e22ebe
commit
4e89b44185
|
@ -88,7 +88,7 @@ func initConfig() {
|
||||||
util.FatalOnError("unable to load configuration: ", err)
|
util.FatalOnError("unable to load configuration: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.ConfigureLogger(&cfg.Log)
|
log.Configure(&cfg.Log)
|
||||||
|
|
||||||
if len(cfg.Ports.HTTP) != 0 {
|
if len(cfg.Ports.HTTP) != 0 {
|
||||||
split := strings.Split(cfg.Ports.HTTP[0], ":")
|
split := strings.Split(cfg.Ports.HTTP[0], ":")
|
||||||
|
|
|
@ -40,7 +40,7 @@ func startServer(_ *cobra.Command, _ []string) error {
|
||||||
return fmt.Errorf("unable to load configuration: %w", err)
|
return fmt.Errorf("unable to load configuration: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.ConfigureLogger(&cfg.Log)
|
log.Configure(&cfg.Log)
|
||||||
|
|
||||||
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
|
"github.com/creasty/defaults"
|
||||||
"github.com/mattn/go-colorable"
|
"github.com/mattn/go-colorable"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
prefixed "github.com/x-cray/logrus-prefixed-formatter"
|
prefixed "github.com/x-cray/logrus-prefixed-formatter"
|
||||||
|
@ -49,22 +50,26 @@ type Config struct {
|
||||||
Timestamp bool `yaml:"timestamp" default:"true"`
|
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
|
//nolint:gochecknoinits
|
||||||
func init() {
|
func init() {
|
||||||
if !initDone.CompareAndSwap(false, true) {
|
if !initDone.CompareAndSwap(false, true) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
logger = logrus.New()
|
newLogger := logrus.New()
|
||||||
|
|
||||||
defaultConfig := &Config{
|
ConfigureLogger(newLogger, DefaultConfig())
|
||||||
Level: LevelInfo,
|
|
||||||
Format: FormatTypeText,
|
|
||||||
Privacy: false,
|
|
||||||
Timestamp: true,
|
|
||||||
}
|
|
||||||
|
|
||||||
ConfigureLogger(defaultConfig)
|
logger = newLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log returns the global logger
|
// Log returns the global logger
|
||||||
|
@ -94,8 +99,13 @@ func EscapeInput(input string) string {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// ConfigureLogger applies configuration to the global logger
|
// Configure applies configuration to the global logger.
|
||||||
func ConfigureLogger(cfg *Config) {
|
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 {
|
if level, err := logrus.ParseLevel(cfg.Level.String()); err != nil {
|
||||||
logger.Fatalf("invalid log level %s %v", cfg.Level, err)
|
logger.Fatalf("invalid log level %s %v", cfg.Level, err)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -3,6 +3,7 @@ package util
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
@ -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
|
// FatalOnError logs the message only if error is not nil and exits the program execution
|
||||||
func FatalOnError(message string, err error) {
|
func FatalOnError(message string, err error) {
|
||||||
if err != nil {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue