Skip to content

Commit

Permalink
Fix logger injection
Browse files Browse the repository at this point in the history
Signed-off-by: Kimmo Lehto <[email protected]>
  • Loading branch information
kke committed Mar 7, 2024
1 parent 959d52f commit c64873d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
17 changes: 6 additions & 11 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package rig

import (
"context"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -86,7 +87,8 @@ func (c *ClientWithConfig) Setup(opts ...ClientOption) error {
if c.Client != nil {
return nil
}
client, err := NewClientWithConnectionConfigurer(&c.ConnectionConfig, opts...)
opts = append(opts, WithConnectionConfigurer(&c.ConnectionConfig))
client, err := NewClient(opts...)
if err != nil {
return fmt.Errorf("new client: %w", err)
}
Expand All @@ -112,16 +114,6 @@ func (c *ClientWithConfig) UnmarshalYAML(unmarshal func(interface{}) error) erro
return c.Setup()
}

// NewClientWithConnection returns a new Client with the given connection and options.
func NewClientWithConnection(conn protocol.Connection, opts ...ClientOption) (*Client, error) {
return NewClient(append(opts, WithConnection(conn))...)
}

// NewClientWithConnectionConfigurer returns a new Client with the given connection configurer and options.
func NewClientWithConnectionConfigurer(cc ConnectionConfigurer, opts ...ClientOption) (*Client, error) {
return NewClient(append(opts, WithConnectionConfigurer(cc))...)
}

// NewClient returns a new Connection object with the given options.
//
// You must use either WithConnection or WithConnectionConfigurer to provide a connection or
Expand All @@ -147,6 +139,7 @@ func (c *Client) setupConnection() error {
if err != nil {
return fmt.Errorf("get connection: %w", err)
}
log.Trace(context.Background(), "connection from configurer", log.HostAttr(conn))
c.connection = conn
return nil
}
Expand All @@ -161,7 +154,9 @@ func (c *Client) setup(opts ...ClientOption) error {
return
}

log.Trace(context.Background(), "client setup", log.HostAttr(c.connection))
logger := log.GetLogger(c.connection)
log.Trace(context.Background(), "logger from connection", "is_nil", logger == nil, "is_null", logger == log.Null)
log.InjectLogger(logger, c)

c.Runner = c.options.GetRunner(c.connection)
Expand Down
4 changes: 3 additions & 1 deletion log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ type Log interface {

type injectable interface {
LogWithAttrs(attrs ...any) Logger
InjectLogger(obj any)
InjectLoggerTo(obj any, attrs ...any)
SetLogger(logger Logger)
Log() Logger
}
Expand All @@ -153,6 +153,8 @@ func InjectLogger(l Logger, obj any, attrs ...any) {
} else {
o.SetLogger(l)
}
} else {
Trace(context.Background(), "logger is not injectable", slog.String("object", fmt.Sprintf("%T", obj)))
}
}

Expand Down
10 changes: 8 additions & 2 deletions rigtest/mocklogger.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package rigtest

import (
"fmt"
"context"
"log/slog"
"os"
"regexp"
Expand Down Expand Up @@ -54,7 +54,12 @@ func (m MockLogMessage) KeysAndValues() []any {

// String returns the log message as a string.
func (m MockLogMessage) String() string {
return m.message + " " + fmt.Sprint(m.keysAndValues...)
sb := strings.Builder{}
logger := slog.New(slog.NewTextHandler(&sb, &slog.HandlerOptions{
Level: slog.LevelDebug,
}))
logger.Log(context.Background(), slog.Level(m.level), m.message, m.keysAndValues...)
return sb.String()
}

// MockLogger is a mock logger.
Expand All @@ -66,6 +71,7 @@ type MockLogger struct {
func (l *MockLogger) log(level int, t string, args ...any) {
l.mu.Lock()
defer l.mu.Unlock()

l.messages = append(l.messages, MockLogMessage{level: level, message: t, keysAndValues: args})
}

Expand Down
1 change: 1 addition & 0 deletions rigtest/mockrunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func (m *MockRunner) String() string {
return "[MockRunner] " + m.MockConnection.String()
}

// StartProcess simulates a start of a process on the client.
func (m *MockRunner) StartProcess(ctx context.Context, cmd string, stdin io.Reader, stdout io.Writer, stderr io.Writer) (protocol.Waiter, error) {
return m.MockConnection.StartProcess(ctx, cmd, stdin, stdout, stderr)
}
Expand Down

0 comments on commit c64873d

Please sign in to comment.