refactor: framework modules log through diary components
- Each module logs through diary.For(<package>): cabana, lagoon, attach, cooler, postcard, beachcomber, lighthouse, centrifugo, flare, phrasebook, wristband, bouncer - App-published *slog.Logger overrides stay first; messages and attributes unchanged - sunscreen: InstallDefault documented as the compatibility entry point; diary wraps every channel - READMEs list diary as a dependency; logging docs: plugins use diary.For
This commit is contained in:
@@ -211,7 +211,7 @@ func AllowExport(ctx context.Context, app *backpack.App, fallback cooler.Attempt
|
||||
|
||||
## Dependencies
|
||||
|
||||
- SummerCMS modules: [backpack](../backpack/README.md), [compass](../compass/README.md), [lagoon](../lagoon/README.md) (`lagoon.AfterCommit` for the after-commit purge).
|
||||
- SummerCMS modules: [backpack](../backpack/README.md), [compass](../compass/README.md), [diary](../diary/README.md) (component logger `cooler` when the app publishes no logger), [lagoon](../lagoon/README.md) (`lagoon.AfterCommit` for the after-commit purge).
|
||||
- Third-party: `github.com/redis/go-redis/v9` (Redis client), `github.com/maypok86/otter/v2` (memory driver).
|
||||
- Third-party (purge): `gorm.io/gorm` (the save transaction handle).
|
||||
- Standard library: `bytes`, `context`, `encoding/json`, `errors`, `fmt`, `io`, `log/slog`, `net`, `net/http`, `net/url`, `strconv`, `strings`, `sync`, `time`.
|
||||
|
||||
@@ -22,9 +22,14 @@ import (
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/backpack"
|
||||
"git.golem15.com/golem15/summercms/modules/compass"
|
||||
"git.golem15.com/golem15/summercms/modules/diary"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// moduleLog is cooler's logger: records carry component=cooler and follow
|
||||
// log.components.cooler (diary).
|
||||
var moduleLog = diary.For("cooler")
|
||||
|
||||
// Config keys and defaults cooler reads.
|
||||
const (
|
||||
// RedisURLKey is the shared Redis URL (redis://, rediss:// or unix://),
|
||||
@@ -387,7 +392,7 @@ func loggerFromApp(app *backpack.App) *slog.Logger {
|
||||
return log
|
||||
}
|
||||
}
|
||||
return slog.Default()
|
||||
return moduleLog
|
||||
}
|
||||
|
||||
// errNilClient is returned when a Redis driver is built without a client.
|
||||
@@ -428,7 +433,7 @@ func (w *warner) warn(msg string, args ...any) {
|
||||
log := w.log
|
||||
w.mu.Unlock()
|
||||
if log == nil {
|
||||
log = slog.Default()
|
||||
log = moduleLog
|
||||
}
|
||||
log.Warn(msg, args...)
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ func NewMemoryCache(prefix string, maxEntries int) (Cache, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cooler: memory cache: %w", err)
|
||||
}
|
||||
return &memoryCache{c: c, prefix: prefix, warn: newWarner(slog.Default())}, nil
|
||||
return &memoryCache{c: c, prefix: prefix, warn: newWarner(moduleLog)}, nil
|
||||
}
|
||||
|
||||
func (m *memoryCache) key(k string) string { return m.prefix + "cache:" + k }
|
||||
|
||||
@@ -3,7 +3,6 @@ package cooler
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
@@ -49,7 +48,7 @@ type RateLimitStore struct {
|
||||
// <prefix>rl:<key> on client. fallback answers while Redis fails; surf passes
|
||||
// a surf.MemoryStore. A nil fallback admits every request during an outage.
|
||||
func NewRateLimitStore(client *redis.Client, prefix string, fallback Attempter) *RateLimitStore {
|
||||
return &RateLimitStore{client: client, prefix: prefix, fallback: fallback, warn: newWarner(slog.Default())}
|
||||
return &RateLimitStore{client: client, prefix: prefix, fallback: fallback, warn: newWarner(moduleLog)}
|
||||
}
|
||||
|
||||
// Attempt admits and counts one request when key is below max, in one
|
||||
|
||||
@@ -24,7 +24,7 @@ type redisCache struct {
|
||||
// NewRedisCache returns a Cache on client with keys <prefix>cache:<key>.
|
||||
// It does not dial; the first call does.
|
||||
func NewRedisCache(client *redis.Client, prefix string) Cache {
|
||||
return &redisCache{client: client, prefix: prefix, warn: newWarner(slog.Default())}
|
||||
return &redisCache{client: client, prefix: prefix, warn: newWarner(moduleLog)}
|
||||
}
|
||||
|
||||
func (r *redisCache) key(k string) string { return r.prefix + "cache:" + k }
|
||||
|
||||
@@ -158,7 +158,7 @@ func (p *SiteCachePurger) send(ctx context.Context, kind, slug string, coalesced
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), SiteCachePurgeTimeout)
|
||||
defer cancel()
|
||||
log := slog.Default()
|
||||
log := moduleLog
|
||||
failed := func(attrs ...any) {
|
||||
attrs = append([]any{"kind", kind, "slug", slug}, attrs...)
|
||||
if coalesced > 0 {
|
||||
|
||||
Reference in New Issue
Block a user