Improve logging: add structured logs, enrich logs with useful info.

This commit is contained in:
Rustem Kamalov
2026-04-22 03:39:30 +03:00
parent d12a301cf8
commit 0d4e2da960
26 changed files with 657 additions and 247 deletions

View File

@@ -97,11 +97,16 @@ func (bing *Bing) acceptCookies(ctx context.Context, page *rod.Page) error {
// Search executes a Bing web search and returns normalized search results.
// It may return core.ErrCaptcha or core.ErrSearchTimeout.
func (bing *Bing) Search(ctx context.Context, query core.Query) (results []core.SearchResult, err error) {
ctx = core.EnsureContext(ctx)
ctx = core.WithEngine(core.EnsureContext(ctx), bing.Name())
ctx = core.WithQueryHash(ctx, core.QueryHashFromQuery(query))
scoped := *bing
scoped.logger = bing.logger.WithRequest(ctx)
bing = &scoped
bing.logger.Debug("Starting search, query: %+v", query)
defer func() {
if recovered := recover(); recovered != nil {
err = core.RecoverEnginePanic(bing.Name(), recovered, bing.logger)
err = core.RecoverEnginePanicWithContext(ctx, bing.Name(), recovered, bing.logger)
results = nil
}
}()
@@ -268,7 +273,12 @@ type BingImageData struct {
// SearchImage executes a Bing image search and returns normalized image
// results. It may return core.ErrCaptcha or core.ErrSearchTimeout.
func (bing *Bing) SearchImage(ctx context.Context, query core.Query) ([]core.SearchResult, error) {
ctx = core.EnsureContext(ctx)
ctx = core.WithEngine(core.EnsureContext(ctx), bing.Name())
ctx = core.WithQueryHash(ctx, core.QueryHashFromQuery(query))
scoped := *bing
scoped.logger = bing.logger.WithRequest(ctx)
bing = &scoped
bing.logger.Debug("Starting image search, query: %+v", query)
searchResults := []core.SearchResult{}

View File

@@ -33,7 +33,7 @@ func BuildURL(q core.Query) (string, error) {
text += " filetype:" + q.Filetype
}
logrus.Tracef("Query text: %s", text)
logrus.WithField("query_hash", core.QueryHash(text)).Trace(fmt.Sprintf("Query text: %s", text))
params.Add("q", text)
}