Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate from logrus to log/slog #586

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ import (
"bufio"
"context"
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"
"testing"

"github.com/sirupsen/logrus"

"github.com/firecracker-microvm/firecracker-go-sdk/client/models"
)

Expand Down Expand Up @@ -69,9 +68,7 @@ func createMachine(ctx context.Context, name string, forwardSignals []os.Signal)
WithBin(getFirecrackerBinaryPath()).
Build(ctx)

log := logrus.New()
log.SetLevel(logrus.FatalLevel)
machine, err := NewMachine(ctx, config, WithProcessRunner(cmd), WithLogger(logrus.NewEntry(log)))
machine, err := NewMachine(ctx, config, WithProcessRunner(cmd), WithLogger(slog.New(slog.NewTextHandler(os.Stdout, nil))))
if err != nil {
return nil, cleanup, err
}
Expand Down
10 changes: 6 additions & 4 deletions client_transports.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@ package firecracker

import (
"context"
"github.com/go-openapi/runtime"
"log/slog"
"net"
"net/http"

"github.com/go-openapi/runtime"

httptransport "github.com/go-openapi/runtime/client"
"github.com/sirupsen/logrus"

"github.com/firecracker-microvm/firecracker-go-sdk/client"
)

// NewUnixSocketTransport creates a new clientTransport configured at the specified Unix socketPath.
func NewUnixSocketTransport(socketPath string, logger *logrus.Entry, debug bool) runtime.ClientTransport {
func NewUnixSocketTransport(socketPath string, logger *slog.Logger, debug bool) runtime.ClientTransport {
socketTransport := &http.Transport{
DialContext: func(ctx context.Context, network, path string) (net.Conn, error) {
addr, err := net.ResolveUnixAddr("unix", socketPath)
Expand All @@ -46,7 +47,8 @@ func NewUnixSocketTransport(socketPath string, logger *logrus.Entry, debug bool)
}

if logger != nil {
transport.SetLogger(logger)
// TODO: fix this
// transport.SetLogger(logger)
}

return transport
Expand Down
6 changes: 2 additions & 4 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ package firecracker_test
import (
"context"
"fmt"
"log/slog"
"os"
"time"

log "github.com/sirupsen/logrus"

"github.com/firecracker-microvm/firecracker-go-sdk"
models "github.com/firecracker-microvm/firecracker-go-sdk/client/models"
)
Expand Down Expand Up @@ -279,8 +278,7 @@ func ExampleJailerConfig_enablingJailer() {
f.Close()
}

logger := log.New()
m, err := firecracker.NewMachine(vmmCtx, fcCfg, firecracker.WithLogger(log.NewEntry(logger)))
m, err := firecracker.NewMachine(vmmCtx, fcCfg, firecracker.WithLogger(slog.New(slog.NewTextHandler(os.Stdout, nil))))
if err != nil {
panic(err)
}
Expand Down
39 changes: 20 additions & 19 deletions fctesting/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ package fctesting

import (
"fmt"
"io"
"log/slog"
"os"
"os/user"
"testing"

"golang.org/x/sys/unix"

log "github.com/sirupsen/logrus"
)

const rootDisableEnvName = "DISABLE_ROOT_TESTS"
Expand Down Expand Up @@ -68,23 +68,24 @@ func RequiresRoot(t testing.TB) {
}
}

func newLogger(t testing.TB) *log.Logger {
str := os.Getenv(logLevelEnvName)
l := log.New()
if str == "" {
return l
}

logLevel, err := log.ParseLevel(str)
if err != nil {
t.Fatalf("Failed to parse %q as Log Level: %v", str, err)
}

l.SetLevel(logLevel)
return l
}
// func newLogger(t testing.TB) *log.Logger {
// str := os.Getenv(logLevelEnvName)
// l := log.New()
// if str == "" {
// return l
// }
//
// logLevel, err := log.ParseLevel(str)
// if err != nil {
// t.Fatalf("Failed to parse %q as Log Level: %v", str, err)
// }
//
// l.SetLevel(logLevel)
// return l
// }

// NewLogEntry creates log.Entry. The level is specified by "FC_TEST_LOG_LEVEL" environment variable
func NewLogEntry(t testing.TB) *log.Entry {
return log.NewEntry(newLogger(t))
func NewLogEntry(t testing.TB, level slog.Level, w io.Writer) *slog.Logger {
return slog.New(slog.NewTextHandler(w, &slog.HandlerOptions{Level: level}))
//return log.NewEntry(newLogger(t))
}
3 changes: 2 additions & 1 deletion fctesting/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package fctesting

import (
"log/slog"
"os"
"testing"
)
Expand All @@ -25,6 +26,6 @@ func TestLoggingPanic(t *testing.T) {
}()

os.Setenv("FC_TEST_LOG_LEVEL", "debug")
l := NewLogEntry(t)
l := NewLogEntry(t, slog.LevelDebug, os.Stdout)
l.Debug("TestLoggingPanic")
}
7 changes: 3 additions & 4 deletions firecracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ package firecracker

import (
"context"
"log/slog"
"time"

"github.com/go-openapi/strfmt"

"github.com/sirupsen/logrus"

"github.com/firecracker-microvm/firecracker-go-sdk/client"
models "github.com/firecracker-microvm/firecracker-go-sdk/client/models"
ops "github.com/firecracker-microvm/firecracker-go-sdk/client/operations"
Expand All @@ -34,7 +33,7 @@ const (
)

// newFirecrackerClient creates a FirecrackerClient
func newFirecrackerClient(socketPath string, logger *logrus.Entry, debug bool) *client.Firecracker {
func newFirecrackerClient(socketPath string, logger *slog.Logger, debug bool) *client.Firecracker {
httpClient := client.NewHTTPClient(strfmt.NewFormats())

transport := NewUnixSocketTransport(socketPath, logger, debug)
Expand Down Expand Up @@ -62,7 +61,7 @@ type Client struct {
}

// NewClient creates a Client
func NewClient(socketPath string, logger *logrus.Entry, debug bool, opts ...ClientOpt) *Client {
func NewClient(socketPath string, logger *slog.Logger, debug bool, opts ...ClientOpt) *Client {
httpClient := newFirecrackerClient(socketPath, logger, debug)
c := &Client{client: httpClient}
c.firecrackerRequestTimeout = envValueOrDefaultInt(firecrackerRequestTimeoutEnv, defaultFirecrackerRequestTimeout)
Expand Down
6 changes: 4 additions & 2 deletions firecracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ package firecracker

import (
"context"
"log/slog"
"os"
"path/filepath"
"testing"
"time"
Expand Down Expand Up @@ -54,7 +56,7 @@ func TestClient(t *testing.T) {
PathOnHost: String(filepath.Join(testDataPath, "drive-2.img")),
}

client := NewClient(socketpath, fctesting.NewLogEntry(t), true)
client := NewClient(socketpath, fctesting.NewLogEntry(t, slog.LevelDebug, os.Stdout), true)
deadlineCtx, deadlineCancel := context.WithTimeout(ctx, 250*time.Millisecond)
defer deadlineCancel()
if err := waitForAliveVMM(deadlineCtx, client); err != nil {
Expand Down Expand Up @@ -90,7 +92,7 @@ func TestGetFirecrackerVersion(t *testing.T) {
}
}()

client := NewClient(socketpath, fctesting.NewLogEntry(t), true)
client := NewClient(socketpath, fctesting.NewLogEntry(t, slog.LevelDebug, os.Stdout), true)
deadlineCtx, deadlineCancel := context.WithTimeout(ctx, 250*time.Millisecond)
defer deadlineCancel()
if err := waitForAliveVMM(deadlineCtx, client); err != nil {
Expand Down
11 changes: 6 additions & 5 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package firecracker
import (
"context"
"fmt"
"log/slog"
"os"
)

Expand Down Expand Up @@ -135,7 +136,7 @@ var StartVMMHandler = Handler{

func createFifoOrFile(ctx context.Context, m *Machine, fifo, path string) error {
if len(fifo) > 0 {
if err := createFifo(fifo); err != nil {
if err := createFifo(fifo, m.logger); err != nil {
return err
}

Expand Down Expand Up @@ -171,7 +172,7 @@ var CreateLogFilesHandler = Handler{

if m.Cfg.FifoLogWriter != nil {
if err := m.captureFifoToFile(ctx, m.logger, m.Cfg.LogFifo, m.Cfg.FifoLogWriter); err != nil {
m.logger.Warnf("captureFifoToFile() returned %s. Continuing anyway.", err)
m.logger.Warn("captureFifoToFile() errored. Continuing anyway.", slog.Any("err", err))
}
}

Expand All @@ -192,7 +193,7 @@ var BootstrapLoggingHandler = Handler{
if err := m.setupMetrics(ctx); err != nil {
return err
}
m.logger.Debugf("setup logging: success")
m.logger.Debug("setup logging: success")
return nil
},
}
Expand Down Expand Up @@ -466,9 +467,9 @@ func (l HandlerList) Clear() HandlerList {
// any of the handlers, then the list will halt execution and return the error.
func (l HandlerList) Run(ctx context.Context, m *Machine) error {
for _, handler := range l.list {
m.logger.Debugf("Running handler %s", handler.Name)
m.logger.Debug("Running handler", slog.String("handler_name", handler.Name))
if err := handler.Fn(ctx, m); err != nil {
m.logger.Warnf("Failed handler %q: %v", handler.Name, err)
m.logger.Warn("Failed handler", slog.String("handler_name", handler.Name), slog.Any("err", err))
return err
}
}
Expand Down
7 changes: 4 additions & 3 deletions handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package firecracker
import (
"context"
"fmt"
"log/slog"
"net"
"os"
"path/filepath"
Expand Down Expand Up @@ -186,7 +187,7 @@ func TestHandlerListRun(t *testing.T) {

ctx := context.Background()
m := &Machine{
logger: fctesting.NewLogEntry(t),
logger: fctesting.NewLogEntry(t, slog.LevelDebug, os.Stdout),
}
if err := h.Run(ctx, m); err != bazErr {
t.Errorf("expected an error, but received %v", err)
Expand Down Expand Up @@ -679,8 +680,8 @@ func TestHandlers(t *testing.T) {
// resetting called for the next test
called = ""

client := NewClient(socketpath, fctesting.NewLogEntry(t), true, WithOpsClient(&c.Client))
m, err := NewMachine(ctx, c.Config, WithClient(client), WithLogger(fctesting.NewLogEntry(t)))
client := NewClient(socketpath, fctesting.NewLogEntry(t, slog.LevelDebug, os.Stdout), true, WithOpsClient(&c.Client))
m, err := NewMachine(ctx, c.Config, WithClient(client), WithLogger(fctesting.NewLogEntry(t, slog.LevelDebug, os.Stdout)))
if err != nil {
t.Fatalf("failed to create machine: %v", err)
}
Expand Down
Loading