Skip to content

Commit 138c61f

Browse files
refactor: make use of logger provided by configuration
1 parent 226b39c commit 138c61f

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

pkg/ws/hub.go

+13-8
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ package ws
33
import (
44
"encoding/json"
55
"io"
6-
"log"
76
"net/http"
87
"sync"
98

10-
"github.com/gorilla/websocket"
119
"github.com/iota-uz/iota-sdk/modules/core/domain/aggregates/user"
1210
"github.com/iota-uz/iota-sdk/modules/core/domain/entities/session"
11+
"github.com/iota-uz/iota-sdk/pkg/configuration"
1312
"github.com/iota-uz/iota-sdk/pkg/constants"
13+
14+
"github.com/gorilla/websocket"
15+
"github.com/sirupsen/logrus"
1416
)
1517

1618
type Connection struct {
@@ -95,11 +97,13 @@ type Hub struct {
9597
userConnections map[uint]Set[*Connection]
9698
channelConnections map[string]Set[*Connection]
9799
mu sync.RWMutex
100+
log *logrus.Logger
98101
}
99102

100103
var _ Huber = (*Hub)(nil)
101104

102105
func NewHub() *Hub {
106+
conf := configuration.Use()
103107
return &Hub{
104108
upgrader: websocket.Upgrader{
105109
ReadBufferSize: 1024,
@@ -111,13 +115,14 @@ func NewHub() *Hub {
111115
connections: make(Set[*Connection]),
112116
userConnections: make(map[uint]Set[*Connection]),
113117
channelConnections: make(map[string]Set[*Connection]),
118+
log: conf.Logger(),
114119
}
115120
}
116121

117122
func (h *Hub) ServeHTTP(w http.ResponseWriter, r *http.Request) {
118123
conn, err := h.upgrader.Upgrade(w, r, nil)
119124
if err != nil {
120-
log.Printf("Error upgrading connection: %v", err)
125+
h.log.Printf("Error upgrading connection: %v", err)
121126
return
122127
}
123128

@@ -156,13 +161,13 @@ func (h *Hub) readPump(conn *Connection) {
156161
_, message, err := conn.conn.ReadMessage()
157162
if err != nil {
158163
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
159-
log.Printf("WebSocket error: %v", err)
164+
h.log.Printf("WebSocket error: %v", err)
160165
}
161166
break
162167
}
163168

164169
if err := h.handleMessage(conn, message); err != nil {
165-
log.Printf("Error handling message: %v", err)
170+
h.log.Printf("Error handling message: %v", err)
166171
break
167172
}
168173
}
@@ -215,7 +220,7 @@ func (h *Hub) BroadcastToAll(message []byte) {
215220

216221
for conn := range h.connections {
217222
if err := conn.SendMessage(message); err != nil {
218-
log.Printf("Error broadcasting message: %v", err)
223+
h.log.Printf("Error broadcasting message: %v", err)
219224
}
220225
}
221226
}
@@ -227,7 +232,7 @@ func (h *Hub) BroadcastToUser(userID uint, message []byte) {
227232
if userConns, ok := h.userConnections[userID]; ok {
228233
for conn := range userConns {
229234
if err := conn.SendMessage(message); err != nil {
230-
log.Printf("Error broadcasting to user %d: %v", userID, err)
235+
h.log.Printf("Error broadcasting to user %d: %v", userID, err)
231236
}
232237
}
233238
}
@@ -240,7 +245,7 @@ func (h *Hub) BroadcastToChannel(channel string, message []byte) {
240245
if channelConns, ok := h.channelConnections[channel]; ok {
241246
for conn := range channelConns {
242247
if err := conn.SendMessage(message); err != nil {
243-
log.Printf("Error broadcasting to channel %s: %v", channel, err)
248+
h.log.Printf("Error broadcasting to channel %s: %v", channel, err)
244249
}
245250
}
246251
}

0 commit comments

Comments
 (0)