filtering: slog

This commit is contained in:
Dimitry Kolyshev 2025-06-30 15:16:58 +04:00
parent 9bc1abfac1
commit de62c55be1
3 changed files with 14 additions and 9 deletions

View File

@ -1041,7 +1041,7 @@ func New(c *Config, blockFilters []Filter) (d *DNSFilter, err error) {
d = &DNSFilter{
logger: c.Logger,
idGen: newIDGenerator(int32(time.Now().Unix())),
idGen: newIDGenerator(int32(time.Now().Unix()), c.Logger),
bufPool: syncutil.NewSlicePool[byte](rulelist.DefaultRuleBufSize),
safeSearch: c.SafeSearch,
refreshLock: &sync.Mutex{},

View File

@ -1,12 +1,13 @@
package filtering
import (
"context"
"fmt"
"log/slog"
"sync/atomic"
"github.com/AdguardTeam/AdGuardHome/internal/filtering/rulelist"
"github.com/AdguardTeam/golibs/container"
"github.com/AdguardTeam/golibs/log"
)
// idGenerator generates filtering-list IDs in a way broadly compatible with the
@ -16,13 +17,15 @@ import (
// rule-list architecture.
type idGenerator struct {
current *atomic.Int32
logger *slog.Logger
}
// newIDGenerator returns a new ID generator initialized with the given seed
// value.
func newIDGenerator(seed int32) (g *idGenerator) {
func newIDGenerator(seed int32, l *slog.Logger) (g *idGenerator) {
g = &idGenerator{
current: &atomic.Int32{},
logger: l,
}
g.current.Store(seed)
@ -61,11 +64,12 @@ func (g *idGenerator) fix(flts []FilterYAML) {
newID = g.next()
}
log.Info(
"filtering: warning: filter at index %d has duplicate id %d; reassigning to %d",
i,
id,
newID,
g.logger.WarnContext(
context.TODO(),
"filter has duplicate id; reassigning",
"idx", i,
"id", id,
"new_id", newID,
)
flts[i].ID = newID

View File

@ -5,6 +5,7 @@ import (
"github.com/AdguardTeam/AdGuardHome/internal/aghalg"
"github.com/AdguardTeam/AdGuardHome/internal/filtering/rulelist"
"github.com/AdguardTeam/golibs/logutil/slogutil"
"github.com/stretchr/testify/assert"
)
@ -64,7 +65,7 @@ func TestIDGenerator_Fix(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
g := newIDGenerator(1)
g := newIDGenerator(1, slogutil.NewDiscardLogger())
g.fix(tc.in)
assertUniqueIDs(t, tc.in)