Skip to content

Commit f64686c

Browse files
authored
refactor(pkg/cache): use context logger instead of struct logger (#141)
# Remove global logger in favor of context-based logging The cache package now uses context-based logging instead of maintaining a global logger instance. This change: - Removes the global logger field from the Cache struct - Passes logger through context in all methods - Updates method signatures to use context-based logging - Ensures consistent logging patterns across the codebase - Maintains proper logging context and correlation The change improves logging consistency and makes it easier to trace operations through the system by using context-based logging throughout the cache package.
1 parent 630ba1d commit f64686c

File tree

5 files changed

+335
-242
lines changed

5 files changed

+335
-242
lines changed

cmd/serve.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func serveAction() cli.ActionFunc {
139139
return fmt.Errorf("error computing the upstream caches: %w", err)
140140
}
141141

142-
cache, err := createCache(ctx, logger, cmd, ucs)
142+
cache, err := createCache(ctx, cmd, ucs)
143143
if err != nil {
144144
return err
145145
}
@@ -201,7 +201,6 @@ func getUpstreamCaches(_ context.Context, logger zerolog.Logger, cmd *cli.Comman
201201

202202
func createCache(
203203
ctx context.Context,
204-
logger zerolog.Logger,
205204
cmd *cli.Command,
206205
ucs []upstream.Cache,
207206
) (*cache.Cache, error) {
@@ -220,7 +219,7 @@ func createCache(
220219
}
221220

222221
c, err := cache.New(
223-
logger.WithContext(ctx),
222+
ctx,
224223
cmd.String("cache-hostname"),
225224
db,
226225
localStore,
@@ -231,7 +230,7 @@ func createCache(
231230
return nil, fmt.Errorf("error creating a new cache: %w", err)
232231
}
233232

234-
c.AddUpstreamCaches(ucs...)
233+
c.AddUpstreamCaches(ctx, ucs...)
235234

236235
if cmd.String("cache-lru-schedule") == "" {
237236
return c, nil
@@ -247,7 +246,8 @@ func createCache(
247246
return nil, fmt.Errorf("error parsing the size: %w", err)
248247
}
249248

250-
logger.Info().
249+
zerolog.Ctx(ctx).
250+
Info().
251251
Uint64("max-size", maxSize).
252252
Msg("setting up the cache max-size")
253253

@@ -262,20 +262,21 @@ func createCache(
262262
}
263263
}
264264

265-
logger.Info().
265+
zerolog.Ctx(ctx).
266+
Info().
266267
Str("time-zone", loc.String()).
267268
Msg("setting up the cache timezone location")
268269

269-
c.SetupCron(loc)
270+
c.SetupCron(ctx, loc)
270271

271272
schedule, err := cron.ParseStandard(cmd.String("cache-lru-schedule"))
272273
if err != nil {
273274
return nil, fmt.Errorf("error parsing the cron spec %q: %w", cmd.String("cache-lru-schedule"), err)
274275
}
275276

276-
c.AddLRUCronJob(schedule)
277+
c.AddLRUCronJob(ctx, schedule)
277278

278-
c.StartCron()
279+
c.StartCron(ctx)
279280

280281
return c, nil
281282
}

0 commit comments

Comments
 (0)