Skip to content

Commit

Permalink
feat: add logger in server and client
Browse files Browse the repository at this point in the history
Signed-off-by: ZhangJian He <shoothzj@gmail.com>
  • Loading branch information
shoothzj committed Sep 9, 2024
1 parent 8104242 commit 0841cac
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go_ci_lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: '1.20'
go-version: '1.21'
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/go_mod_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: '1.20'
go-version: '1.21'
- name: Run Go Mod Check Action
uses: Shoothzj/go-mod-check-action@main
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/go_unit_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: '1.20'
go-version: '1.21'
- name: Run coverage
run: go test ./... -coverpkg=./opcua/... -race -coverprofile=coverage.out -covermode=atomic
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module opcua-go

go 1.20
go 1.21

require github.com/stretchr/testify v1.9.0

Expand Down
3 changes: 3 additions & 0 deletions opcua/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package opcua

import "log/slog"

type ClientConfig struct {
logger *slog.Logger

Check failure on line 6 in opcua/client.go

View workflow job for this annotation

GitHub Actions / lint

field `logger` is unused (unused)
}

type Client struct {
Expand Down
8 changes: 8 additions & 0 deletions opcua/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package opcua

import (
"fmt"
"log/slog"
"net"
"sync"
)

type ServerConfig struct {
Host string
Port int

logger *slog.Logger
}

func (s *ServerConfig) addr() string {
Expand All @@ -19,6 +22,8 @@ type Server struct {
config *ServerConfig
listener net.Listener

logger *slog.Logger

mutex sync.RWMutex
quit chan bool
}
Expand All @@ -27,7 +32,9 @@ func NewServer(config *ServerConfig) *Server {
server := &Server{
config: config,
quit: make(chan bool),
logger: config.logger,
}
server.logger.Info("server initialized", slog.String("host", config.Host), slog.Int("port", config.Port))
return server
}

Expand Down Expand Up @@ -91,6 +98,7 @@ func (s *Server) Close() error {
err := s.listener.Close()
s.listener = nil
if err == nil {
s.logger.Info("server closed successfully")
return nil
}
return fmt.Errorf("failed to close listener: %w", err)
Expand Down
6 changes: 4 additions & 2 deletions opcua/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package opcua
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"log/slog"
"testing"
)

func TestStartWithZeroPort(t *testing.T) {
config := &ServerConfig{
Host: "localhost",
Port: 0,
Host: "localhost",
Port: 0,
logger: slog.Default(),
}

server := NewServer(config)
Expand Down

0 comments on commit 0841cac

Please sign in to comment.