diff options
author | Arthur Zamarin <arthurzam@gentoo.org> | 2024-03-16 12:56:39 +0200 |
---|---|---|
committer | Arthur Zamarin <arthurzam@gentoo.org> | 2024-03-16 12:56:39 +0200 |
commit | 27a7a225ed482bbe74a30f42a03ba7af4625c8db (patch) | |
tree | 6ef45d46035bbe23df705cf83001a391138984f8 | |
parent | use newer templ for simpler pull requests badges (diff) | |
download | soko-27a7a225ed482bbe74a30f42a03ba7af4625c8db.tar.gz soko-27a7a225ed482bbe74a30f42a03ba7af4625c8db.tar.bz2 soko-27a7a225ed482bbe74a30f42a03ba7af4625c8db.zip |
use slog for logging
Signed-off-by: Arthur Zamarin <arthurzam@gentoo.org>
32 files changed, 305 insertions, 382 deletions
@@ -10,7 +10,9 @@ require ( github.com/go-pg/pg/v10 v10.12.0 github.com/gorilla/feeds v1.1.2 github.com/jasonlvhit/gocron v0.0.1 + github.com/lmittmann/tint v1.0.4 github.com/prometheus/client_golang v1.18.0 + github.com/samber/slog-multi v1.0.2 github.com/ulikunitz/xz v0.5.11 github.com/vektah/gqlparser/v2 v2.5.11 golang.org/x/time v0.5.0 @@ -41,6 +43,7 @@ require ( github.com/prometheus/client_model v0.6.0 // indirect github.com/prometheus/common v0.47.0 // indirect github.com/prometheus/procfs v0.12.0 // indirect + github.com/samber/lo v1.39.0 // indirect github.com/sergi/go-diff v1.3.1 // indirect github.com/skeema/knownhosts v1.2.1 // indirect github.com/sosodev/duration v1.2.0 // indirect @@ -51,6 +54,7 @@ require ( github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect github.com/xanzy/ssh-agent v0.3.3 // indirect golang.org/x/crypto v0.19.0 // indirect + golang.org/x/exp v0.0.0-20240314144324-c7f7c6466f7f // indirect golang.org/x/mod v0.15.0 // indirect golang.org/x/net v0.21.0 // indirect golang.org/x/sys v0.17.0 // indirect @@ -87,6 +87,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/lmittmann/tint v1.0.4 h1:LeYihpJ9hyGvE0w+K2okPTGUdVLfng1+nDNVR4vWISc= +github.com/lmittmann/tint v1.0.4/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78= @@ -114,6 +116,10 @@ github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= +github.com/samber/lo v1.39.0 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA= +github.com/samber/lo v1.39.0/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= +github.com/samber/slog-multi v1.0.2 h1:6BVH9uHGAsiGkbbtQgAOQJMpKgV8unMrHhhJaw+X1EQ= +github.com/samber/slog-multi v1.0.2/go.mod h1:uLAvHpGqbYgX4FSL0p1ZwoLuveIAJvBECtE07XmYvFo= github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8= github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I= github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= @@ -151,6 +157,8 @@ golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2Uz golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo= golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= +golang.org/x/exp v0.0.0-20240314144324-c7f7c6466f7f h1:3CW0unweImhOzd5FmYuRsD4Y4oQFKZIjAnKbjV4WIrw= +golang.org/x/exp v0.0.0-20240314144324-c7f7c6466f7f/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8= diff --git a/pkg/app/handler/packages/utils.go b/pkg/app/handler/packages/utils.go index 82d6ede..5a3af7d 100644 --- a/pkg/app/handler/packages/utils.go +++ b/pkg/app/handler/packages/utils.go @@ -3,9 +3,9 @@ package packages import ( + "log/slog" "net/http" "soko/pkg/database" - "soko/pkg/logger" "soko/pkg/models" "sort" "strings" @@ -23,7 +23,7 @@ func GetAddedPackages(n int) (addedPackages []*models.Package) { Relation("Versions.Commits"). Select() if err != nil && err != pg.ErrNoRows { - logger.Error.Println("Error during fetching added packages from database", err) + slog.Error("Failed fetching added packages from database", slog.Any("err", err)) } return } @@ -161,7 +161,7 @@ func getPackageUseflags(gpackage *models.Package) ([]models.Useflag, []models.Us Order("name ASC"). Select() if err != nil && err != pg.ErrNoRows { - logger.Error.Println("Error during fetching added packages from database", err) + slog.Error("Failed fetching use flags", slog.Any("err", err)) return localUseflags, allGlobalUseflags, useExpands } diff --git a/pkg/app/serve.go b/pkg/app/serve.go index c685073..1f5f25a 100644 --- a/pkg/app/serve.go +++ b/pkg/app/serve.go @@ -3,9 +3,10 @@ package app import ( - "log" + "log/slog" "net/http" _ "net/http/pprof" + "os" "soko/pkg/api/graphql/generated" "soko/pkg/api/graphql/graphiql" "soko/pkg/api/graphql/resolvers" @@ -19,7 +20,6 @@ import ( "soko/pkg/app/handler/user" "soko/pkg/config" "soko/pkg/database" - "soko/pkg/logger" "github.com/99designs/gqlgen/graphql/handler" "github.com/99designs/gqlgen/graphql/handler/extension" @@ -134,8 +134,11 @@ func Serve() { // graphiql: api explorer setRoute("/api/explore/", graphiql.Show) - logger.Info.Println("Serving on port: " + config.Port()) - log.Fatal(http.ListenAndServe(":"+config.Port(), nil)) + address := ":" + config.Port() + slog.Info("Serving HTTP", "address", address) + err := http.ListenAndServe(address, nil) + slog.Error("exited server", "err", err) + os.Exit(1) } // define a route using the default middleware and the given handler diff --git a/pkg/app/utils/applicationdata.go b/pkg/app/utils/applicationdata.go index de937ef..db27f63 100644 --- a/pkg/app/utils/applicationdata.go +++ b/pkg/app/utils/applicationdata.go @@ -3,8 +3,8 @@ package utils import ( + "log/slog" "soko/pkg/database" - "soko/pkg/logger" "soko/pkg/models" "time" ) @@ -16,7 +16,7 @@ func GetApplicationData() models.Application { applicationData := &models.Application{Id: "latest"} err := database.DBCon.Model(applicationData).WherePK().Select() if err != nil { - logger.Error.Println("Error fetching application data") + slog.Error("Failed fetching application data", slog.Any("err", err)) return models.Application{ Id: "latest", LastUpdate: time.Now(), diff --git a/pkg/config/config.go b/pkg/config/config.go index b27d117..3bd651f 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -37,12 +37,12 @@ func DevMode() string { return getEnv("SOKO_DEVMODE", "false") } -func Debug() string { - return getEnv("SOKO_DEBUG", "false") +func Debug() bool { + return getEnv("SOKO_DEBUG", "false") == "true" } -func Quiet() string { - return getEnv("SOKO_QUIET", "false") +func Quiet() bool { + return getEnv("SOKO_QUIET", "false") == "true" } func LogFile() string { diff --git a/pkg/database/connection.go b/pkg/database/connection.go index 3e63633..82f2377 100644 --- a/pkg/database/connection.go +++ b/pkg/database/connection.go @@ -4,9 +4,9 @@ package database import ( "context" - "log" + "log/slog" + "os" "soko/pkg/config" - "soko/pkg/logger" "soko/pkg/models" "github.com/go-pg/pg/v10" @@ -53,13 +53,14 @@ func CreateSchema() error { IfNotExists: true, }) if err != nil { - logger.Error.Printf("Failed creating table %T, err: %s", model, err) + tableName := string(DBCon.Model(model).TableModel().Table().TypeName) + slog.Error("Failed creating table", slog.String("table", tableName), slog.Any("err", err)) return err } } _, err := DBCon.Exec("CREATE EXTENSION IF NOT EXISTS pg_trgm") if err != nil { - logger.Error.Println("Failed creating extension, err:", err) + slog.Error("Failed creating extension 'pg_trgm'", slog.Any("err", err)) return err } @@ -76,7 +77,7 @@ func (d dbLogger) BeforeQuery(c context.Context, q *pg.QueryEvent) (context.Cont func (d dbLogger) AfterQuery(c context.Context, q *pg.QueryEvent) error { query, err := q.FormattedQuery() if err == nil { - logger.Debug.Println(string(query)) + slog.Debug(string(query)) } return nil } @@ -91,32 +92,24 @@ func Connect() { Addr: config.PostgresHost() + ":" + config.PostgresPort(), }) - DBCon.AddQueryHook(dbLogger{}) + if !config.Quiet() { + DBCon.AddQueryHook(dbLogger{}) + } err := CreateSchema() if err != nil { - logger.Error.Println("ERROR: Could not create database schema") - logger.Error.Println(err) - log.Fatalln(err) + slog.Error("Failed creating database schema", slog.Any("err", err)) + os.Exit(1) } - } -func TruncateTable[K any](primary string) { - var val K - var allRows []*K - err := DBCon.Model(&allRows).Column(primary).Select() - if err != nil { - logger.Error.Println(err) - return - } else if len(allRows) == 0 { - logger.Info.Printf("No %T to delete from the database", val) - return - } - res, err := DBCon.Model(&allRows).Delete() +func TruncateTable[K any](_ string) { + query := DBCon.Model((*K)(nil)) + tableName := string(query.TableModel().Table().TypeName) + _, err := query.Exec("TRUNCATE TABLE ?TableName") if err != nil { - logger.Error.Println(err) - return + slog.Error("Failed truncating table", slog.String("table", tableName), slog.Any("err", err)) + } else { + slog.Info("Truncated table", slog.String("table", tableName)) } - logger.Info.Printf("Deleted %d %T from the database", res.RowsAffected(), val) } diff --git a/pkg/logger/loggers.go b/pkg/logger/loggers.go deleted file mode 100644 index 0ef51e9..0000000 --- a/pkg/logger/loggers.go +++ /dev/null @@ -1,39 +0,0 @@ -package logger - -import ( - "io" - "log" - "os" -) - -var ( - Debug *log.Logger - Info *log.Logger - Error *log.Logger -) - -func Init( - debugHandle io.Writer, - infoHandle io.Writer, - errorHandle io.Writer) { - - Debug = log.New(debugHandle, - "DEBUG: ", - log.Ldate|log.Ltime|log.Lshortfile) - - Info = log.New(infoHandle, - "INFO: ", - log.Ldate|log.Ltime|log.Lshortfile) - - Error = log.New(errorHandle, - "ERROR: ", - log.Ldate|log.Ltime|log.Lshortfile) -} - -func CreateLogFile(path string) *os.File { - f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) - if err != nil { - log.Println(err) - } - return f -} diff --git a/pkg/portage/bugs/bugs.go b/pkg/portage/bugs/bugs.go index 1d491f6..3887803 100644 --- a/pkg/portage/bugs/bugs.go +++ b/pkg/portage/bugs/bugs.go @@ -2,12 +2,12 @@ package bugs import ( "encoding/json" + "log/slog" "net/http" "net/url" "regexp" "soko/pkg/config" "soko/pkg/database" - "soko/pkg/logger" "soko/pkg/models" "soko/pkg/portage/utils" "strconv" @@ -53,7 +53,7 @@ func UpdateBugs() { } err := database.DBCon.Model(&update).WherePK().Select() if err != nil && err != pg.ErrNoRows { - logger.Error.Println("Error:", err) + slog.Error("Failed to fetch last update time for bugs", slog.Any("err", err)) return } if update.LastCommit != "" { @@ -92,7 +92,7 @@ func fetchBugs(changedSince *time.Time, bugStatus []string) (bugs []restAPIBug, } for offset := 0; ; offset += limit { - logger.Info.Println("Importing bugs from bugs.gentoo.org:", offset, "to", offset+limit) + slog.Info("Importing bugs from bugs.gentoo.org", slog.Int("start", offset), slog.Int("end", offset+limit)) params.Set("offset", strconv.Itoa(offset)) resp, err := http.Get("https://bugs.gentoo.org/rest/bug?" + params.Encode()) if err != nil { @@ -101,7 +101,7 @@ func fetchBugs(changedSince *time.Time, bugStatus []string) (bugs []restAPIBug, defer resp.Body.Close() if resp.StatusCode != http.StatusOK { - logger.Info.Println("Not 200") + slog.Error("Failed to fetch bugs", slog.Int("status", resp.StatusCode)) return bugs, nil } @@ -110,7 +110,7 @@ func fetchBugs(changedSince *time.Time, bugStatus []string) (bugs []restAPIBug, } err = json.NewDecoder(resp.Body).Decode(&response) if err != nil { - logger.Info.Println("Error:", err) + slog.Error("Failed to decode bugs", slog.Any("err", err)) return bugs, nil } @@ -120,14 +120,16 @@ func fetchBugs(changedSince *time.Time, bugStatus []string) (bugs []restAPIBug, break } } - logger.Info.Println("Collected", len(bugs), "bugs") + slog.Info("Collected bugs", slog.Int("count", len(bugs))) return } func importAllOpenBugs() { bugs, err := fetchBugs(nil, []string{"UNCONFIRMED", "CONFIRMED", "IN_PROGRESS"}) if err != nil { - logger.Error.Println(err) + slog.Error("Failed to fetch bugs", + slog.String("status", "UNCONFIRMED, CONFIRMED, IN_PROGRESS"), + slog.Any("err", err)) return } @@ -141,7 +143,10 @@ func importAllOpenBugs() { func updateChangedBugs(changedSince time.Time) { bugs, err := fetchBugs(&changedSince, []string{"UNCONFIRMED", "CONFIRMED", "IN_PROGRESS", "RESOLVED"}) if err != nil { - logger.Error.Println(err) + slog.Error("Failed to fetch bugs", + slog.String("status", "UNCONFIRMED, CONFIRMED, IN_PROGRESS, RESOLVED"), + slog.Time("changed_since", changedSince), + slog.Any("err", err)) return } processApiBugs(bugs) @@ -193,44 +198,50 @@ func processApiBugs(bugs []restAPIBug) { res1, err := database.DBCon.Model(&dbBugs).OnConflict("(id) DO UPDATE").Insert() if err != nil { - logger.Error.Println("Failed to insert bugs:", err) + slog.Error("Failed to insert bugs", slog.Any("err", err)) return } res2, err := database.DBCon.Model(&verBugs).OnConflict("(id) DO UPDATE").Insert() if err != nil { - logger.Error.Println("Failed to insert version bugs:", err) + slog.Error("Failed to insert version bugs", slog.Any("err", err)) return } res3, err := database.DBCon.Model(&pkgsBugs).OnConflict("(id) DO UPDATE").Insert() if err != nil { - logger.Error.Println("Failed to insert package bugs:", err) + slog.Error("Failed to insert package bugs", slog.Any("err", err)) return } - logger.Info.Println("Inserted", res1.RowsAffected(), "bugs,", res2.RowsAffected(), "version bugs and", res3.RowsAffected(), "package bugs") + slog.Info("Inserted", + slog.Int("bugs", res1.RowsAffected()), + slog.Int("version_bugs", res2.RowsAffected()), + slog.Int("package_bugs", res3.RowsAffected())) if len(resolvedBugs) > 0 { res1, err := database.DBCon.Model((*models.Bug)(nil)).WhereIn("id IN (?)", resolvedBugs).Delete() if err != nil { - logger.Error.Println("Failed to delete bugs:", err) + slog.Error("Failed to delete bugs", slog.Any("err", err)) return } res2, err := database.DBCon.Model((*models.PackageToBug)(nil)).WhereIn("bug_id IN (?)", resolvedBugs).Delete() if err != nil { - logger.Error.Println("Failed to delete package bugs:", err) + slog.Error("Failed to delete package bugs", slog.Any("err", err)) return } res3, err := database.DBCon.Model((*models.VersionToBug)(nil)).WhereIn("bug_id IN (?)", resolvedBugs).Delete() if err != nil { - logger.Error.Println("Failed to delete package bugs:", err) + slog.Error("Failed to delete version bugs", slog.Any("err", err)) return } - logger.Info.Println("Deleted", res1.RowsAffected(), "bugs and", res2.RowsAffected(), "package bugs and", res3.RowsAffected(), "version bugs") + slog.Info("Deleted", + slog.Int("bugs", res1.RowsAffected()), + slog.Int("package_bugs", res2.RowsAffected()), + slog.Int("version_bugs", res3.RowsAffected())) } } @@ -267,7 +278,7 @@ func updateCategoriesInfo() { GroupExpr("SPLIT_PART(package_atom, '/', 1)"). Select(&categoriesInfoArr) if err != nil { - logger.Error.Println("Error while parsing bugs data. Aborting...", err) + slog.Error("Failed collecting bugs stats", slog.Any("err", err)) return } categoriesInfo := make(map[string]*models.CategoryPackagesInformation, len(categoriesInfoArr)) @@ -280,7 +291,7 @@ func updateCategoriesInfo() { var categories []*models.CategoryPackagesInformation err = database.DBCon.Model(&categories).Column("name").Select() if err != nil { - logger.Error.Println("Error while fetching categories packages information", err) + slog.Error("Failed fetching categories packages information", slog.Any("err", err)) return } else if len(categories) > 0 { for _, category := range categories { @@ -298,7 +309,7 @@ func updateCategoriesInfo() { Set("security_bugs = ?security_bugs"). Update() if err != nil { - logger.Error.Println("Error while updating categories packages information", err) + slog.Error("Failed updating categories packages information", slog.Any("err", err)) } categories = make([]*models.CategoryPackagesInformation, 0, len(categoriesInfo)) } @@ -309,7 +320,7 @@ func updateCategoriesInfo() { if len(categories) > 0 { _, err = database.DBCon.Model(&categories).Insert() if err != nil { - logger.Error.Println("Error while inserting categories packages information", err) + slog.Error("Failed inserting categories packages information", slog.Any("err", err)) } } } diff --git a/pkg/portage/dependencies/dependency.go b/pkg/portage/dependencies/dependency.go index 18d7a39..6530802 100644 --- a/pkg/portage/dependencies/dependency.go +++ b/pkg/portage/dependencies/dependency.go @@ -4,11 +4,11 @@ import ( "archive/tar" "fmt" "io" + "log/slog" "net/http" "regexp" "soko/pkg/config" "soko/pkg/database" - "soko/pkg/logger" "soko/pkg/models" "strings" "time" @@ -19,7 +19,6 @@ import ( var Dependencies []*models.ReverseDependency func FullPackageDependenciesUpdate() { - database.Connect() defer database.DBCon.Close() @@ -28,16 +27,16 @@ func FullPackageDependenciesUpdate() { return } - logger.Info.Println("Got", dependencyCounter, "dependencies.") + slog.Info("collected dependencies", slog.Int("count", dependencyCounter)) database.TruncateTable[models.ReverseDependency]("id") // because we removed all previous rows in table, we aren't concerned about // duplicates, so we can use bulk insert res, err := database.DBCon.Model(&Dependencies).Insert() if err != nil { - logger.Error.Println("Error during inserting dependencies", err) + slog.Error("Error during inserting dependencies", slog.Any("err", err)) } else { - logger.Info.Println("Inserted", res.RowsAffected(), "dependencies") + slog.Info("Inserted dependencies", slog.Int("rows", res.RowsAffected())) } updateStatus() @@ -50,19 +49,19 @@ func UpdateDependencies() (int, error) { resp, err := client.Get("https://qa-reports.gentoo.org/output/genrdeps/rdeps.tar.xz") if err != nil { - logger.Error.Println(err) + slog.Error("Failed fetching dependencies", slog.Any("err", err)) return 0, err } defer resp.Body.Close() if resp.StatusCode != 200 { - logger.Error.Printf("status code: %d", resp.StatusCode) + slog.Error("Got bad status code", slog.Int("code", resp.StatusCode)) return 0, fmt.Errorf("status code: %d", resp.StatusCode) } xz, err := xz.NewReader(resp.Body) if err != nil { - logger.Error.Println(err) + slog.Error("Failed decompressing dependencies", slog.Any("err", err)) return 0, err } @@ -74,7 +73,7 @@ func UpdateDependencies() (int, error) { break // end of tar archive } if err != nil { - logger.Error.Println(err) + slog.Error("Failed reading dependencies tar", slog.Any("err", err)) return 0, err } switch hdr.Typeflag { @@ -83,10 +82,9 @@ func UpdateDependencies() (int, error) { rawResponse, err := io.ReadAll(tr) if err != nil { - logger.Error.Println(err) + slog.Error("Failed reading file from tar", slog.Any("err", err)) return 0, err } - parseDependencies(string(rawResponse), nameParts[1], nameParts[0]) dependencyCounter++ } @@ -95,10 +93,7 @@ func UpdateDependencies() (int, error) { } func parseDependencies(rawResponse, atom, kind string) { - rawDependencies := strings.Split(rawResponse, "\n") - - for _, rawDependency := range rawDependencies { - + for _, rawDependency := range strings.Split(rawResponse, "\n") { dependencyParts := strings.Split(rawDependency, ":") if strings.TrimSpace(dependencyParts[0]) == "" { @@ -118,9 +113,7 @@ func parseDependencies(rawResponse, atom, kind string) { ReverseDependencyVersion: dependencyParts[0], Condition: condition, }) - } - } func versionSpecifierToPackageAtom(versionSpecifier string) string { diff --git a/pkg/portage/github/pullrequests.go b/pkg/portage/github/pullrequests.go index 05cf79d..71748ec 100644 --- a/pkg/portage/github/pullrequests.go +++ b/pkg/portage/github/pullrequests.go @@ -3,11 +3,10 @@ package github import ( "bytes" "encoding/json" + "log/slog" "net/http" - "soko/pkg/app/utils" "soko/pkg/config" "soko/pkg/database" - "soko/pkg/logger" "soko/pkg/models" "strconv" "strings" @@ -15,7 +14,6 @@ import ( ) func buildQuery(limit int, isOpen bool, lastUpdated, after string) map[string]string { - var lastUpdatedQuery string if lastUpdated != "" { lastUpdatedQuery = `updated:>` + lastUpdated @@ -107,7 +105,6 @@ func buildQuery(limit int, isOpen bool, lastUpdated, after string) map[string]st } func FullUpdatePullRequests() { - database.Connect() defer database.DBCon.Close() @@ -115,44 +112,30 @@ func FullUpdatePullRequests() { database.TruncateTable[models.PackageToGithubPullRequest]("id") // year of the git migration - UpdatePullRequestsAfter(true, "2015-01-01", "") - - updateStatus() -} - -func IncrementalUpdatePullRequests() { - - database.Connect() - defer database.DBCon.Close() - - lastUpdate := utils.GetApplicationData().LastUpdate.UTC().Format(time.RFC3339) - lastUpdate = strings.Split(lastUpdate, "Z")[0] + "Z" - UpdatePullRequestsAfter(false, lastUpdate, "") - // TODO --> we need to update old ent - // TODO --> delete closed pull requests + updatePullRequestsAfter(true, "2015-01-01", "") updateStatus() } -func UpdatePullRequestsAfter(isOpen bool, lastUpdated, after string) { +func updatePullRequestsAfter(isOpen bool, lastUpdated, after string) { pullRequests := make(map[int]*models.GithubPullRequest) client := &http.Client{Timeout: time.Second * 30} for { - logger.Info.Println("Requesting pull requests starting with", len(pullRequests)) + slog.Info("Requesting pull requests", slog.Int("index", len(pullRequests))) jsonData := buildQuery(100, isOpen, lastUpdated, after) jsonValue, _ := json.Marshal(jsonData) request, err := http.NewRequest(http.MethodPost, "https://api.github.com/graphql", bytes.NewBuffer(jsonValue)) if err != nil { - logger.Error.Println("Failed to query github graphql", err) + slog.Error("Failed querying github graphql", slog.Any("err", err)) return } request.Header.Set("Authorization", "bearer "+config.GithubAPIToken()) response, err := client.Do(request) if err != nil { - logger.Error.Println("The HTTP request failed with error", err) + slog.Error("The HTTP request failed", slog.Any("err", err)) return } defer response.Body.Close() @@ -160,7 +143,7 @@ func UpdatePullRequestsAfter(isOpen bool, lastUpdated, after string) { var prData models.GitHubPullRequestQueryResult err = json.NewDecoder(response.Body).Decode(&prData) if err != nil { - logger.Error.Println("Failed to parse JSON", err) + slog.Error("Failed to parse JSON", slog.Any("err", err)) return } prData.AppendPullRequest(pullRequests) @@ -175,7 +158,7 @@ func UpdatePullRequestsAfter(isOpen bool, lastUpdated, after string) { } if len(pullRequests) == 0 { - logger.Info.Println("No pull requests to insert") + slog.Info("No pull requests to insert") return } @@ -211,17 +194,17 @@ func UpdatePullRequestsAfter(isOpen bool, lastUpdated, after string) { } result, err := database.DBCon.Model(&rows).OnConflict("(id) DO UPDATE").Insert() if err != nil { - logger.Error.Println("Failed to insert pull requests", err) + slog.Error("Failed to insert pull requests", slog.Any("err", err)) return } - logger.Info.Println("Inserted", result.RowsAffected(), "pull requests") + slog.Info("Inserted pull requests", slog.Int("rows", result.RowsAffected())) result, err = database.DBCon.Model(&pkgsPullRequests).OnConflict("(id) DO UPDATE").Insert() if err != nil { - logger.Error.Println("Failed to insert packages to pull requests", err) + slog.Error("Failed to insert packages to pull requests", slog.Any("err", err)) return } - logger.Info.Println("Inserted", result.RowsAffected(), "packages to pull requests") + slog.Info("Inserted packages to pull requests", slog.Int("rows", result.RowsAffected())) updateCategoriesPullRequests(categoriesPullRequests) } @@ -230,7 +213,7 @@ func updateCategoriesPullRequests(categoriesPullRequests map[string]map[string]s var categories []*models.CategoryPackagesInformation err := database.DBCon.Model(&categories).Column("name").Select() if err != nil { - logger.Error.Println("Error while fetching categories packages information", err) + slog.Error("Failed fetching categories packages information", slog.Any("err", err)) return } else if len(categories) > 0 { for _, category := range categories { @@ -239,7 +222,7 @@ func updateCategoriesPullRequests(categoriesPullRequests map[string]map[string]s } _, err = database.DBCon.Model(&categories).Set("pull_requests = ?pull_requests").Update() if err != nil { - logger.Error.Println("Error while fetching categories packages information", err) + slog.Error("Failed updating categories packages information", slog.Any("err", err)) } categories = make([]*models.CategoryPackagesInformation, 0, len(categoriesPullRequests)) } @@ -253,7 +236,7 @@ func updateCategoriesPullRequests(categoriesPullRequests map[string]map[string]s if len(categories) > 0 { _, err = database.DBCon.Model(&categories).Insert() if err != nil { - logger.Error.Println("Error while inserting categories packages information", err) + slog.Error("Failed inserting categories packages information", slog.Any("err", err)) } } } diff --git a/pkg/portage/maintainers/update.go b/pkg/portage/maintainers/update.go index f518914..46aaf32 100644 --- a/pkg/portage/maintainers/update.go +++ b/pkg/portage/maintainers/update.go @@ -1,9 +1,9 @@ package maintainers import ( + "log/slog" "soko/pkg/config" "soko/pkg/database" - "soko/pkg/logger" "soko/pkg/models" "strings" "time" @@ -16,7 +16,7 @@ func FullImport() { database.Connect() defer database.DBCon.Close() - logger.Info.Println("Loading all raw maintainers from the database") + slog.Info("Loading all raw maintainers from the database") var allMaintainerInformation []*models.Maintainer database.DBCon.Model((*models.Package)(nil)).ColumnExpr("jsonb_array_elements(maintainers)->>'Name' as name, jsonb_array_elements(maintainers) ->> 'Email' as email, jsonb_array_elements(maintainers) ->> 'Type' as type").Select(&allMaintainerInformation) @@ -37,7 +37,7 @@ func FullImport() { } } - logger.Info.Println("Loading all packages from the database") + slog.Info("Loading all packages from the database") var gpackages []*models.Package database.DBCon.Model(&gpackages). Relation("Outdated"). @@ -125,10 +125,10 @@ func FullImport() { } res, err := database.DBCon.Model(&rows).OnConflict("(email) DO NOTHING").Insert() if err != nil { - logger.Error.Println("Error during inserting maintainers", err) + slog.Error("Failed inserting maintainers", slog.Any("err", err)) return } - logger.Info.Println("Inserted", res.RowsAffected(), "maintainers") + slog.Info("Inserted maintainers", slog.Int("rows", res.RowsAffected())) updateStatus() } diff --git a/pkg/portage/pkgcheck/parse.go b/pkg/portage/pkgcheck/parse.go index 153a242..ae7a3d2 100644 --- a/pkg/portage/pkgcheck/parse.go +++ b/pkg/portage/pkgcheck/parse.go @@ -2,12 +2,10 @@ package pkgcheck import ( "encoding/xml" - "io" - "log" + "log/slog" "net/http" "soko/pkg/config" "soko/pkg/database" - "soko/pkg/logger" "soko/pkg/models" "time" ) @@ -30,18 +28,13 @@ type PkgCheckResult struct { // UpdatePkgCheckResults will update the database table that contains all pkgcheck results func UpdatePkgCheckResults() { - database.Connect() defer database.DBCon.Close() - if config.Quiet() == "true" { - log.SetOutput(io.Discard) - } - // get the pkg check results from qa-reports.gentoo.org pkgCheckResults, err := parseQAReport() if err != nil { - logger.Error.Println("Error while parsing qa-reports data. Aborting...") + slog.Error("Failed parsing qa-reports data", slog.Any("err", err)) return } @@ -72,10 +65,10 @@ func UpdatePkgCheckResults() { } res, err := database.DBCon.Model(&rows).OnConflict("(id) DO NOTHING").Insert() if err != nil { - logger.Error.Println("Error during inserting pkgcheck results", err) + slog.Error("Failed inserting pkgcheck results", slog.Any("err", err)) return } - logger.Info.Println("Inserted", res.RowsAffected(), "pkgcheck results") + slog.Info("Inserted pkgcheck results", slog.Int("rows", res.RowsAffected())) updateCategoriesInfo() @@ -104,7 +97,7 @@ func updateCategoriesInfo() { GroupExpr("SPLIT_PART(atom, '/', 1)"). Select(&categoriesInfoArr) if err != nil { - logger.Error.Println("Error while parsing qa-reports data. Aborting...", err) + slog.Error("Failed collecting pkgcheck results stats", slog.Any("err", err)) return } categoriesInfo := make(map[string]int, len(categoriesInfoArr)) @@ -115,7 +108,7 @@ func updateCategoriesInfo() { var categories []*models.CategoryPackagesInformation err = database.DBCon.Model(&categories).Column("name").Select() if err != nil { - logger.Error.Println("Error while fetching categories packages information", err) + slog.Error("Failed fetching categories packages information", slog.Any("err", err)) return } else if len(categories) > 0 { for _, category := range categories { @@ -124,7 +117,7 @@ func updateCategoriesInfo() { } _, err = database.DBCon.Model(&categories).Set("stable_requests = ?stable_requests").Update() if err != nil { - logger.Error.Println("Error while fetching categories packages information", err) + slog.Error("Failed updating categories packages information", slog.Any("err", err)) } categories = make([]*models.CategoryPackagesInformation, 0, len(categoriesInfo)) } @@ -138,7 +131,7 @@ func updateCategoriesInfo() { if len(categories) > 0 { _, err = database.DBCon.Model(&categories).Insert() if err != nil { - logger.Error.Println("Error while inserting categories packages information", err) + slog.Error("Error while inserting categories packages information", slog.Any("err", err)) } } } diff --git a/pkg/portage/projects/update.go b/pkg/portage/projects/update.go index c17b22f..30fe7a8 100644 --- a/pkg/portage/projects/update.go +++ b/pkg/portage/projects/update.go @@ -2,12 +2,10 @@ package projects import ( "encoding/xml" - "io" - "log" + "log/slog" "net/http" "soko/pkg/config" "soko/pkg/database" - "soko/pkg/logger" "soko/pkg/models" "time" ) @@ -17,14 +15,10 @@ func UpdateProjects() { database.Connect() defer database.DBCon.Close() - if config.Quiet() == "true" { - log.SetOutput(io.Discard) - } - // get projects from api.gentoo.org projectList, err := parseProjectList() if err != nil { - logger.Error.Println("Error while parsing project list", err) + slog.Error("Error while parsing project list", slog.Any("err", err)) return } @@ -52,11 +46,13 @@ func UpdateProjects() { // insert new project list _, err = database.DBCon.Model(&projectList).Insert() if err != nil { - logger.Error.Println("Error while inserting project list", err) + slog.Error("Error while inserting project list", slog.Any("err", err)) + return } _, err = database.DBCon.Model(&members).Insert() if err != nil { - logger.Error.Println("Error while inserting project members", err) + slog.Error("Error while inserting project members", slog.Any("err", err)) + return } updateStatus() diff --git a/pkg/portage/repology/outdated.go b/pkg/portage/repology/outdated.go index 60dc17a..fbdb280 100644 --- a/pkg/portage/repology/outdated.go +++ b/pkg/portage/repology/outdated.go @@ -5,12 +5,10 @@ import ( "context" "encoding/json" "fmt" - "io" - "log" + "log/slog" "net/http" "soko/pkg/config" "soko/pkg/database" - "soko/pkg/logger" "soko/pkg/models" "strings" "time" @@ -33,14 +31,9 @@ var clientRateLimiter = rate.NewLimiter(rate.Every(2*time.Second), 1) // UpdateOutdated will update the database table that contains all outdated gentoo versions func UpdateOutdated() { - database.Connect() defer database.DBCon.Close() - if config.Quiet() == "true" { - log.SetOutput(io.Discard) - } - // Get all outdated Versions outdatedCategories := make(map[string]int) var outdatedVersions []*models.OutdatedPackages @@ -60,7 +53,7 @@ func UpdateOutdated() { var categories []*models.CategoryPackagesInformation err := database.DBCon.Model(&categories).Column("name").Select() if err != nil { - logger.Error.Println("Error while fetching categories packages information", err) + slog.Error("Failed fetching categories packages information", slog.Any("err", err)) return } else if len(categories) > 0 { for _, category := range categories { @@ -69,7 +62,7 @@ func UpdateOutdated() { } _, err = database.DBCon.Model(&categories).Set("outdated = ?outdated").Update() if err != nil { - logger.Error.Println("Error while fetching categories packages information", err) + slog.Error("Failed updating categories packages information", slog.Any("err", err)) } categories = make([]*models.CategoryPackagesInformation, 0, len(outdatedCategories)) } @@ -83,7 +76,7 @@ func UpdateOutdated() { if len(categories) > 0 { _, err = database.DBCon.Model(&categories).Insert() if err != nil { - logger.Error.Println("Error while inserting categories packages information", err) + slog.Error("Error while inserting categories packages information", slog.Any("err", err)) } } @@ -94,7 +87,7 @@ func UpdateOutdated() { func getOutdatedStartingWith(letter rune, outdatedCategories map[string]int) []*models.OutdatedPackages { repoPackages, err := parseRepologyData(letter) if err != nil { - logger.Error.Printf("Error while fetching repology data (%s): %s", string(letter), err) + slog.Error("Error while fetching repology data", slog.String("letter", string(letter)), slog.Any("err", err)) return nil } diff --git a/pkg/portage/repository/category.go b/pkg/portage/repository/category.go index d88009e..b0dfe18 100644 --- a/pkg/portage/repository/category.go +++ b/pkg/portage/repository/category.go @@ -4,11 +4,11 @@ package repository import ( "encoding/xml" + "log/slog" "os" "regexp" "soko/pkg/config" "soko/pkg/database" - "soko/pkg/logger" "soko/pkg/models" "strings" ) @@ -60,9 +60,9 @@ func UpdateCategories(paths []string) { } res, err := database.DBCon.Model(&rows).Delete() if err != nil { - logger.Error.Println("Error during deleting categories", err) + slog.Error("Failed deleting categories", slog.Any("err", err)) } else { - logger.Info.Println("Deleted", res.RowsAffected(), "categories") + slog.Info("Deleted categories", slog.Int("rows", res.RowsAffected())) } } @@ -73,9 +73,9 @@ func UpdateCategories(paths []string) { } res, err := database.DBCon.Model(&rows).OnConflict("(name) DO UPDATE").Insert() if err != nil { - logger.Error.Println("Error during updating categories", err) + slog.Error("Failed updating categories", slog.Any("err", err)) } else { - logger.Info.Println("Updated", res.RowsAffected(), "categories") + slog.Info("Updated categories", slog.Int("rows", res.RowsAffected())) } } } @@ -93,7 +93,7 @@ func updateModifiedCategory(changedFile string) *models.Category { xmlFile, err := os.Open(config.PortDir() + "/" + changedFile) if err != nil { - logger.Error.Println("Error during reading category metadata", err) + slog.Error("Failed reading category metadata", slog.String("category", changedFile), slog.Any("err", err)) return nil } defer xmlFile.Close() @@ -101,7 +101,7 @@ func updateModifiedCategory(changedFile string) *models.Category { var catMetadata CatMetadata err = xml.NewDecoder(xmlFile).Decode(&catMetadata) if err != nil { - logger.Error.Println("Error during category", changedFile, "decoding", err) + slog.Error("Error decoding category", slog.String("category", changedFile), slog.Any("err", err)) return nil } diff --git a/pkg/portage/repository/commit.go b/pkg/portage/repository/commit.go index 615009f..c0bda5b 100644 --- a/pkg/portage/repository/commit.go +++ b/pkg/portage/repository/commit.go @@ -3,13 +3,13 @@ package repository import ( + "log/slog" "os/exec" + "slices" "soko/pkg/config" "soko/pkg/database" - "soko/pkg/logger" "soko/pkg/models" "soko/pkg/portage/utils" - "strconv" "strings" "time" ) @@ -28,7 +28,7 @@ var ( // and parsing all following commits. In case no last commit is present // a full import starting with the first commit in the tree is done. func UpdateCommits() string { - logger.Info.Println("Start updating commits") + slog.Info("Start updating commits") latestCommit, precedingCommitsOffset := utils.GetLatestCommitAndPreceding() @@ -40,7 +40,7 @@ func UpdateCommits() string { } } dumpToDatabase() - logger.Info.Println("Finished updating commits") + slog.Info("Finished updating commits") return latestCommit } @@ -126,10 +126,10 @@ func processChangedFiles(PrecedingCommits, PrecedingCommitsOffset int, commitLin // logProgress logs the progress of a loop func logProgress(counter int) { if counter%1000 == 0 { - logger.Info.Println("Processed commits: " + strconv.Itoa(counter)) + slog.Info("Processed commits", slog.Int("commits", counter)) } else if counter == 1 { // The initial commit is *huge* that's why we log it as well - logger.Info.Println("Processed first commit.") + slog.Info("Processed first commit.") } } @@ -176,7 +176,7 @@ func createKeywordChange(id, path, commitLine string) { raw_lines, err := utils.Exec(config.PortDir(), "git", "show", id, "--", path) if err != nil { if exitError, ok := err.(*exec.ExitError); !ok || exitError.ExitCode() != 1 { - logger.Error.Println("Problem parsing file") + slog.Error("Failed running git show", slog.String("id", id), slog.String("path", path), slog.Any("err", err)) return } } @@ -195,11 +195,11 @@ func createKeywordChange(id, path, commitLine string) { if keywords_old != nil && keywords_new != nil { for _, keyword := range keywords_new { - if !utils.Contains(keywords_old, keyword) { + if !slices.Contains(keywords_old, keyword) { added_keywords = append(added_keywords, keyword) } - if !strings.HasPrefix(keyword, "~") && utils.Contains(keywords_old, ("~"+keyword)) { + if !strings.HasPrefix(keyword, "~") && slices.Contains(keywords_old, "~"+keyword) { stabilized_keywords = append(stabilized_keywords, keyword) } } @@ -226,8 +226,7 @@ func createAddedKeywords(id string, path string, commitLine string) { raw_lines, err := utils.Exec(config.PortDir(), "git", "show", id, "--", path) if err != nil { if exitError, ok := err.(*exec.ExitError); !ok || exitError.ExitCode() != 1 { - logger.Error.Println("Problem parsing file") - logger.Error.Println(exitError) + slog.Error("Failed running git show", slog.String("id", id), slog.String("path", path), slog.Any("err", err)) return } } @@ -264,7 +263,12 @@ func updateFirstCommitOfPackage(path string, commitLine string, precedingCommits } func dumpToDatabase() { - logger.Info.Println("Writing to database") + slog.Info("Writing to database", + slog.Int("KeywordChange", len(keywordChanges)), + slog.Int("Package", len(packages)), + slog.Int("CommitToPackage", len(packagesCommit)), + slog.Int("CommitToVersion", len(versionsCommits)), + slog.Int("Commit", len(commits))) if len(keywordChanges) > 0 { rows := make([]*models.KeywordChange, 0, len(keywordChanges)) @@ -273,41 +277,40 @@ func dumpToDatabase() { } _, err := database.DBCon.Model(&rows).OnConflict("(id) DO UPDATE").Insert() if err != nil { - logger.Error.Println("Error during updating KeywordChange", err) + slog.Error("Failed inserting KeywordChange", slog.Any("err", err)) } - keywordChanges = map[string]*models.KeywordChange{} + clear(keywordChanges) } if len(packages) > 0 { _, err := database.DBCon.Model(&packages).Column("preceding_commits").Update() if err != nil { - logger.Error.Println("Error during updating precedingCommits", err) + slog.Error("Failed inserting Package", slog.Any("err", err)) } - packages = nil + clear(packages) } if len(packagesCommit) > 0 { _, err := database.DBCon.Model(&packagesCommit).OnConflict("(id) DO NOTHING").Insert() if err != nil { - logger.Error.Println("Error during updating CommitToPackage", err) + slog.Error("Failed inserting CommitToPackage", slog.Any("err", err)) } - packagesCommit = nil + clear(packagesCommit) } if len(versionsCommits) > 0 { _, err := database.DBCon.Model(&versionsCommits).OnConflict("(id) DO NOTHING").Insert() if err != nil { - logger.Error.Println("Error during updating CommitToVersion", err) + slog.Error("Failed inserting CommitToVersion", slog.Any("err", err)) } - versionsCommits = nil + clear(versionsCommits) } if len(commits) > 0 { _, err := database.DBCon.Model(&commits).OnConflict("(id) DO UPDATE").Insert() if err != nil { - logger.Error.Println("Error during updating commits:", err) + slog.Error("Failed inserting Commit", slog.Any("err", err)) } - commits = nil + clear(commits) } - } diff --git a/pkg/portage/repository/deprecated.go b/pkg/portage/repository/deprecated.go index 6e2546d..689faa4 100644 --- a/pkg/portage/repository/deprecated.go +++ b/pkg/portage/repository/deprecated.go @@ -10,9 +10,9 @@ package repository import ( + "log/slog" "soko/pkg/config" "soko/pkg/database" - "soko/pkg/logger" "soko/pkg/models" "soko/pkg/portage/utils" "strings" @@ -48,7 +48,7 @@ func UpdatePackagesDeprecated(path string) { } if status != "D" && isPackagesDeprecated(changedFile) { - logger.Info.Println("Updating package.deprecated") + slog.Info("Updating package.deprecated") // delete all existing entries before parsing the file again // in future we might implement a incremental version here @@ -96,7 +96,7 @@ func parsePackagesDeprecated(entry string) { _, err := database.DBCon.Model(entry).OnConflict("(versions) DO UPDATE").Insert() if err != nil { - logger.Error.Println("Error while inserting/updating package deprecated entry", err) + slog.Error("Failed inserting/updating package deprecated entry", slog.Any("err", err)) } } } @@ -108,7 +108,7 @@ func getDeprecatedPackages(path string) []string { var deprecates []string lines, err := utils.ReadLines(config.PortDir() + "/" + path) if err != nil { - logger.Error.Println("Could not read package.deprecated file, aborting import, err:", err) + slog.Error("Could not read package.deprecated file. Abort deprecated import", slog.Any("err", err)) return deprecates } @@ -129,7 +129,7 @@ func CalculateDeprecatedToVersion() { var deprecates []*models.DeprecatedPackage err := database.DBCon.Model(&deprecates).Select() if err != nil && err != pg.ErrNoRows { - logger.Error.Println("Failed to retrieve package masks. Aborting update", err) + slog.Error("Failed to retrieve package masks. Aborting update", slog.Any("err", err)) return } @@ -147,7 +147,7 @@ func CalculateDeprecatedToVersion() { _, err := database.DBCon.Model(depToVersion).OnConflict("(id) DO UPDATE").Insert() if err != nil { - logger.Error.Println("Error while inserting mask to version entry", err) + slog.Error("Failed inserting/updating deprecated to version entry", slog.Any("err", err)) } } } diff --git a/pkg/portage/repository/mask.go b/pkg/portage/repository/mask.go index b250ac0..6101fd8 100644 --- a/pkg/portage/repository/mask.go +++ b/pkg/portage/repository/mask.go @@ -12,10 +12,10 @@ package repository import ( + "log/slog" "regexp" "soko/pkg/config" "soko/pkg/database" - "soko/pkg/logger" "soko/pkg/models" "soko/pkg/portage/utils" "strings" @@ -52,8 +52,7 @@ func UpdateMask(path string) { } if status != "D" && isMask(changedFile) { - - logger.Info.Println("Updating Masks") + slog.Info("Updating Masks") // delete all existing masks before parsing the file again // in future we might implement a incremental version here @@ -83,9 +82,8 @@ func versionSpecifierToPackageAtom(versionSpecifier string) string { // parseAuthorLine parses the first line in the package.mask file // and returns the author name, author email and the date func parseAuthorLine(authorLine string) (string, string, time.Time) { - if !(strings.Contains(authorLine, "<") && strings.Contains(authorLine, ">")) { - logger.Error.Println("Error while parsing the author line in mask entry:", authorLine) + slog.Error("Error while parsing the author line in mask entry", slog.String("authorLine", authorLine)) return "", "", time.Now() } @@ -97,8 +95,7 @@ func parseAuthorLine(authorLine string) (string, string, time.Time) { date = strings.ReplaceAll(date, ")", "") parsedDate, err := time.Parse("2006-01-02", date) if err != nil { - logger.Error.Println("Error while parsing package mask date: " + date) - logger.Error.Println(err) + slog.Error("Failed parsing package mask date", slog.String("date", date), slog.Any("err", err)) } return author, authorEmail, parsedDate } @@ -143,7 +140,7 @@ func parsePackageMask(packageMask string) { _, err := database.DBCon.Model(mask).OnConflict("(versions) DO UPDATE").Insert() if err != nil { - logger.Error.Println("Error while inserting/updating package mask entry", err) + slog.Error("Failed inserting/updating package mask entry", slog.Any("err", err)) } } } @@ -156,7 +153,7 @@ func getMasks(path string) []string { lines, err := utils.ReadLines(config.PortDir() + "/" + path) if err != nil { - logger.Error.Println("Could not read Masks file. Abort masks import", err) + slog.Error("Could not read Masks file. Abort masks import", slog.Any("err", err)) return masks } @@ -178,7 +175,7 @@ func CalculateMaskedVersions() { var masks []*models.Mask err := database.DBCon.Model(&masks).Select() if err != nil && err != pg.ErrNoRows { - logger.Error.Println("Failed to retrieve package masks. Aborting update", err) + slog.Error("Failed to retrieve package masks. Aborting update", slog.Any("err", err)) return } @@ -202,7 +199,7 @@ func maskVersions(versionSpecifier string, versions []*models.Version) { _, err := database.DBCon.Model(maskToVersion).OnConflict("(id) DO UPDATE").Insert() if err != nil { - logger.Error.Println("Error while inserting mask to version entry", err) + slog.Error("Error while inserting mask to version entry", slog.Any("err", err)) } } } diff --git a/pkg/portage/repository/package.go b/pkg/portage/repository/package.go index 62390fd..09c060e 100644 --- a/pkg/portage/repository/package.go +++ b/pkg/portage/repository/package.go @@ -4,11 +4,11 @@ package repository import ( "encoding/xml" + "log/slog" "os" "regexp" "soko/pkg/config" "soko/pkg/database" - "soko/pkg/logger" "soko/pkg/models" "strings" ) @@ -60,9 +60,9 @@ func UpdatePackages(paths []string) { } res, err := database.DBCon.Model(&rows).Delete() if err != nil { - logger.Error.Println("Error during deleting packages", err) + slog.Error("Failed deleting packages", slog.Any("err", err)) } else { - logger.Info.Println("Deleted", res.RowsAffected(), "packages") + slog.Info("Deleted packages", slog.Int("rows", res.RowsAffected())) } } @@ -80,9 +80,9 @@ func UpdatePackages(paths []string) { Set("upstream = EXCLUDED.upstream"). Insert() if err != nil { - logger.Error.Println("Error during updating packages", err) + slog.Error("Failed updating packages", slog.Any("err", err)) } else { - logger.Info.Println("Updated", res.RowsAffected(), "packages") + slog.Info("Updated packages", slog.Int("rows", res.RowsAffected())) } } } @@ -107,14 +107,14 @@ func updateModifiedPackage(changedFile string) *models.Package { xmlFile, err := os.Open(config.PortDir() + "/" + atom + "/metadata.xml") if err != nil { - logger.Error.Println("Error during reading package metadata", err) + slog.Error("Failed reading package metadata", slog.String("atom", atom), slog.Any("err", err)) return nil } defer xmlFile.Close() var pkgMetadata PkgMetadata err = xml.NewDecoder(xmlFile).Decode(&pkgMetadata) if err != nil { - logger.Error.Println("Error during package", changedFile, "decoding", err) + slog.Error("Failed decoding package metadata", slog.String("atom", atom), slog.Any("err", err)) return nil } diff --git a/pkg/portage/repository/use.go b/pkg/portage/repository/use.go index 8ba915a..3de6d5e 100644 --- a/pkg/portage/repository/use.go +++ b/pkg/portage/repository/use.go @@ -3,9 +3,9 @@ package repository import ( + "log/slog" "soko/pkg/config" "soko/pkg/database" - "soko/pkg/logger" "soko/pkg/models" "soko/pkg/portage/utils" "strings" @@ -60,9 +60,9 @@ func UpdateUse(path string) { } res, err := database.DBCon.Model(&rows).OnConflict("(id) DO UPDATE").Insert() if err != nil { - logger.Error.Println("Error during updating use flags", err) + slog.Error("Failed updating use flags", slog.Any("err", err)) } else { - logger.Info.Println("Updated", res.RowsAffected(), "use flags") + slog.Info("Updated use flags", slog.Int("rows", res.RowsAffected())) } } } diff --git a/pkg/portage/repository/version.go b/pkg/portage/repository/version.go index 3ee520d..178a9d2 100644 --- a/pkg/portage/repository/version.go +++ b/pkg/portage/repository/version.go @@ -3,10 +3,10 @@ package repository import ( + "log/slog" "regexp" "soko/pkg/config" "soko/pkg/database" - "soko/pkg/logger" "soko/pkg/models" "soko/pkg/portage/utils" "strings" @@ -57,9 +57,9 @@ func UpdateVersions(paths []string) { } res, err := database.DBCon.Model(&rows).Delete() if err != nil { - logger.Error.Println("Error during deleting versions", err) + slog.Error("Failed deleting versions", slog.Any("err", err)) } else { - logger.Info.Println("Deleted", res.RowsAffected(), "versions") + slog.Info("Deleted versions", slog.Int("rows", res.RowsAffected())) } } @@ -70,9 +70,9 @@ func UpdateVersions(paths []string) { } res, err := database.DBCon.Model(&rows).OnConflict("(id) DO UPDATE").Insert() if err != nil { - logger.Error.Println("Error during updating versions", err) + slog.Error("Failed updating versions", slog.Any("err", err)) } else { - logger.Info.Println("Updated", res.RowsAffected(), "versions") + slog.Info("Updated versions", slog.Int("rows", res.RowsAffected())) } } } @@ -109,7 +109,6 @@ func updateModifiedVersion(changedFile string) *models.Version { var useflags, restricts, properties, homepages []string for _, metadata := range version_metadata { - switch { case strings.HasPrefix(metadata, "EAPI="): eapi = strings.TrimPrefix(metadata, "EAPI=") @@ -142,7 +141,6 @@ func updateModifiedVersion(changedFile string) *models.Version { rawSlot := strings.TrimPrefix(metadata, "SLOT=") slot, subslot, _ = strings.Cut(rawSlot, "/") } - } return &models.Version{ diff --git a/pkg/portage/update.go b/pkg/portage/update.go index 3db726c..8e53104 100644 --- a/pkg/portage/update.go +++ b/pkg/portage/update.go @@ -3,12 +3,10 @@ package portage import ( - "io" - "log" + "log/slog" "os" "soko/pkg/config" "soko/pkg/database" - "soko/pkg/logger" "soko/pkg/models" "soko/pkg/portage/repository" "soko/pkg/portage/utils" @@ -21,15 +19,10 @@ import ( // this is the first update that is there is no last update a full import // starting with the first commit in the tree is done. func Update() { - database.Connect() defer database.DBCon.Close() - if config.Quiet() == "true" { - log.SetOutput(io.Discard) - } - - logger.Info.Println("Start update...") + slog.Info("Start update...") // update the local useflags repository.UpdateUse("profiles/use.local.desc") @@ -61,17 +54,14 @@ func Update() { // following commits. In case no last commit is present a full import // starting with the first commit in the tree is done. func updateMetadata(changed []string) { - - logger.Info.Println("Start updating changed metadata") - - logger.Info.Println("Iterating", len(changed), "changed files") + slog.Info("Start updating changed metadata") + slog.Info("Iterating changed files", slog.Int("count", len(changed))) for _, path := range changed { repository.UpdateUse(path) repository.UpdateMask(path) repository.UpdatePackagesDeprecated(path) repository.UpdateArch(path) } - } // updatePackageData incrementally updates all package data in the database, that has @@ -82,14 +72,12 @@ func updateMetadata(changed []string) { // // changed data is determined by parsing all commits since the last update. func updatePackageData(changed []string) { + slog.Info("Start updating changed package data") + slog.Info("Iterating changed files", slog.Int("count", len(changed))) - logger.Info.Println("Start updating changed package data") - - logger.Info.Println("Iterating", len(changed), "changed files") repository.UpdateVersions(changed) repository.UpdatePackages(changed) repository.UpdateCategories(changed) - } // updateHistory incrementally imports all new commits. New commits are @@ -97,8 +85,7 @@ func updatePackageData(changed []string) { // and parsing all following commits. In case no last commit is present // a full import starting with the first commit in the tree is done. func updateHistory() { - - logger.Info.Println("Start updating the history") + slog.Info("Start updating the history") latestCommit := repository.UpdateCommits() @@ -114,13 +101,9 @@ func updateHistory() { LastCommit: latestCommit, } - _, err := database.DBCon.Model(application). - OnConflict("(id) DO UPDATE"). - Insert() - + _, err := database.DBCon.Model(application).OnConflict("(id) DO UPDATE").Insert() if err != nil { - logger.Error.Println("Updating application data failed") - logger.Error.Println(err) + slog.Error("Failed updating application data", slog.Any("err", err)) } } @@ -135,26 +118,20 @@ func updateHistory() { // Once there is no outdated data found anymore this method may become // obsolete. func FullUpdate() { - database.Connect() defer database.DBCon.Close() - if config.Quiet() == "true" { - log.SetOutput(io.Discard) - } - - logger.Info.Println("Full update up...") + slog.Info("Full update up...") // Add new entries & update existing - logger.Info.Println("Update all present files") + slog.Info("Update all present files") // update useflags database.TruncateTable[models.Useflag]("id") repository.UpdateUse("profiles/use.desc") repository.UpdateUse("profiles/use.local.desc") - entries, err := os.ReadDir(config.PortDir() + "/profiles/desc") - if err != nil { - logger.Error.Println("Error reading profiles/desc", err) + if entries, err := os.ReadDir(config.PortDir() + "/profiles/desc"); err != nil { + slog.Error("Error reading profiles/desc", slog.Any("err", err)) } else { for _, entry := range entries { repository.UpdateUse(config.PortDir() + "/profiles/desc/" + entry.Name()) @@ -168,7 +145,7 @@ func FullUpdate() { repository.UpdateCategories(allFiles) // Delete removed entries - logger.Info.Println("Delete removed files from the database") + slog.Info("Delete removed files from the database") deleteRemovedVersions() deleteRemovedPackages() deleteRemovedCategories() @@ -178,7 +155,7 @@ func FullUpdate() { repository.CalculateMaskedVersions() repository.CalculateDeprecatedToVersion() - logger.Info.Println("Finished update up...") + slog.Info("Finished update up...") } // deleteRemovedVersions removes all versions from the database @@ -190,7 +167,7 @@ func deleteRemovedVersions() { for _, version := range versions { path := config.PortDir() + "/" + version.Atom + "/" + version.Package + "-" + version.Version + ".ebuild" if !utils.FileExists(path) { - logger.Error.Println("Found ebuild version in the database that does not exist at:", path) + slog.Error("Found ebuild version in the database that does not exist", slog.String("path", path)) toDelete = append(toDelete, version) } } @@ -198,9 +175,9 @@ func deleteRemovedVersions() { if len(toDelete) > 0 { res, err := database.DBCon.Model(&toDelete).Delete() if err != nil { - logger.Error.Println("Error deleting versions", err) + slog.Error("Failed deleting versions", slog.Any("err", err)) } else { - logger.Info.Println("Deleted", res.RowsAffected(), "versions") + slog.Info("Deleted versions", slog.Int("rows", res.RowsAffected())) } } } @@ -209,22 +186,22 @@ func deleteRemovedVersions() { // that are present in the database but not in the main tree. func deleteRemovedPackages() { var packages, toDelete []*models.Package - database.DBCon.Model(&packages).Select() + database.DBCon.Model(&packages).Column("atom").Select() for _, pkg := range packages { path := config.PortDir() + "/" + pkg.Atom if !utils.FileExists(path) { - logger.Error.Println("Found package in the database that does not exist at:", path) + slog.Error("Found package in the database that does not exist", slog.String("path", path)) toDelete = append(toDelete, pkg) } } if len(toDelete) > 0 { - res, err := database.DBCon.Model(&toDelete).Delete() + res, err := database.DBCon.Model(&toDelete).WherePK().Delete() if err != nil { - logger.Error.Println("Error deleting packages", err) + slog.Error("Failed deleting packages", slog.Any("err", err)) } else { - logger.Info.Println("Deleted", res.RowsAffected(), "packages") + slog.Info("Deleted packages", slog.Int("rows", res.RowsAffected())) } } } @@ -238,7 +215,7 @@ func deleteRemovedCategories() { for _, category := range categories { path := config.PortDir() + "/" + category.Name if !utils.FileExists(path) { - logger.Error.Println("Found category in the database that does not exist at:", path) + slog.Error("Found category in the database that does not exist", slog.String("path", path)) toDelete = append(toDelete, category) } } @@ -246,9 +223,9 @@ func deleteRemovedCategories() { if len(toDelete) > 0 { res, err := database.DBCon.Model(&toDelete).Delete() if err != nil { - logger.Error.Println("Error deleting categories", err) + slog.Error("Failed deleting categories", slog.Any("err", err)) } else { - logger.Info.Println("Deleted", res.RowsAffected(), "categories") + slog.Info("Deleted categories", slog.Int("rows", res.RowsAffected())) } } } @@ -266,7 +243,7 @@ func fixPrecedingCommitsOfPackages() { return } - logger.Error.Println("Found", len(packages), "packages with preceding commits == 0. This should not happen. Fixing...") + slog.Error("Found packages with preceding commits == 0. This should not happen. Fixing...", slog.Int("count", len(packages))) for _, pkg := range packages { pkg.PrecedingCommits = 1 } @@ -280,7 +257,7 @@ func getApplicationData() models.Application { applicationData := &models.Application{Id: "latest"} err := database.DBCon.Model(applicationData).WherePK().Select() if err != nil { - logger.Error.Println("Error fetching application data") + slog.Error("Failed fetching application data", slog.Any("err", err)) return models.Application{ Id: "latest", LastUpdate: time.Now(), diff --git a/pkg/portage/utils/git.go b/pkg/portage/utils/git.go index 3e76c34..c576105 100644 --- a/pkg/portage/utils/git.go +++ b/pkg/portage/utils/git.go @@ -3,10 +3,10 @@ package utils import ( + "log/slog" "os/exec" "soko/pkg/config" "soko/pkg/database" - "soko/pkg/logger" "soko/pkg/models" "strings" ) @@ -23,7 +23,7 @@ func AllFiles() []string { cmd.Dir = config.PortDir() out, err := cmd.CombinedOutput() if err != nil { - logger.Error.Println("cmd.Run() failed with:", err) + slog.Error("cmd.Run() failed", slog.Any("err", err)) return allFiles } @@ -44,7 +44,7 @@ func ChangedFiles(startCommit string, endCommit string) []string { cmd.Dir = config.PortDir() out, err := cmd.CombinedOutput() if err != nil { - logger.Error.Println("cmd.Run() failed with", err) + slog.Error("cmd.Run() failed", slog.Any("err", err)) return changedFiles } @@ -76,7 +76,7 @@ func GetCommits(startCommit string, endCommit string) []string { cmd.Dir = config.PortDir() out, err := cmd.CombinedOutput() if err != nil { - logger.Error.Println("cmd.Run() failed with", err) + slog.Error("cmd.Run() failed", slog.Any("err", err)) return commits } diff --git a/pkg/portage/utils/misc.go b/pkg/portage/utils/misc.go deleted file mode 100644 index 3e4e79c..0000000 --- a/pkg/portage/utils/misc.go +++ /dev/null @@ -1,13 +0,0 @@ -// Contains miscellaneous utility functions - -package utils - -// Contains tells whether string a contains string x. -func Contains(a []string, x string) bool { - for _, n := range a { - if x == n { - return true - } - } - return false -} diff --git a/pkg/selfcheck/check.go b/pkg/selfcheck/check.go index 40ccc9f..6d02548 100644 --- a/pkg/selfcheck/check.go +++ b/pkg/selfcheck/check.go @@ -1,31 +1,32 @@ package selfcheck import ( - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/promauto" + "log/slog" "soko/pkg/database" - "soko/pkg/logger" "soko/pkg/models" "soko/pkg/selfcheck/metrics" "soko/pkg/selfcheck/repository" "soko/pkg/selfcheck/storage" + + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" ) func AllPackages() { - logger.Info.Println("selfcheck: Preparing new check...") - logger.Info.Println("selfcheck: Updating selfcheck repository") + slog.Info("selfcheck: Preparing new check...") + slog.Info("selfcheck: Updating selfcheck repository") repository.UpdateRepo() - logger.Info.Println("selfcheck: Importing data") + slog.Info("selfcheck: Importing data") repository.Import() - logger.Info.Println("selfcheck: Resetting metrics") + slog.Info("selfcheck: Resetting metrics") resetMetrics() - logger.Info.Println("selfcheck: Start check") + slog.Info("selfcheck: Start check") for _, category := range storage.Categories { - //logger.Info.Println("Checking " + category.Name) + //slog.Info("Checking " + category.Name) checkCategory(category) } - logger.Info.Println("selfcheck: Finished check") + slog.Info("selfcheck: Finished check") } func resetMetrics() { @@ -53,7 +54,7 @@ func checkCategory(category *models.Category) { Select() if err != nil { - logger.Error.Println(err) + slog.Error("Failed fetching category", slog.Any("err", err), slog.Any("category", category.Name)) return } diff --git a/pkg/selfcheck/portage/category.go b/pkg/selfcheck/portage/category.go index d7c04a9..387514d 100644 --- a/pkg/selfcheck/portage/category.go +++ b/pkg/selfcheck/portage/category.go @@ -5,10 +5,10 @@ package repository import ( "encoding/xml" "io" + "log/slog" "os" "regexp" "soko/pkg/config" - "soko/pkg/logger" "soko/pkg/models" "soko/pkg/selfcheck/storage" "strings" @@ -68,8 +68,8 @@ func addCategory(category *models.Category) { func GetCatMetadata(path string) Catmetadata { xmlFile, err := os.Open(path) if err != nil { - logger.Error.Println("Error during reading category metadata") - logger.Error.Println(err) + slog.Error("Failed reading category metadata", slog.String("path", path), slog.Any("err", err)) + return Catmetadata{} } defer xmlFile.Close() byteValue, _ := io.ReadAll(xmlFile) diff --git a/pkg/selfcheck/portage/mask.go b/pkg/selfcheck/portage/mask.go index 12e2f86..1d8fc53 100644 --- a/pkg/selfcheck/portage/mask.go +++ b/pkg/selfcheck/portage/mask.go @@ -12,8 +12,8 @@ package repository import ( + "log/slog" "regexp" - "soko/pkg/logger" "soko/pkg/models" "soko/pkg/portage/utils" "soko/pkg/selfcheck/storage" @@ -57,8 +57,7 @@ func versionSpecifierToPackageAtom(versionSpecifier string) string { func parseAuthorLine(authorLine string) (string, string, time.Time) { if !(strings.Contains(authorLine, "<") && strings.Contains(authorLine, ">")) { - logger.Error.Println("Error while parsing the author line in mask entry:") - logger.Error.Println(authorLine) + slog.Error("Error while parsing the author line in mask entry", slog.String("authorLine", authorLine)) return "", "", time.Now() } @@ -70,8 +69,7 @@ func parseAuthorLine(authorLine string) (string, string, time.Time) { date = strings.ReplaceAll(date, ")", "") parsedDate, err := time.Parse("2006-01-02", date) if err != nil { - logger.Error.Println("Error while parsing package mask date: " + date) - logger.Error.Println(err) + slog.Error("Failed parsing package mask date", slog.String("date", date), slog.Any("err", err)) } return author, authorEmail, parsedDate } @@ -115,8 +113,7 @@ func getMasks(path string) []string { lines, err := utils.ReadLines(path) if err != nil { - logger.Error.Println("Could not read Masks file. Abort masks import") - logger.Error.Println(err) + slog.Error("Could not read Masks file. Abort masks import", slog.Any("err", err)) return masks } @@ -171,7 +168,7 @@ func comparedVersions(operator string, versionSpecifier string, packageAtom stri versionSpecifier = strings.Split(versionSpecifier, ":")[0] for _, version := range storage.Versions { - if version.Atom == packageAtom { + if version.Atom == packageAtom { versions = append(versions, version) } } @@ -207,7 +204,7 @@ func allRevisions(versionSpecifier string, packageAtom string) []*models.Version versionWithoutRevision = strings.ReplaceAll(versionWithoutRevision, "~", "") for _, version := range storage.Versions { - if strings.HasPrefix(version.Id, versionWithoutRevision) { + if strings.HasPrefix(version.Id, versionWithoutRevision) { versions = append(versions, version) } } @@ -268,4 +265,4 @@ func maskVersions(mask *models.Mask, versions []*models.Version) { } } -}
\ No newline at end of file +} diff --git a/pkg/selfcheck/portage/package.go b/pkg/selfcheck/portage/package.go index 14289a0..278f087 100644 --- a/pkg/selfcheck/portage/package.go +++ b/pkg/selfcheck/portage/package.go @@ -5,10 +5,10 @@ package repository import ( "encoding/xml" "io" + "log/slog" "os" "regexp" "soko/pkg/config" - "soko/pkg/logger" "soko/pkg/models" "soko/pkg/selfcheck/storage" "strings" @@ -101,8 +101,8 @@ func addPackage(newPackage *models.Package) { func GetPkgMetadata(path string) Pkgmetadata { xmlFile, err := os.Open(path) if err != nil { - logger.Error.Println("Error during reading package metadata") - logger.Error.Println(err) + slog.Error("Failed reading package metadata", slog.String("path", path), slog.Any("err", err)) + return Pkgmetadata{} } defer xmlFile.Close() byteValue, _ := io.ReadAll(xmlFile) diff --git a/pkg/selfcheck/portage/use.go b/pkg/selfcheck/portage/use.go index a47f543..36bed48 100644 --- a/pkg/selfcheck/portage/use.go +++ b/pkg/selfcheck/portage/use.go @@ -3,8 +3,8 @@ package repository import ( + "log/slog" "soko/pkg/config" - "soko/pkg/logger" "soko/pkg/models" "soko/pkg/portage/utils" "soko/pkg/selfcheck/storage" @@ -37,10 +37,7 @@ func UpdateUse(path string) { } if err != nil { - logger.Info.Println("Error during updating useflag " + rawFlag) - logger.Info.Println(err) - logger.Error.Println("Error during updating useflag " + rawFlag) - logger.Error.Println(err) + slog.Error("Failed updating useflag", slog.String("useflag", rawFlag), slog.Any("err", err)) } } } diff --git a/pkg/selfcheck/serve.go b/pkg/selfcheck/serve.go index 20899ee..7d5498b 100644 --- a/pkg/selfcheck/serve.go +++ b/pkg/selfcheck/serve.go @@ -1,23 +1,26 @@ package selfcheck import ( - "github.com/prometheus/client_golang/prometheus/promhttp" - "log" + "log/slog" "net/http" + "os" + + "github.com/prometheus/client_golang/prometheus/promhttp" + "soko/pkg/config" - "soko/pkg/logger" "soko/pkg/metrics" ) // Serve is used to serve the web application func Serve() { - // prometheus metrics http.Handle("/metrics", metricsHandler()) - logger.Info.Println("Serving on port: " + config.Port()) - log.Fatal(http.ListenAndServe(":"+config.Port(), nil)) - + address := ":" + config.Port() + slog.Info("Serving self-check", slog.String("address", address)) + err := http.ListenAndServe(address, nil) + slog.Error("exited server", "err", err) + os.Exit(1) } // metricsHandler is used as default middleware to update the metrics @@ -2,11 +2,17 @@ package main import ( "flag" - "io" + "log" + "log/slog" "os" + "time" + + "github.com/jasonlvhit/gocron" + "github.com/lmittmann/tint" + slogmulti "github.com/samber/slog-multi" + "soko/pkg/app" "soko/pkg/config" - "soko/pkg/logger" "soko/pkg/portage" "soko/pkg/portage/bugs" "soko/pkg/portage/dependencies" @@ -16,19 +22,13 @@ import ( "soko/pkg/portage/projects" "soko/pkg/portage/repology" "soko/pkg/selfcheck" - "time" - - "github.com/jasonlvhit/gocron" ) func main() { + initLoggers() waitForPostgres() - errorLogFile := logger.CreateLogFile(config.LogFile()) - defer errorLogFile.Close() - initLoggers(os.Stdout, errorLogFile) - serve := flag.Bool("serve", false, "Start serving the application") selfchecks := flag.Bool("enable-selfchecks", false, "Perform selfchecks periodicals to monitor the consistency of the data") update := flag.Bool("update", false, "Perform an incremental update of the package data") @@ -47,36 +47,36 @@ func main() { flag.Parse() if *selfchecks { - logger.Info.Println("Enabling periodical selfcheck") + slog.Info("Enabling periodical selfcheck") go runSelfChecks() selfcheck.Serve() } if *update { - logger.Info.Println("Updating package data") + slog.Info("Updating package data") portage.Update() } if *fullupdate { - logger.Info.Println("Performing full update of the package data") + slog.Info("Performing full update of the package data") portage.FullUpdate() } if *updateOutdatedPackages { - logger.Info.Println("Updating the repology data") + slog.Info("Updating the repology data") repology.UpdateOutdated() } if *updatePkgcheckResults { - logger.Info.Println("Updating the qa-reports that is the pkgcheck data") + slog.Info("Updating the qa-reports that is the pkgcheck data") pkgcheck.UpdatePkgCheckResults() } if *updatePullrequests { - logger.Info.Println("Updating the pull requests data") + slog.Info("Updating the pull requests data") github.FullUpdatePullRequests() } if *updateBugs { - logger.Info.Println("Updating the bugs data") + slog.Info("Updating the bugs data") bugs.UpdateBugs() } if *updateDependencies { - logger.Info.Println("Updating the dependencies data") + slog.Info("Updating the dependencies data") dependencies.FullPackageDependenciesUpdate() } if *updateProjects { @@ -85,7 +85,7 @@ func main() { // updateMaintainers should always be executed last, as it is using // the updated bugs, pullrequests and and outdated packages if *updateMaintainers { - logger.Info.Println("Updating the maintainers data") + slog.Info("Updating the maintainers data") maintainers.FullImport() } @@ -96,17 +96,42 @@ func main() { if *help { flag.PrintDefaults() } - } // initialize the loggers depending on whether // config.debug is set to true -func initLoggers(infoHandler io.Writer, errorHandler io.Writer) { - if config.Debug() == "true" { - logger.Init(os.Stdout, infoHandler, errorHandler) +func initLoggers() { + errorHandler, err := os.OpenFile(config.LogFile(), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + log.Println("failed to open error log file", config.LogFile(), "error:", err) + errorHandler = os.Stderr + } + + var handler slog.Handler + if config.Debug() { + handler = tint.NewHandler(os.Stdout, &tint.Options{ + Level: slog.LevelDebug, + AddSource: true, + TimeFormat: time.DateTime, + }) } else { - logger.Init(io.Discard, infoHandler, errorHandler) + handler = slogmulti.Fanout( + tint.NewHandler(os.Stdout, &tint.Options{ + Level: slog.LevelInfo, + AddSource: true, + TimeFormat: time.DateTime, + NoColor: true, + }), + tint.NewHandler(errorHandler, &tint.Options{ + Level: slog.LevelError, + AddSource: true, + TimeFormat: time.DateTime, + NoColor: true, + }), + ) } + slog.SetLogLoggerLevel(slog.LevelInfo) + slog.SetDefault(slog.New(handler)) } // TODO this has to be solved differently |