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/importer | |
parent | f076b5bd6cb82edd99be50f3dbdd39bb9be2c44e (diff) | |
download | searchix-94b21b286edff37496a2fe481963625ac01c30a1.tar.lz searchix-94b21b286edff37496a2fe481963625ac01c30a1.tar.zst searchix-94b21b286edff37496a2fe481963625ac01c30a1.zip |
feat: more structured logging
Diffstat (limited to 'internal/importer')
-rw-r--r-- | internal/importer/importer.go | 4 | ||||
-rw-r--r-- | internal/importer/main.go | 26 | ||||
-rw-r--r-- | internal/importer/main_test.go | 7 | ||||
-rw-r--r-- | internal/importer/options.go | 20 | ||||
-rw-r--r-- | internal/importer/package.go | 9 |
5 files changed, 45 insertions, 21 deletions
diff --git a/internal/importer/importer.go b/internal/importer/importer.go index 31d13c1..99f7e7a 100644 --- a/internal/importer/importer.go +++ b/internal/importer/importer.go @@ -2,11 +2,11 @@ package importer import ( "context" - "log/slog" "sync" "go.alanpearce.eu/searchix/internal/index" "go.alanpearce.eu/searchix/internal/nix" + "go.alanpearce.eu/x/log" ) type Importer interface { @@ -21,7 +21,7 @@ func process( ctx context.Context, indexer *index.WriteIndex, processor Processor, - logger *slog.Logger, + logger *log.Logger, ) (bool, error) { wg := sync.WaitGroup{} diff --git a/internal/importer/main.go b/internal/importer/main.go index bbd8b6c..4c66501 100644 --- a/internal/importer/main.go +++ b/internal/importer/main.go @@ -3,7 +3,6 @@ package importer import ( "context" "fmt" - "log/slog" "os/exec" "slices" "strings" @@ -12,18 +11,20 @@ import ( "go.alanpearce.eu/searchix/internal/config" "go.alanpearce.eu/searchix/internal/fetcher" "go.alanpearce.eu/searchix/internal/index" + "go.alanpearce.eu/x/log" "github.com/pkg/errors" ) func createSourceImporter( parent context.Context, + log *log.Logger, meta *index.Meta, indexer *index.WriteIndex, forceUpdate bool, ) func(*config.Source) error { return func(source *config.Source) error { - logger := slog.With( + logger := log.With( "name", source.Key, "fetcher", @@ -94,9 +95,17 @@ func createSourceImporter( switch source.Importer { case config.Options: logger.Debug("processor created", "file", fmt.Sprintf("%T", files.Options)) - processor, err = NewOptionProcessor(files.Options, source) + processor, err = NewOptionProcessor( + files.Options, + source, + logger.Named("processor"), + ) case config.Packages: - processor, err = NewPackageProcessor(files.Packages, source) + processor, err = NewPackageProcessor( + files.Packages, + source, + logger.Named("processor"), + ) } if err != nil { return errors.WithMessagef(err, "failed to create processor") @@ -123,17 +132,18 @@ func createSourceImporter( func Start( cfg *config.Config, + log *log.Logger, indexer *index.WriteIndex, forceUpdate bool, onlyUpdateSources *[]string, ) error { if len(cfg.Importer.Sources) == 0 { - slog.Info("No sources enabled") + log.Info("No sources enabled") return nil } - slog.Debug("starting importer", "timeout", cfg.Importer.Timeout.Duration) + log.Debug("starting importer", "timeout", cfg.Importer.Timeout.Duration) importCtx, cancelImport := context.WithTimeout( context.Background(), cfg.Importer.Timeout.Duration, @@ -144,7 +154,7 @@ func Start( meta := indexer.Meta - importSource := createSourceImporter(importCtx, meta, indexer, forceUpdate) + importSource := createSourceImporter(importCtx, log, meta, indexer, forceUpdate) for name, source := range cfg.Importer.Sources { if onlyUpdateSources != nil && len(*onlyUpdateSources) > 0 { if !slices.Contains(*onlyUpdateSources, name) { @@ -153,7 +163,7 @@ func Start( } err := importSource(source) if err != nil { - slog.Error("import failed", "source", name, "error", err) + log.Error("import failed", "source", name, "error", err) } } diff --git a/internal/importer/main_test.go b/internal/importer/main_test.go index cb4d1db..0bc438d 100644 --- a/internal/importer/main_test.go +++ b/internal/importer/main_test.go @@ -1,26 +1,27 @@ package importer import ( - "log/slog" "testing" "go.alanpearce.eu/searchix/internal/config" "go.alanpearce.eu/searchix/internal/index" + "go.alanpearce.eu/x/log" ) var cfg = config.DefaultConfig func BenchmarkImporterLowMemory(b *testing.B) { tmp := b.TempDir() - cfg.LogLevel = slog.LevelDebug + logger := log.Configure(false) _, write, _, err := index.OpenOrCreate(tmp, false, &index.Options{ LowMemory: true, + Logger: logger.Named("index"), }) if err != nil { b.Fatal(err) } - err = Start(&cfg, write, false, &[]string{"nixpkgs"}) + err = Start(&cfg, logger.Named("importer"), write, false, &[]string{"nixpkgs"}) if err != nil { b.Fatal(err) } diff --git a/internal/importer/options.go b/internal/importer/options.go index 290e2e3..763f57f 100644 --- a/internal/importer/options.go +++ b/internal/importer/options.go @@ -3,11 +3,11 @@ package importer import ( "context" "io" - "log/slog" "reflect" "go.alanpearce.eu/searchix/internal/config" "go.alanpearce.eu/searchix/internal/nix" + "go.alanpearce.eu/x/log" "github.com/bcicen/jstream" "github.com/mitchellh/mapstructure" @@ -35,7 +35,7 @@ type nixOptionJSON struct { Type string } -func convertValue(nj *nixValueJSON) *nix.Value { +func (i *OptionIngester) convertValue(nj *nixValueJSON) *nix.Value { if nj == nil { return nil } @@ -49,7 +49,7 @@ func convertValue(nj *nixValueJSON) *nix.Value { Markdown: nix.Markdown(nj.Text), } default: - slog.Warn("got unexpected Value type", "type", nj.Type, "text", nj.Text) + i.log.Warn("got unexpected Value type", "type", nj.Type, "text", nj.Text) return nil } @@ -58,14 +58,20 @@ func convertValue(nj *nixValueJSON) *nix.Value { type OptionIngester struct { dec *jstream.Decoder ms *mapstructure.Decoder + log *log.Logger optJSON nixOptionJSON infile io.ReadCloser source *config.Source } -func NewOptionProcessor(infile io.ReadCloser, source *config.Source) (*OptionIngester, error) { +func NewOptionProcessor( + infile io.ReadCloser, + source *config.Source, + log *log.Logger, +) (*OptionIngester, error) { i := OptionIngester{ dec: jstream.NewDecoder(infile, 1).EmitKV(), + log: log, optJSON: nixOptionJSON{}, infile: infile, source: source, @@ -163,14 +169,14 @@ func (i *OptionIngester) Process(ctx context.Context) (<-chan nix.Importable, <- decs[i] = nix.Link(d) } - // slog.Debug("sending option", "name", kv.Key) + // log.Debug("sending option", "name", kv.Key) results <- nix.Option{ Name: kv.Key, Source: i.source.Key, Declarations: decs, - Default: convertValue(i.optJSON.Default), + Default: i.convertValue(i.optJSON.Default), Description: nix.Markdown(i.optJSON.Description), - Example: convertValue(i.optJSON.Example), + Example: i.convertValue(i.optJSON.Example), RelatedPackages: nix.Markdown(i.optJSON.RelatedPackages), Loc: i.optJSON.Loc, Type: i.optJSON.Type, diff --git a/internal/importer/package.go b/internal/importer/package.go index 5a0ea00..80adc38 100644 --- a/internal/importer/package.go +++ b/internal/importer/package.go @@ -9,6 +9,7 @@ import ( "go.alanpearce.eu/searchix/internal/config" "go.alanpearce.eu/searchix/internal/nix" + "go.alanpearce.eu/x/log" "github.com/bcicen/jstream" "github.com/mitchellh/mapstructure" @@ -40,6 +41,7 @@ type maintainerJSON struct { type PackageIngester struct { dec *jstream.Decoder ms *mapstructure.Decoder + log *log.Logger pkg packageJSON infile io.ReadCloser source *config.Source @@ -60,9 +62,14 @@ func makeAdhocPlatform(v any) string { return string(s) } -func NewPackageProcessor(infile io.ReadCloser, source *config.Source) (*PackageIngester, error) { +func NewPackageProcessor( + infile io.ReadCloser, + source *config.Source, + log *log.Logger, +) (*PackageIngester, error) { i := &PackageIngester{ dec: jstream.NewDecoder(infile, 2).EmitKV(), + log: log, pkg: packageJSON{}, infile: infile, source: source, |