Skip to content

Commit

Permalink
logger from context only
Browse files Browse the repository at this point in the history
  • Loading branch information
gosom committed May 13, 2023
1 parent 59e6218 commit 849f5dd
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
9 changes: 6 additions & 3 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import (
"github.com/gosom/kit/logging"
)

// GetLoggerFromContext returns a logger from the context.
// GetLoggerFromContext returns a logger from the context or a default logger
func GetLoggerFromContext(ctx context.Context) logging.Logger {
//nolint:errcheck // we don't care about the error
log := ctx.Value("log").(logging.Logger)
log, ok := ctx.Value(contextKey("log")).(logging.Logger)
if !ok {
return logging.Get()
}

return log
}
3 changes: 1 addition & 2 deletions examples/books-to-scrape-simple/bookstoscrape/detail_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"

"github.com/PuerkitoBio/goquery"
"github.com/gosom/kit/logging"
"github.com/gosom/scrapemate"
)

Expand All @@ -14,7 +13,7 @@ type BookDetailJob struct {
}

func (o *BookDetailJob) Process(ctx context.Context, resp *scrapemate.Response) (any, []scrapemate.IJob, error) {
log := ctx.Value("log").(logging.Logger)
log := scrapemate.GetLoggerFromContext(ctx)
log.Info("processing book detail job")
doc, ok := resp.Document.(*goquery.Document)
if !ok {
Expand Down
3 changes: 1 addition & 2 deletions examples/quotes-to-scrape-app/quotes/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/PuerkitoBio/goquery"
"github.com/google/uuid"
"github.com/gosom/kit/logging"
"github.com/gosom/scrapemate"
)

Expand Down Expand Up @@ -37,7 +36,7 @@ func NewQuoteCollectJob(u string) *QuoteCollectJob {

// Process is the function that will be called by scrapemate to process the job
func (o *QuoteCollectJob) Process(ctx context.Context, resp *scrapemate.Response) (any, []scrapemate.IJob, error) {
log := ctx.Value("log").(logging.Logger)
log := scrapemate.GetLoggerFromContext(ctx)
log.Info("processing quotes collect job")
doc, ok := resp.Document.(*goquery.Document)
if !ok {
Expand Down

0 comments on commit 849f5dd

Please sign in to comment.