Skip to content

Commit 647b38e

Browse files
committed
Refactor to use zap logger lib
1 parent 8573f8e commit 647b38e

9 files changed

Lines changed: 121 additions & 79 deletions

File tree

config/logger.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package config
2+
3+
import "go.uber.org/zap"
4+
5+
// NewLogger creates a new zap.Logger based on the provided configuration.
6+
func NewLogger(config *Config) (*zap.Logger, error) {
7+
if config.Development {
8+
return zap.NewDevelopment()
9+
}
10+
return zap.NewProduction()
11+
}

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ require (
2424
github.com/gorilla/sessions v1.4.0 // indirect
2525
github.com/pmezard/go-difflib v1.0.0 // indirect
2626
go.uber.org/dig v1.19.0 // indirect
27-
go.uber.org/multierr v1.10.0 // indirect
28-
go.uber.org/zap v1.26.0 // indirect
27+
go.uber.org/multierr v1.11.0 // indirect
28+
go.uber.org/zap v1.27.1 // indirect
2929
)
3030

3131
require (

go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,15 @@ go.uber.org/fx v1.24.0 h1:wE8mruvpg2kiiL1Vqd0CC+tr0/24XIB10Iwp2lLWzkg=
142142
go.uber.org/fx v1.24.0/go.mod h1:AmDeGyS+ZARGKM4tlH4FY2Jr63VjbEDJHtqXTGP5hbo=
143143
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
144144
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
145+
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
145146
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
146147
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
148+
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
149+
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
147150
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
148151
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
152+
go.uber.org/zap v1.27.1 h1:08RqriUEv8+ArZRYSTXy1LeBScaMpVSTBhCeaZYfMYc=
153+
go.uber.org/zap v1.27.1/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
149154
golang.org/x/arch v0.16.0 h1:foMtLTdyOmIniqWCHjY6+JxuC54XP1fDwx4N0ASyW+U=
150155
golang.org/x/arch v0.16.0/go.mod h1:JmwW7aLIoRUKgaTzhkiEFxvcEiQGyOg9BMonBJUS7EE=
151156
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

internal/repository/sqlite.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,21 @@ import (
1414
"github.com/google/uuid"
1515
_ "github.com/mattn/go-sqlite3"
1616
"go.uber.org/fx"
17+
"go.uber.org/zap"
1718
)
1819

1920
var _ Repository = (*SQLite)(nil)
2021

2122
type SQLite struct {
2223
conn *sql.DB
2324
config *config.Config
25+
logger *zap.Logger
2426
}
2527

2628
type SQLiteParams struct {
2729
fx.In
2830
Config *config.Config
31+
Logger *zap.Logger
2932
}
3033

3134
var (
@@ -54,6 +57,7 @@ func NewSQLite(p SQLiteParams) (Repository, error) {
5457
repo := &SQLite{
5558
conn: db,
5659
config: p.Config,
60+
logger: p.Logger,
5761
}
5862

5963
err = repo.migrate(p.Config.Database.Schema)

internal/repository/websocket.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"errors"
66
"fmt"
77
"io"
8-
"log"
98
"net"
109
"net/http"
1110
"time"
@@ -15,16 +14,19 @@ import (
1514
"github.com/google/uuid"
1615
"github.com/gorilla/websocket"
1716
"go.uber.org/fx"
17+
"go.uber.org/zap"
1818
)
1919

2020
var _ WebSocketRepository = (*WebSocket)(nil)
2121

2222
type WebSocket struct {
2323
connections map[uuid.UUID][]*websocket.Conn
24+
logger *zap.Logger
2425
}
2526

2627
type WebSocketParams struct {
2728
fx.In
29+
Logger *zap.Logger
2830
}
2931

3032
var upgrader = websocket.Upgrader{
@@ -39,6 +41,7 @@ func NewWebSocket(p WebSocketParams) WebSocketRepository {
3941
connections := make(map[uuid.UUID][]*websocket.Conn)
4042
return &WebSocket{
4143
connections: connections,
44+
logger: p.Logger,
4245
}
4346
}
4447

@@ -64,7 +67,7 @@ func (ws *WebSocket) AddConnection(ctx context.Context, w http.ResponseWriter, r
6467
for {
6568
err := conn.SetReadDeadline(time.Now().Add(30 * time.Second))
6669
if err != nil {
67-
fmt.Println(err)
70+
ws.logger.Error("error setting read deadline", zap.Error(err))
6871
break
6972
}
7073

@@ -75,7 +78,7 @@ func (ws *WebSocket) AddConnection(ctx context.Context, w http.ResponseWriter, r
7578
if message.Type == "ping" {
7679
errWrite := conn.WriteJSON(types.WebSocketMessage{Type: "pong"})
7780
if errWrite != nil {
78-
fmt.Println(errWrite)
81+
ws.logger.Error("error writing pong message", zap.Error(errWrite))
7982
}
8083
}
8184
continue
@@ -86,7 +89,7 @@ func (ws *WebSocket) AddConnection(ctx context.Context, w http.ResponseWriter, r
8689
break
8790
}
8891

89-
fmt.Println(err)
92+
ws.logger.Error("error reading json message", zap.Error(err))
9093
}
9194
conn.Close()
9295
ws.connections[retrospectiveID][i] = nil
@@ -119,7 +122,7 @@ func (w *WebSocket) sendMessageToRetro(ctx context.Context, message types.WebSoc
119122
}
120123
err := conn.WriteJSON(message)
121124
if err != nil {
122-
log.Printf("Error sending message %+v to connection: %v", message, err)
125+
w.logger.Error("error sending message to connection", zap.Any("message", message), zap.Error(err))
123126
}
124127
}
125128

@@ -170,11 +173,11 @@ func (w *WebSocket) DeleteQuestion(ctx context.Context, id uuid.UUID) (*types.Qu
170173
return nil, w.sendMessageToRetro(ctx, message, nil)
171174
}
172175

173-
func (s *WebSocket) GetOldRetrospectives(ctx context.Context, date time.Time) ([]uuid.UUID, error) {
176+
func (*WebSocket) GetOldRetrospectives(ctx context.Context, date time.Time) ([]uuid.UUID, error) {
174177
panic("unimplemented")
175178
}
176179

177-
func (s *WebSocket) GetAllRetrospectives(ctx context.Context) ([]uuid.UUID, error) {
180+
func (*WebSocket) GetAllRetrospectives(ctx context.Context) ([]uuid.UUID, error) {
178181
panic("unimplemented")
179182
}
180183

internal/schedule/schedule.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import (
44
"api/config"
55
"api/internal/service"
66
"context"
7-
"log"
87
"time"
98

109
"go.uber.org/fx"
10+
"go.uber.org/zap"
1111
)
1212

1313
type Schedule struct {
1414
service *service.Service
1515
config *config.Config
16+
logger *zap.Logger
1617
stopCh chan struct{}
1718
}
1819

@@ -54,7 +55,7 @@ func (s *Schedule) start() {
5455
case <-ticker.C:
5556
s.cleanUp()
5657
case <-s.stopCh:
57-
log.Println("stopping schedule")
58+
s.logger.Info("stopping schedule")
5859
return
5960
}
6061
}
@@ -66,9 +67,9 @@ func (s *Schedule) stop() {
6667
}
6768

6869
func (s *Schedule) cleanUp() {
69-
log.Println("starting clean up routine")
70+
s.logger.Info("starting clean up routine")
7071
ctx := context.Background()
7172
if err := s.service.CleanUpRetros(ctx); err != nil {
72-
log.Printf("error running clean up routine: %s", err.Error())
73+
s.logger.Error("error running clean up routine", zap.Error(err))
7374
}
7475
}

0 commit comments

Comments
 (0)