diff options
author | Alan Pearce | 2024-07-01 22:15:06 +0200 |
---|---|---|
committer | Alan Pearce | 2024-07-01 22:15:06 +0200 |
commit | 94b21b286edff37496a2fe481963625ac01c30a1 (patch) | |
tree | bdfd7803dd4eb22da785331b8547ff9858e2b0e6 /internal/index | |
parent | f076b5bd6cb82edd99be50f3dbdd39bb9be2c44e (diff) | |
download | searchix-94b21b286edff37496a2fe481963625ac01c30a1.tar.lz searchix-94b21b286edff37496a2fe481963625ac01c30a1.tar.zst searchix-94b21b286edff37496a2fe481963625ac01c30a1.zip |
feat: more structured logging
Diffstat (limited to 'internal/index')
-rw-r--r-- | internal/index/index_meta.go | 15 | ||||
-rw-r--r-- | internal/index/indexer.go | 28 | ||||
-rw-r--r-- | internal/index/search.go | 2 |
3 files changed, 27 insertions, 18 deletions
diff --git a/internal/index/index_meta.go b/internal/index/index_meta.go index fe1b26c..e67c6f2 100644 --- a/internal/index/index_meta.go +++ b/internal/index/index_meta.go @@ -2,11 +2,11 @@ package index import ( "encoding/json" - "log/slog" "os" "time" "go.alanpearce.eu/searchix/internal/file" + "go.alanpearce.eu/x/log" "github.com/pkg/errors" ) @@ -26,10 +26,11 @@ type data struct { type Meta struct { path string + log *log.Logger data } -func createMeta(path string) (*Meta, error) { +func createMeta(path string, log *log.Logger) (*Meta, error) { exists, err := file.Exists(path) if err != nil { return nil, errors.WithMessage(err, "could not check for existence of index metadata") @@ -40,19 +41,20 @@ func createMeta(path string) (*Meta, error) { return &Meta{ path: path, + log: log, data: data{ SchemaVersion: CurrentSchemaVersion, }, }, nil } -func openMeta(path string) (*Meta, error) { +func openMeta(path string, log *log.Logger) (*Meta, error) { exists, err := file.Exists(path) if err != nil { return nil, errors.WithMessage(err, "could not check for existence of index metadata") } if !exists { - return createMeta(path) + return createMeta(path, log) } j, err := os.ReadFile(path) @@ -61,6 +63,7 @@ func openMeta(path string) (*Meta, error) { } meta := Meta{ path: path, + log: log, } err = json.Unmarshal(j, &meta.data) if err != nil { @@ -74,7 +77,7 @@ func openMeta(path string) (*Meta, error) { func (i *Meta) checkSchemaVersion() { if i.SchemaVersion < CurrentSchemaVersion { - slog.Warn( + i.log.Warn( "Index schema version out of date, suggest re-indexing", "schema_version", i.SchemaVersion, @@ -90,7 +93,7 @@ func (i *Meta) Save() error { if err != nil { return errors.WithMessage(err, "could not prepare index metadata for saving") } - slog.Debug("saving index metadata", "path", i.path) + i.log.Debug("saving index metadata", "path", i.path) err = os.WriteFile(i.path, j, 0o600) if err != nil { return errors.WithMessage(err, "could not save index metadata") diff --git a/internal/index/indexer.go b/internal/index/indexer.go index 62edbc1..47701bd 100644 --- a/internal/index/indexer.go +++ b/internal/index/indexer.go @@ -5,8 +5,6 @@ import ( "context" "encoding/gob" "io/fs" - "log" - "log/slog" "math" "os" "path" @@ -14,6 +12,8 @@ import ( "go.alanpearce.eu/searchix/internal/file" "go.alanpearce.eu/searchix/internal/nix" + "go.alanpearce.eu/x/log" + "go.uber.org/zap" "github.com/blevesearch/bleve/v2" "github.com/blevesearch/bleve/v2/analysis/analyzer/custom" @@ -30,6 +30,7 @@ import ( type WriteIndex struct { index bleve.Index + log *log.Logger Meta *Meta } @@ -190,6 +191,7 @@ func deleteIndex(dataRoot string) error { type Options struct { LowMemory bool + Logger *log.Logger } func OpenOrCreate( @@ -198,7 +200,7 @@ func OpenOrCreate( options *Options, ) (*ReadIndex, *WriteIndex, bool, error) { var err error - bleve.SetLog(log.Default()) + bleve.SetLog(zap.NewStdLog(options.Logger.Named("bleve").GetLogger())) indexPath := path.Join(dataRoot, indexBaseName) metaPath := path.Join(dataRoot, metaBaseName) @@ -226,7 +228,7 @@ func OpenOrCreate( return nil, nil, false, err } - meta, err = createMeta(metaPath) + meta, err = createMeta(metaPath, options.Logger) if err != nil { return nil, nil, false, err } @@ -237,7 +239,7 @@ func OpenOrCreate( return nil, nil, exists, errors.WithMessagef(err, "could not open index at path %s", indexPath) } - meta, err = openMeta(metaPath) + meta, err = openMeta(metaPath, options.Logger) if err != nil { return nil, nil, exists, err } @@ -248,12 +250,14 @@ func OpenOrCreate( } return &ReadIndex{ - idx, - meta, + index: idx, + log: options.Logger, + meta: meta, }, &WriteIndex{ - idx, - meta, + index: idx, + log: options.Logger, + Meta: meta, }, exists, nil @@ -280,7 +284,7 @@ func (i *WriteIndex) Import( for obj := range objects { select { case <-ctx.Done(): - slog.Warn("import aborted") + i.log.Warn("import aborted") break outer default: @@ -305,7 +309,7 @@ func (i *WriteIndex) Import( field := document.NewTextFieldWithIndexingOptions("_data", nil, data.Bytes(), indexAPI.StoreField) newDoc := doc.AddField(field) - // slog.Debug("adding object to index", "name", opt.Name) + // log.Debug("adding object to index", "name", opt.Name) err = batch.IndexAdvanced(newDoc) if err != nil { @@ -340,7 +344,7 @@ func (i *WriteIndex) Flush(batch *bleve.Batch) error { error: errors.New("no documents to flush"), } } - slog.Debug("flushing batch", "size", size) + i.log.Debug("flushing batch", "size", size) err := i.index.Batch(batch) if err != nil { diff --git a/internal/index/search.go b/internal/index/search.go index dc19db4..03a8e60 100644 --- a/internal/index/search.go +++ b/internal/index/search.go @@ -7,6 +7,7 @@ import ( "go.alanpearce.eu/searchix/internal/config" "go.alanpearce.eu/searchix/internal/nix" + "go.alanpearce.eu/x/log" "github.com/blevesearch/bleve/v2" "github.com/blevesearch/bleve/v2/analysis/analyzer/standard" @@ -29,6 +30,7 @@ type Result struct { type ReadIndex struct { index bleve.Index + log *log.Logger meta *Meta } |