Skip to content

Conversation

@maxdml
Copy link
Collaborator

@maxdml maxdml commented Jul 18, 2025

This PR allows users to inject their existing logger to DBOS, for us to reuse.

Logging landscape

Go as four major loggers:

Why log/slog

It is design to be extensible with a custom Handler interface, and, fortunately, a long time golang contributor provided two libraries to convert a logrus or a zerolog logger into a slog instance:

Zap has its own Handler compatibility layer.

These provide a simple way for a user to create a slog logger from their existing logger and retain their configuration.

example usage

func main() {
	//SLOG
	logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
		Level: slog.LevelDebug,
	})).With("logger", "dbos-slog")
	err := dbos.Launch(dbos.WithLogger(logger))
	if err != nil {
		panic(err)
	}
	dbos.Shutdown()

	// ZAP
	zapLogger, err := zap.NewProduction()
	if err != nil {
		panic(err)
	}
	defer zapLogger.Sync()

	logger = slog.New(zapslog.NewHandler(zapLogger.Core(), zapslog.WithName("dbos-zap")))
	err = dbos.Launch(dbos.WithLogger(logger))
	if err != nil {
		panic(err)
	}
	dbos.Shutdown()

	// LOGRUS
	logrusLogger := logrus.New()
	logger = slog.New(sloglogrus.Option{
		Level:  slog.LevelDebug,
		Logger: logrusLogger,
	}.NewLogrusHandler())

	err = dbos.Launch(dbos.WithLogger(logger))
	if err != nil {
		panic(err)
	}
	dbos.Shutdown()

	// ZEROLOG
	zerologLogger := zerolog.New(zerolog.ConsoleWriter{
		Out: os.Stdout,
	}).With().Timestamp().Logger()

	logger = slog.New(slogzerolog.Option{
		Level:  slog.LevelDebug,
		Logger: &zerologLogger,
	}.NewZerologHandler())

	err = dbos.Launch(dbos.WithLogger(logger))
	if err != nil {
		panic(err)
	}
	dbos.Shutdown()
}
Screenshot 2025-07-17 at 16 54 52

@maxdml maxdml marked this pull request as ready for review July 18, 2025 02:18
Comment on lines +976 to +977
if counter >= currentCounter+2 {
t.Fatalf("Scheduled workflow continued executing after stopping scheduler: %d (expected < %d)", counter, currentCounter+2)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test is flaky

Comment on lines +78 to +88
func getLogger() *slog.Logger {
if dbos == nil {
fmt.Println("warning: DBOS instance not initialized, using default logger")
return slog.New(slog.NewTextHandler(os.Stderr, nil))
}
if logger == nil {
fmt.Println("warning: DBOS logger is nil, using default logger")
return slog.New(slog.NewTextHandler(os.Stderr, nil))
}
return logger
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A simple gate to not panic

@maxdml maxdml merged commit 416db9b into main Jul 18, 2025
1 check passed
@maxdml maxdml deleted the logger branch July 18, 2025 15:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants