Skip to content

Commit

Permalink
chore: Bring back ability to use a custom logger
Browse files Browse the repository at this point in the history
  • Loading branch information
srebhan committed May 2, 2024
1 parent 5255f1c commit 226dbbd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
15 changes: 15 additions & 0 deletions modbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ Package modbus provides a client for MODBUS TCP and RTU/ASCII.
package modbus

import (
"context"
"fmt"
"log/slog"
)

const (
Expand Down Expand Up @@ -116,3 +118,16 @@ type Connector interface {
Connect() error
Close() error
}

// Logger is an interface to define custom loggers
type Logger interface {
Debug(string, ...any)
DebugContext(context.Context, string, ...any)
Info(string, ...any)
InfoContext(context.Context, string, ...any)
Warn(string, ...any)
WarnContext(context.Context, string, ...any)
Error(string, ...any)
ErrorContext(context.Context, string, ...any)
Log(context.Context, slog.Level, string, ...any)
}
3 changes: 1 addition & 2 deletions rtu_over_udp_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package modbus
import (
"fmt"
"io"
logger "log/slog"
"net"
"sync"
)
Expand Down Expand Up @@ -46,7 +45,7 @@ type rtuUDPTransporter struct {
// Connect string
Address string
// Transmission logger
Logger *logger.Logger
Logger Logger

// UDP connection
mu sync.Mutex
Expand Down
3 changes: 1 addition & 2 deletions serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"context"
"fmt"
"io"
"log/slog"
"sync"
"time"

Expand All @@ -26,7 +25,7 @@ type serialPort struct {
// Serial port configuration.
serial.Config

Logger *slog.Logger
Logger Logger
IdleTimeout time.Duration

mu sync.Mutex
Expand Down
3 changes: 1 addition & 2 deletions tcpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"encoding/binary"
"fmt"
"io"
"log/slog"
"net"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -138,7 +137,7 @@ type tcpTransporter struct {
// Silent period after successful connection
ConnectDelay time.Duration
// Transmission logger
Logger *slog.Logger
Logger Logger

// TCP connection
mu sync.Mutex
Expand Down

0 comments on commit 226dbbd

Please sign in to comment.