Skip to content

Commit

Permalink
Merge pull request #316 from hearchco/as/fix/logging-caller
Browse files Browse the repository at this point in the history
fix(logging): remove function names from logs, instead use zerolog Caller
  • Loading branch information
aleksasiriski authored May 28, 2024
2 parents 93a4bcb + 07a6ec8 commit cf8110e
Show file tree
Hide file tree
Showing 33 changed files with 207 additions and 89 deletions.
6 changes: 4 additions & 2 deletions src/cache/badger/badger.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func New(dataDirPath string, config config.Badger) (DB, error) {
Err(err).
Bool("persistence", config.Persist).
Str("path", badgerPath).
Msg("badger.New(): error opening badger")
Msg("Error opening badger")
} else if config.Persist {
log.Info().
Bool("persistence", config.Persist).
Expand All @@ -50,7 +50,9 @@ func New(dataDirPath string, config config.Badger) (DB, error) {

func (db DB) Close() {
if err := db.bdb.Close(); err != nil {
log.Error().Err(err).Msg("badger.Close(): error closing badger")
log.Error().
Err(err).
Msg("Error closing badger")
} else {
log.Debug().Msg("Successfully closed badger")
}
Expand Down
8 changes: 5 additions & 3 deletions src/cache/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,21 @@ func New(ctx context.Context, config config.Redis) (DB, error) {
log.Error().
Err(err).
Str("address", fmt.Sprintf("%v:%v/%v", config.Host, config.Port, config.Database)).
Msg("redis.New(): error connecting to redis")
Msg("Error creating new connection to redis")
} else {
log.Info().
Str("address", fmt.Sprintf("%v:%v/%v", config.Host, config.Port, config.Database)).
Msg("Successful connection to redis")
Msg("Successfully connected to redis")
}

return DB{rdb: rdb, ctx: ctx}, nil
}

func (db DB) Close() {
if err := db.rdb.Close(); err != nil {
log.Error().Err(err).Msg("redis.Close(): error disconnecting from redis")
log.Error().
Err(err).
Msg("Error closing connection to redis")
} else {
log.Debug().Msg("Successfully disconnected from redis")
}
Expand Down
7 changes: 5 additions & 2 deletions src/cli/climode.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,14 @@ func Run(flags Flags, db cache.DB, conf config.Config) {
Str("queryHash", anonymize.HashToSHA256B64(flags.Query)).
Int("maxPages", flags.PagesMax).
Bool("visit", flags.Visit).
Msg("Started hearching")
Msg("Started searching...")

categoryName, err := category.FromString(flags.Category)
if err != nil {
log.Fatal().Err(err).Msg("Invalid category")
log.Fatal().
Caller().
Err(err).
Msg("Invalid category")
}

// all of these have default values set and are validated beforehand
Expand Down
22 changes: 16 additions & 6 deletions src/cli/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@ func Setup() Flags {
)

if err := ctx.Validate(); err != nil {
log.Panic().Caller().Err(err).Msg("failed parsing cli") // panic is also run inside the library. when does this happen?
log.Panic().
Caller().
Err(err).
Msg("Failed parsing cli")
// ^PANIC
}

if cli.Query == "" {
log.Fatal().Caller().Msg("query cannot be empty or whitespace")
log.Fatal().
Caller().
Msg("Query cannot be empty or whitespace")
// ^FATAL
}

Expand All @@ -45,7 +50,7 @@ func Setup() Flags {
Int("pages", cli.PagesMax).
Int("min", 1).
Int("max", pagesMaxUpperLimit).
Msg("pages value out of range")
Msg("Pages value out of range")
// ^FATAL
}

Expand All @@ -55,20 +60,25 @@ func Setup() Flags {
Int("start", cli.PagesStart).
Int("min", 1).
Int("max", gotypelimits.MaxInt-pagesMaxUpperLimit).
Msg("start value out of range")
Msg("Start value out of range")
// ^FATAL
} else {
// since it's >=1, we decrement it to match the 0-based index
cli.PagesStart -= 1
}

if err := engines.ValidateLocale(cli.Locale); err != nil {
log.Fatal().Caller().Err(err).Msg("invalid locale flag")
log.Fatal().
Caller().
Err(err).
Msg("Invalid locale flag")
// ^FATAL
}

if _, err := category.FromString(cli.Category); err != nil {
log.Fatal().Caller().Msg("invalid category flag")
log.Fatal().
Caller().
Msg("Invalid category flag")
// ^FATAL
}

Expand Down
3 changes: 2 additions & 1 deletion src/config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ func NewSettings() map[engines.Name]Settings {
for _, eng := range engines.Names() {
if _, ok := mp[eng]; !ok {
log.Fatal().
Caller().
Str("engine", eng.String()).
Msg("config.NewSettings(): no shortcut set")
Msg("No shortcut set")
// ^FATAL
}
}
Expand Down
43 changes: 33 additions & 10 deletions src/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import (
// passed as pointer since config is modified
func (c *Config) fromReader(rc ReaderConfig) {
if rc.Server.Proxy.Salt == "" {
log.Fatal().Msg("config.fromReader(): proxy salt is empty")
log.Fatal().
Caller().
Msg("Image proxy salt is empty")
}

nc := Config{
Expand Down Expand Up @@ -53,9 +55,10 @@ func (c *Config) fromReader(rc ReaderConfig) {
keyName, err := engines.NameString(key)
if err != nil {
log.Panic().
Caller().
Err(err).
Str("engine", key).
Msg("config.fromReader(): invalid engine name")
Msg("Invalid engine name")
// ^PANIC
}
nc.Settings[keyName] = val
Expand All @@ -67,7 +70,10 @@ func (c *Config) fromReader(rc ReaderConfig) {
if eng.Enabled {
engineName, nameErr := engines.NameString(name)
if nameErr != nil {
log.Panic().Err(nameErr).Msg("failed converting string to engine name")
log.Panic().
Caller().
Err(nameErr).
Msg("Failed converting string to engine name")
// ^PANIC
}

Expand Down Expand Up @@ -164,41 +170,58 @@ func (c *Config) Load(dataDirPath string) {
// We provide a struct along with the struct tag `koanf` to the
// provider.
if err := k.Load(structs.Provider(&rc, "koanf"), nil); err != nil {
log.Panic().Err(err).Msg("config.Load(): failed loading default values")
log.Panic().
Caller().
Err(err).
Msg("Failed loading default values")
// ^PANIC
}

// Load YAML config
yamlPath := path.Join(dataDirPath, "hearchco.yaml")
if _, err := os.Stat(yamlPath); err != nil {
log.Trace().
Caller().
Str("path", yamlPath).
Msg("config.Load(): no yaml config found, looking for .yml")
Msg("No yaml (.yaml) config found, looking for .yml")
yamlPath = path.Join(dataDirPath, "hearchco.yml")
if _, errr := os.Stat(yamlPath); errr != nil {
log.Trace().
Caller().
Str("path", yamlPath).
Msg("config.Load(): no yaml config found")
Msg("No yaml (.yml) config found")
} else if errr := k.Load(file.Provider(yamlPath), yaml.Parser()); errr != nil {
log.Panic().Err(err).Msg("config.Load(): error loading yaml config")
log.Panic().
Caller().
Err(err).
Msg("Error loading yaml config")
// ^PANIC
}
} else if err := k.Load(file.Provider(yamlPath), yaml.Parser()); err != nil {
log.Panic().Err(err).Msg("config.Load(): error loading yaml config")
log.Panic().
Caller().
Err(err).
Msg("Error loading yaml config")
// ^PANIC
}

// Load ENV config
if err := k.Load(env.Provider("HEARCHCO_", ".", func(s string) string {
return strings.Replace(strings.ToLower(strings.TrimPrefix(s, "HEARCHCO_")), "_", ".", -1)
}), nil); err != nil {
log.Panic().Err(err).Msg("config.Load(): error loading env config")
log.Panic().
Caller().
Err(err).
Msg("Error loading env config")
// ^PANIC
}

// Unmarshal config into struct
if err := k.Unmarshal("", &rc); err != nil {
log.Panic().Err(err).Msg("config.Load(): failed unmarshaling koanf config")
log.Panic().
Caller().
Err(err).
Msg("Failed unmarshaling koanf config")
// ^PANIC
}

Expand Down
3 changes: 2 additions & 1 deletion src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ func main() {
db, err := cache.New(ctx, cliFlags.DataDirPath, conf.Server.Cache)
if err != nil {
log.Fatal().
Caller().
Err(err).
Msg("main.main(): failed creating a new db")
Msg("Failed creating a new db")
// ^FATAL
}

Expand Down
3 changes: 2 additions & 1 deletion src/moretime/fancy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ func handleAtoi(s string) int64 {
i, err := strconv.Atoi(s)
if err != nil {
log.Panic().
Caller().
Err(err).
Msg("failed converting string to int")
Msg("Failed converting string to int")
// ^PANIC
}
return int64(i)
Expand Down
4 changes: 3 additions & 1 deletion src/profiling.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ func runProfiler(cliFlags cli.Flags) (bool, func()) {
profilerToRun := profiler{enabled: false}
for _, p := range profilers {
if profilerToRun.enabled && p.enabled {
log.Fatal().Msg("main.runProfiler(): only one profiler can be run at a time")
log.Fatal().
Caller().
Msg("Only one profiler can be run at a time")
// ^FATAL
} else if p.enabled {
profilerToRun = p
Expand Down
1 change: 1 addition & 0 deletions src/router/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func Proxy(w http.ResponseWriter, r *http.Request, salt string, timeouts config.
}

log.Trace().
Caller().
Str("request", fmt.Sprint(nr)).
Msg("Created new request")

Expand Down
2 changes: 2 additions & 0 deletions src/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (rw RouterWrapper) Start(ctx context.Context) {
err := srv.Shutdown(timeout)
if err != nil {
log.Error().
Caller().
Err(err).
Msg("Server shut down failed")
} else {
Expand All @@ -66,6 +67,7 @@ func (rw RouterWrapper) Start(ctx context.Context) {
err := srv.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
log.Fatal().
Caller().
Err(err).
Msg("Failed to start server")
}
Expand Down
9 changes: 6 additions & 3 deletions src/search/bucket/addresult.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ import (
func AddSEResult(seResult *result.RetrievedResult, seName engines.Name, relay *Relay, options engines.Options, pagesCol *colly.Collector, nEnabledEngines int) bool {
if seResult == nil {
log.Error().
Caller().
Str("engine", seName.String()).
Msg("bucket.AddSEResult(): nil result")
Msg("Result is nil")
return false
}

// TODO: add a check if image result is valid
if seResult.URL == "" || seResult.Title == "" {
log.Error().
Caller().
Str("engine", seName.String()).
Str("url", seResult.URL).
Str("title", seResult.Title).
Str("description", seResult.Description).
Msg("bucket.AddSEResult(): invalid result, some fields are empty")
Msg("Invalid result, some fields are empty")
return false
}

Expand Down Expand Up @@ -90,9 +92,10 @@ func AddSEResult(seResult *result.RetrievedResult, seName engines.Name, relay *R
if !exists && options.VisitPages {
if err := pagesCol.Visit(seResult.URL); err != nil {
log.Error().
Caller().
Err(err).
Str("url", seResult.URL).
Msg("bucket.AddSEResult(): failed visiting")
Msg("Failed visiting page")
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/search/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ func CacheAndUpdateResults(
serr := db.Set(query, results, ttlConf.Time)
if serr != nil {
log.Error().
Caller().
Err(serr).
Str("queryAnon", anonymize.String(query)).
Str("queryHash", anonymize.HashToSHA256B64(query)).
Msg("cli.Run(): error updating database with search results")
Msg("Error updating database with search results")
}
} else {
log.Debug().
Expand All @@ -36,10 +37,11 @@ func CacheAndUpdateResults(
ttl, terr := db.GetTTL(query)
if terr != nil {
log.Error().
Caller().
Err(terr).
Str("queryAnon", anonymize.String(query)).
Str("queryHash", anonymize.HashToSHA256B64(query)).
Msg("cli.Run(): error getting TTL from database")
Msg("Error getting TTL from database")
} else if ttl < ttlConf.RefreshTime {
log.Info().
Str("queryAnon", anonymize.String(query)).
Expand All @@ -50,10 +52,11 @@ func CacheAndUpdateResults(
if uerr != nil {
// Error in updating cache is not returned, just logged
log.Error().
Caller().
Err(uerr).
Str("queryAnon", anonymize.String(query)).
Str("queryHash", anonymize.HashToSHA256B64(query)).
Msg("cli.Run(): error replacing old results while updating database")
Msg("Error replacing old results while updating database")
}
}
}
Expand Down
Loading

0 comments on commit cf8110e

Please sign in to comment.