Skip to content

Commit

Permalink
Make logger configurable in the embedded ferretdb package (#4028)
Browse files Browse the repository at this point in the history
Co-authored-by: noisersup <patryk@kwiatek.xyz>
  • Loading branch information
fadyat and noisersup authored Feb 22, 2024
1 parent 033dfef commit b6fd73b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
47 changes: 30 additions & 17 deletions ferretdb/ferretdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"errors"
"fmt"
"net/url"
"sync"

"go.uber.org/zap"

Expand All @@ -38,6 +39,9 @@ import (
type Config struct {
Listener ListenerConfig

// Logger to use; if nil, it uses the default global logger.
Logger *zap.Logger

// Handler to use; one of `postgresql` or `sqlite`.
Handler string

Expand Down Expand Up @@ -113,8 +117,15 @@ func New(config *Config) (*FerretDB, error) {

metrics := connmetrics.NewListenerMetrics()

log := config.Logger
if log == nil {
log = getGlobalLogger()
} else {
log = logging.WithHooks(log)
}

h, closeBackend, err := registry.NewHandler(config.Handler, &registry.NewHandlerOpts{
Logger: logger,
Logger: log,
ConnMetrics: metrics.ConnMetrics,
StateProvider: sp,
TCPHost: config.Listener.TCP,
Expand Down Expand Up @@ -143,7 +154,7 @@ func New(config *Config) (*FerretDB, error) {
Mode: clientconn.NormalMode,
Metrics: metrics,
Handler: h,
Logger: logger,
Logger: log,
})

return &FerretDB{
Expand Down Expand Up @@ -214,21 +225,23 @@ func (f *FerretDB) MongoDBURI() string {
return u.String()
}

// logger is a global logger used by FerretDB.
//
// TODO https://github.com/FerretDB/FerretDB/issues/4014
var logger *zap.Logger
var (
loggerOnce sync.Once
logger *zap.Logger
)

// Initialize the global logger there to avoid creating too many issues for zap users that initialize it in their
// `main()` functions. It is still not a full solution; eventually, we should remove the usage of the global logger.
//
// TODO https://github.com/FerretDB/FerretDB/issues/4014
func init() {
l := zap.ErrorLevel
if version.Get().DebugBuild {
l = zap.DebugLevel
}
// getGlobalLogger retrieves or creates a global logger using
// a loggerOnce to ensure it is created only once.
func getGlobalLogger() *zap.Logger {
loggerOnce.Do(func() {
level := zap.ErrorLevel
if version.Get().DebugBuild {
level = zap.DebugLevel
}

logging.Setup(level, "console", "")
logger = zap.L()
})

logging.Setup(l, "console", "")
logger = zap.L()
return logger
}
2 changes: 2 additions & 0 deletions internal/bson2/slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func slogValue(v any) slog.Value {
if v == nil {
return slog.StringValue("RawDocument(nil)")
}

return slog.StringValue("RawDocument(" + strconv.Itoa(len(v)) + " bytes)")

case *Array:
Expand All @@ -56,6 +57,7 @@ func slogValue(v any) slog.Value {
if v == nil {
return slog.StringValue("RawArray(nil)")
}

return slog.StringValue("RawArray(" + strconv.Itoa(len(v)) + " bytes)")

default:
Expand Down
9 changes: 6 additions & 3 deletions internal/util/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,15 @@ func Setup(level zapcore.Level, encoding, uuid string) {
log.Fatal(err)
}

logger = logger.WithOptions(zap.Hooks(func(entry zapcore.Entry) error {
SetupWithZapLogger(WithHooks(logger))
}

// WithHooks returns a logger with recent entries hooks.
func WithHooks(logger *zap.Logger) *zap.Logger {
return logger.WithOptions(zap.Hooks(func(entry zapcore.Entry) error {
RecentEntries.append(&entry)
return nil
}))

SetupWithZapLogger(logger)
}

// setupSlog initializes slog logging with a given level.
Expand Down

0 comments on commit b6fd73b

Please sign in to comment.