Skip to content

Commit 3e98779

Browse files
authored
refactor(log): add DebugIf and IsDebugEnabled methods (#26)
- Only print debug messages if debugging is enabled - Warn about unusual pagination and HTTP responses - Change debug to warn in validation failures
1 parent eb98cff commit 3e98779

File tree

6 files changed

+66
-35
lines changed

6 files changed

+66
-35
lines changed

internal/collections/manager.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ func NewCollectionsManager(db *database.Queries) *CollectionsManager {
1515

1616
func (c *CollectionsManager) Create(ctx context.Context, name string) (CollectionEntity, error) {
1717
if err := crud.ValidateName(name); err != nil {
18-
log.Debug("collection creation failed validation", "name", name)
18+
log.Warn("collection creation failed validation", "name", name)
1919
return CollectionEntity{}, crud.ErrInvalidInput
2020
}
2121

22-
log.Debug("creating collection", "name", name)
22+
log.DebugIf("creating collection", "name", name)
2323
collection, err := c.DB.CreateCollection(ctx, name)
2424
if err != nil {
2525
log.Error("failed to create collection", "name", name, "error", err)
@@ -32,11 +32,11 @@ func (c *CollectionsManager) Create(ctx context.Context, name string) (Collectio
3232

3333
func (c *CollectionsManager) Read(ctx context.Context, id int64) (CollectionEntity, error) {
3434
if err := crud.ValidateID(id); err != nil {
35-
log.Debug("collection read failed validation", "id", id)
35+
log.Warn("collection read failed validation", "id", id)
3636
return CollectionEntity{}, crud.ErrInvalidInput
3737
}
3838

39-
log.Debug("reading collection", "id", id)
39+
log.DebugIf("reading collection", "id", id)
4040
collection, err := c.DB.GetCollection(ctx, id)
4141
if err != nil {
4242
if err == sql.ErrNoRows {
@@ -52,15 +52,15 @@ func (c *CollectionsManager) Read(ctx context.Context, id int64) (CollectionEnti
5252

5353
func (c *CollectionsManager) Update(ctx context.Context, id int64, name string) (CollectionEntity, error) {
5454
if err := crud.ValidateID(id); err != nil {
55-
log.Debug("collection update failed ID validation", "id", id)
55+
log.Warn("collection update failed ID validation", "id", id)
5656
return CollectionEntity{}, crud.ErrInvalidInput
5757
}
5858
if err := crud.ValidateName(name); err != nil {
59-
log.Debug("collection update failed name validation", "name", name)
59+
log.Warn("collection update failed name validation", "name", name)
6060
return CollectionEntity{}, crud.ErrInvalidInput
6161
}
6262

63-
log.Debug("updating collection", "id", id, "name", name)
63+
log.DebugIf("updating collection", "id", id, "name", name)
6464
collection, err := c.DB.UpdateCollectionName(ctx, database.UpdateCollectionNameParams{
6565
Name: name,
6666
ID: id,
@@ -80,11 +80,11 @@ func (c *CollectionsManager) Update(ctx context.Context, id int64, name string)
8080

8181
func (c *CollectionsManager) Delete(ctx context.Context, id int64) error {
8282
if err := crud.ValidateID(id); err != nil {
83-
log.Debug("collection delete failed validation", "id", id)
83+
log.Warn("collection delete failed validation", "id", id)
8484
return crud.ErrInvalidInput
8585
}
8686

87-
log.Debug("deleting collection", "id", id)
87+
log.DebugIf("deleting collection", "id", id)
8888
err := c.DB.DeleteCollection(ctx, id)
8989
if err != nil {
9090
log.Error("failed to delete collection", "id", id, "error", err)
@@ -96,7 +96,7 @@ func (c *CollectionsManager) Delete(ctx context.Context, id int64) error {
9696
}
9797

9898
func (c *CollectionsManager) List(ctx context.Context) ([]CollectionEntity, error) {
99-
log.Debug("listing all collections with default pagination")
99+
log.DebugIf("listing all collections with default pagination")
100100
paginated, err := c.ListPaginated(ctx, 50, 0)
101101
if err != nil {
102102
return nil, err
@@ -105,7 +105,15 @@ func (c *CollectionsManager) List(ctx context.Context) ([]CollectionEntity, erro
105105
}
106106

107107
func (c *CollectionsManager) ListPaginated(ctx context.Context, limit, offset int) (*PaginatedCollections, error) {
108-
log.Debug("listing paginated collections", "limit", limit, "offset", offset)
108+
// Warn about unusual pagination parameters
109+
if limit > 1000 {
110+
log.Warn("large pagination limit requested", "limit", limit)
111+
}
112+
if offset < 0 {
113+
log.Warn("negative pagination offset", "offset", offset)
114+
}
115+
116+
log.DebugIf("listing paginated collections", "limit", limit, "offset", offset)
109117

110118
total, err := c.DB.CountCollections(ctx)
111119
if err != nil {

internal/crud/utils.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ import (
1111
func ValidateName(name string) error {
1212
name = strings.TrimSpace(name)
1313
if name == "" {
14-
log.Debug("validation failed: empty name")
14+
log.Warn("validation failed: empty name")
1515
return fmt.Errorf("name cannot be empty")
1616
}
1717
if len(name) > 100 {
18-
log.Debug("validation failed: name too long", "length", len(name))
18+
log.Warn("validation failed: name too long", "length", len(name))
1919
return fmt.Errorf("name cannot exceed 100 characters")
2020
}
2121
return nil
2222
}
2323

2424
func ValidateID(id int64) error {
2525
if id <= 0 {
26-
log.Debug("validation failed: invalid ID", "id", id)
26+
log.Warn("validation failed: invalid ID", "id", id)
2727
return fmt.Errorf("ID must be positive")
2828
}
2929
return nil
@@ -33,7 +33,7 @@ func ValidateID(id int64) error {
3333
func ParseTimestamp(timestamp string) time.Time {
3434
parsed, err := time.Parse(time.RFC3339, timestamp)
3535
if err != nil {
36-
log.Debug("failed to parse timestamp", "timestamp", timestamp, "error", err)
36+
log.Warn("failed to parse timestamp", "timestamp", timestamp, "error", err)
3737
return time.Time{}
3838
}
3939
return parsed

internal/endpoints/manager.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ func (e *EndpointsManager) Create(ctx context.Context, name string) (EndpointEnt
2121

2222
func (e *EndpointsManager) Read(ctx context.Context, id int64) (EndpointEntity, error) {
2323
if err := crud.ValidateID(id); err != nil {
24-
log.Debug("endpoint read failed validation", "id", id)
24+
log.Warn("endpoint read failed validation", "id", id)
2525
return EndpointEntity{}, crud.ErrInvalidInput
2626
}
2727

28-
log.Debug("reading endpoint", "id", id)
28+
log.DebugIf("reading endpoint", "id", id)
2929
endpoint, err := e.DB.GetEndpoint(ctx, id)
3030
if err != nil {
3131
if err == sql.ErrNoRows {
@@ -45,11 +45,11 @@ func (e *EndpointsManager) Update(ctx context.Context, id int64, name string) (E
4545

4646
func (e *EndpointsManager) Delete(ctx context.Context, id int64) error {
4747
if err := crud.ValidateID(id); err != nil {
48-
log.Debug("endpoint delete failed validation", "id", id)
48+
log.Warn("endpoint delete failed validation", "id", id)
4949
return crud.ErrInvalidInput
5050
}
5151

52-
log.Debug("deleting endpoint", "id", id)
52+
log.DebugIf("deleting endpoint", "id", id)
5353
err := e.DB.DeleteEndpoint(ctx, id)
5454
if err != nil {
5555
log.Error("failed to delete endpoint", "id", id, "error", err)
@@ -66,11 +66,11 @@ func (e *EndpointsManager) List(ctx context.Context) ([]EndpointEntity, error) {
6666

6767
func (e *EndpointsManager) ListByCollection(ctx context.Context, collectionID int64, limit, offset int) (*PaginatedEndpoints, error) {
6868
if err := crud.ValidateID(collectionID); err != nil {
69-
log.Debug("endpoint list failed collection validation", "collection_id", collectionID)
69+
log.Warn("endpoint list failed collection validation", "collection_id", collectionID)
7070
return nil, crud.ErrInvalidInput
7171
}
7272

73-
log.Debug("listing paginated endpoints", "collection_id", collectionID, "limit", limit, "offset", offset)
73+
log.DebugIf("listing paginated endpoints", "collection_id", collectionID, "limit", limit, "offset", offset)
7474

7575
total, err := e.DB.CountEndpointsByCollection(ctx, collectionID)
7676
if err != nil {
@@ -105,15 +105,15 @@ func (e *EndpointsManager) ListByCollection(ctx context.Context, collectionID in
105105

106106
func (e *EndpointsManager) CreateEndpoint(ctx context.Context, data EndpointData) (EndpointEntity, error) {
107107
if err := crud.ValidateID(data.CollectionID); err != nil {
108-
log.Debug("endpoint creation failed collection validation", "collection_id", data.CollectionID)
108+
log.Warn("endpoint creation failed collection validation", "collection_id", data.CollectionID)
109109
return EndpointEntity{}, crud.ErrInvalidInput
110110
}
111111
if err := crud.ValidateName(data.Name); err != nil {
112-
log.Debug("endpoint creation failed name validation", "name", data.Name)
112+
log.Warn("endpoint creation failed name validation", "name", data.Name)
113113
return EndpointEntity{}, crud.ErrInvalidInput
114114
}
115115
if data.Method == "" || data.URL == "" {
116-
log.Debug("endpoint creation failed - method and URL required", "method", data.Method, "url", data.URL)
116+
log.Warn("endpoint creation failed - method and URL required", "method", data.Method, "url", data.URL)
117117
return EndpointEntity{}, crud.ErrInvalidInput
118118
}
119119

@@ -132,7 +132,7 @@ func (e *EndpointsManager) CreateEndpoint(ctx context.Context, data EndpointData
132132
queryParamsJSON = string(qpBytes)
133133
}
134134

135-
log.Debug("creating endpoint", "collection_id", data.CollectionID, "name", data.Name, "method", data.Method, "url", data.URL)
135+
log.DebugIf("creating endpoint", "collection_id", data.CollectionID, "name", data.Name, "method", data.Method, "url", data.URL)
136136
endpoint, err := e.DB.CreateEndpoint(ctx, database.CreateEndpointParams{
137137
CollectionID: data.CollectionID,
138138
Name: data.Name,
@@ -153,15 +153,15 @@ func (e *EndpointsManager) CreateEndpoint(ctx context.Context, data EndpointData
153153

154154
func (e *EndpointsManager) UpdateEndpoint(ctx context.Context, id int64, data EndpointData) (EndpointEntity, error) {
155155
if err := crud.ValidateID(id); err != nil {
156-
log.Debug("endpoint update failed ID validation", "id", id)
156+
log.Warn("endpoint update failed ID validation", "id", id)
157157
return EndpointEntity{}, crud.ErrInvalidInput
158158
}
159159
if err := crud.ValidateName(data.Name); err != nil {
160-
log.Debug("endpoint update failed name validation", "name", data.Name)
160+
log.Warn("endpoint update failed name validation", "name", data.Name)
161161
return EndpointEntity{}, crud.ErrInvalidInput
162162
}
163163
if data.Method == "" || data.URL == "" {
164-
log.Debug("endpoint update failed - method and URL required", "method", data.Method, "url", data.URL)
164+
log.Warn("endpoint update failed - method and URL required", "method", data.Method, "url", data.URL)
165165
return EndpointEntity{}, crud.ErrInvalidInput
166166
}
167167

@@ -180,7 +180,7 @@ func (e *EndpointsManager) UpdateEndpoint(ctx context.Context, id int64, data En
180180
queryParamsJSON = string(qpBytes)
181181
}
182182

183-
log.Debug("updating endpoint", "id", id, "name", data.Name, "method", data.Method, "url", data.URL)
183+
log.DebugIf("updating endpoint", "id", id, "name", data.Name, "method", data.Method, "url", data.URL)
184184
endpoint, err := e.DB.UpdateEndpoint(ctx, database.UpdateEndpointParams{
185185
Name: data.Name,
186186
Method: data.Method,

internal/history/manager.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (h *HistoryManager) Read(ctx context.Context, id int64) (HistoryEntity, err
3131
return HistoryEntity{}, err
3232
}
3333

34-
log.Debug("read history entry", "id", id)
34+
log.DebugIf("read history entry", "id", id)
3535
return HistoryEntity{History: history}, nil
3636
}
3737

@@ -111,7 +111,7 @@ func (h *HistoryManager) RecordExecution(ctx context.Context, data ExecutionData
111111
return HistoryEntity{}, err
112112
}
113113

114-
log.Debug("recording execution", "method", data.Method, "url", data.URL, "status", data.StatusCode)
114+
log.DebugIf("recording execution", "method", data.Method, "url", data.URL, "status", data.StatusCode)
115115

116116
// Marshal to JSON for database storage
117117
requestHeaders, err := json.Marshal(data.Headers)
@@ -158,17 +158,17 @@ func (h *HistoryManager) RecordExecution(ctx context.Context, data ExecutionData
158158

159159
func validateExecutionData(data ExecutionData) error {
160160
if err := crud.ValidateName(data.Method); err != nil {
161-
log.Debug("execution validation failed: invalid method", "method", data.Method)
161+
log.Warn("execution validation failed: invalid method", "method", data.Method)
162162
return fmt.Errorf("invalid method: %w", err)
163163
}
164164

165165
if err := crud.ValidateName(data.URL); err != nil {
166-
log.Debug("execution validation failed: invalid URL", "url", data.URL)
166+
log.Warn("execution validation failed: invalid URL", "url", data.URL)
167167
return fmt.Errorf("invalid URL: %w", err)
168168
}
169169

170170
if data.StatusCode < 100 || data.StatusCode > 599 {
171-
log.Debug("execution validation failed: invalid status code", "status_code", data.StatusCode)
171+
log.Warn("execution validation failed: invalid status code", "status_code", data.StatusCode)
172172
return fmt.Errorf("invalid status code: %d", data.StatusCode)
173173
}
174174

internal/http/manager.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (h *HTTPManager) ExecuteRequest(req *Request) (*Response, error) {
5959
return nil, err
6060
}
6161

62-
log.Debug("executing HTTP request", "method", req.Method, "url", req.URL)
62+
log.DebugIf("executing HTTP request", "method", req.Method, "url", req.URL)
6363

6464
requestURL, err := h.buildURL(req.URL, req.QueryParams)
6565
if err != nil {
@@ -109,6 +109,18 @@ func (h *HTTPManager) ExecuteRequest(req *Request) (*Response, error) {
109109

110110
duration := time.Since(start)
111111

112+
// Log warnings for concerning HTTP responses
113+
if resp.StatusCode >= 400 && resp.StatusCode < 500 {
114+
log.Warn("HTTP client error", "status", resp.StatusCode, "url", req.URL)
115+
} else if resp.StatusCode >= 500 {
116+
log.Warn("HTTP server error", "status", resp.StatusCode, "url", req.URL)
117+
}
118+
119+
// Warn about slow requests (>5 seconds)
120+
if duration > 5*time.Second {
121+
log.Warn("slow HTTP request", "duration", duration, "url", req.URL)
122+
}
123+
112124
response := &Response{
113125
StatusCode: resp.StatusCode,
114126
Status: resp.Status,

internal/log/logger.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,17 @@ func Debug(msg string, args ...any) {
109109
Global().Debug(msg, args...)
110110
}
111111

112+
func DebugIf(msg string, args ...any) {
113+
if IsDebugEnabled() {
114+
Global().Debug(msg, args...)
115+
}
116+
}
117+
118+
func IsDebugEnabled() bool {
119+
return os.Getenv("REQ_DEBUG") == "1" ||
120+
os.Getenv("REQ_LOG_LEVEL") == "debug"
121+
}
122+
112123
func Info(msg string, args ...any) {
113124
Global().Info(msg, args...)
114125
}

0 commit comments

Comments
 (0)