Skip to content

Commit bfcf097

Browse files
AnnatarHeclaude
andcommitted
refactor(model): replace logrus with slog in ccusage_service
Migrate logging from logrus to standard library slog for consistency with modern Go logging practices. Update all log statements to use structured logging format. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 125465f commit bfcf097

1 file changed

Lines changed: 23 additions & 24 deletions

File tree

model/ccusage_service.go

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"log/slog"
78
"net/http"
89
"os"
910
"os/exec"
1011
"os/user"
1112
"time"
12-
13-
"github.com/sirupsen/logrus"
1413
)
1514

1615
// CCUsageData represents the usage data collected from ccusage command
@@ -49,18 +48,18 @@ func NewCCUsageService(config ShellTimeConfig) CCUsageService {
4948
func (s *ccUsageService) Start(ctx context.Context) error {
5049
// Check if CCUsage is enabled
5150
if s.config.CCUsage == nil || s.config.CCUsage.Enabled == nil || !*s.config.CCUsage.Enabled {
52-
logrus.Info("CCUsage collection is disabled")
51+
slog.Info("CCUsage collection is disabled")
5352
return nil
5453
}
5554

56-
logrus.Info("Starting CCUsage collection service")
55+
slog.Info("Starting CCUsage collection service")
5756

5857
// Create a ticker for hourly collection
5958
s.ticker = time.NewTicker(1 * time.Hour)
6059

6160
// Run initial collection
6261
if err := s.CollectCCUsage(ctx); err != nil {
63-
logrus.Warnf("Initial CCUsage collection failed: %v", err)
62+
slog.Warn("Initial CCUsage collection failed", "error", err)
6463
}
6564

6665
// Start the collection loop
@@ -69,13 +68,13 @@ func (s *ccUsageService) Start(ctx context.Context) error {
6968
select {
7069
case <-s.ticker.C:
7170
if err := s.CollectCCUsage(ctx); err != nil {
72-
logrus.Warnf("CCUsage collection failed: %v", err)
71+
slog.Warn("CCUsage collection failed", "error", err)
7372
}
7473
case <-s.stopChan:
75-
logrus.Info("Stopping CCUsage collection service")
74+
slog.Info("Stopping CCUsage collection service")
7675
return
7776
case <-ctx.Done():
78-
logrus.Info("Context cancelled, stopping CCUsage collection service")
77+
slog.Info("Context cancelled, stopping CCUsage collection service")
7978
return
8079
}
8180
}
@@ -97,7 +96,7 @@ func (s *ccUsageService) CollectCCUsage(ctx context.Context) error {
9796
ctx, span := modelTracer.Start(ctx, "ccusage.collect")
9897
defer span.End()
9998

100-
logrus.Debug("Collecting CCUsage data")
99+
slog.Debug("Collecting CCUsage data")
101100

102101
since := time.Time{}
103102

@@ -111,10 +110,10 @@ func (s *ccUsageService) CollectCCUsage(ctx context.Context) error {
111110
// Try to get last sync timestamp, but don't fail if it doesn't work
112111
lastSync, err := s.getLastSyncTimestamp(ctx, endpoint)
113112
if err != nil {
114-
logrus.Warnf("Failed to get last sync timestamp: %v", err)
113+
slog.Warn("Failed to get last sync timestamp", "error", err)
115114
}
116115
since = lastSync
117-
logrus.Debugf("Got last sync timestamp: %v\n", since)
116+
slog.Debug("Got last sync timestamp", "since", since)
118117
}
119118

120119
// Collect data from ccusage command
@@ -136,7 +135,7 @@ func (s *ccUsageService) CollectCCUsage(ctx context.Context) error {
136135
}
137136
}
138137

139-
logrus.Debug("CCUsage data collection completed")
138+
slog.Debug("CCUsage data collection completed")
140139
return nil
141140
}
142141

@@ -145,7 +144,7 @@ func (s *ccUsageService) getLastSyncTimestamp(ctx context.Context, endpoint Endp
145144
// Get current hostname
146145
hostname, err := os.Hostname()
147146
if err != nil {
148-
logrus.Warnf("Failed to get hostname: %v", err)
147+
slog.Warn("Failed to get hostname", "error", err)
149148
hostname = "unknown"
150149
}
151150

@@ -173,7 +172,7 @@ func (s *ccUsageService) getLastSyncTimestamp(ctx context.Context, endpoint Endp
173172
"hostname": hostname,
174173
}
175174

176-
logrus.Debugf("Fetching CCUsage last sync for hostname: %s", hostname)
175+
slog.Debug("Fetching CCUsage last sync", "hostname", hostname)
177176

178177
err = SendGraphQLRequest(GraphQLRequestOptions[GraphQLResponse[fetchUserResponse]]{
179178
Context: ctx,
@@ -185,7 +184,7 @@ func (s *ccUsageService) getLastSyncTimestamp(ctx context.Context, endpoint Endp
185184
})
186185

187186
if err != nil {
188-
logrus.Warnf("Failed to fetch CCUsage last sync: %v", err)
187+
slog.Warn("Failed to fetch CCUsage last sync", "error", err)
189188
return time.Time{}, nil // Return nil to skip the since parameter
190189
}
191190

@@ -196,7 +195,7 @@ func (s *ccUsageService) getLastSyncTimestamp(ctx context.Context, endpoint Endp
196195
}
197196
lastSyncAt, err := time.Parse(time.RFC3339, lastSyncAtStr)
198197
if err != nil {
199-
logrus.Warnf("Failed to parse last sync timestamp: %v", err)
198+
slog.Warn("Failed to parse last sync timestamp", "error", err)
200199
return time.Time{}, err // Return nil to skip the since parameter
201200
}
202201

@@ -226,18 +225,18 @@ func (s *ccUsageService) collectData(ctx context.Context, since time.Time) (*CCU
226225
// Convert Unix timestamp (seconds) to ISO 8601 date string
227226
sinceDate := since.Format("20060102")
228227
args = append(args, "--since", sinceDate)
229-
logrus.Debugf("Using since parameter: %s (from timestamp %v)\n", sinceDate, since)
228+
slog.Debug("Using since parameter", "sinceDate", sinceDate, "since", since)
230229
}
231230

232231
var cmd *exec.Cmd
233232
if bunxErr == nil {
234233
// Use bunx if available
235234
cmd = exec.CommandContext(ctx, bunxPath, args...)
236-
logrus.Debug("Using bunx to collect ccusage data")
235+
slog.Debug("Using bunx to collect ccusage data")
237236
} else {
238237
// Fall back to npx
239238
cmd = exec.CommandContext(ctx, npxPath, args...)
240-
logrus.Debug("Using npx to collect ccusage data")
239+
slog.Debug("Using npx to collect ccusage data")
241240
}
242241

243242
// Execute the command
@@ -258,15 +257,15 @@ func (s *ccUsageService) collectData(ctx context.Context, since time.Time) (*CCU
258257
// Get system information for metadata
259258
hostname, err := os.Hostname()
260259
if err != nil {
261-
logrus.Warnf("Failed to get hostname: %v", err)
260+
slog.Warn("Failed to get hostname", "error", err)
262261
hostname = "unknown"
263262
}
264263

265264
username := os.Getenv("USER")
266265
if username == "" {
267266
currentUser, err := user.Current()
268267
if err != nil {
269-
logrus.Warnf("Failed to get username: %v", err)
268+
slog.Warn("Failed to get username", "error", err)
270269
username = "unknown"
271270
} else {
272271
username = currentUser.Username
@@ -275,7 +274,7 @@ func (s *ccUsageService) collectData(ctx context.Context, since time.Time) (*CCU
275274

276275
sysInfo, err := GetOSAndVersion()
277276
if err != nil {
278-
logrus.Warnf("Failed to get OS info: %v", err)
277+
slog.Warn("Failed to get OS info", "error", err)
279278
sysInfo = &SysInfo{
280279
Os: "unknown",
281280
Version: "unknown",
@@ -373,7 +372,7 @@ func (s *ccUsageService) sendData(ctx context.Context, endpoint Endpoint, data *
373372
}
374373

375374
if len(entries) == 0 {
376-
logrus.Debug("No CCUsage entries to send")
375+
slog.Debug("No CCUsage entries to send")
377376
return nil
378377
}
379378

@@ -404,6 +403,6 @@ func (s *ccUsageService) sendData(ctx context.Context, endpoint Endpoint, data *
404403
return fmt.Errorf("server rejected CCUsage data: %d/%d entries failed", resp.TotalCount-resp.SuccessCount, resp.TotalCount)
405404
}
406405

407-
logrus.Debugf("CCUsage data sent successfully: %d/%d entries", resp.SuccessCount, resp.TotalCount)
406+
slog.Debug("CCUsage data sent successfully", "successCount", resp.SuccessCount, "totalCount", resp.TotalCount)
408407
return nil
409408
}

0 commit comments

Comments
 (0)