feat: Add optional logging to the scraper

This commit is contained in:
Arik Jones (aider)
2024-09-14 19:59:02 -05:00
parent 01d6b2f54f
commit 23508df6f4
2 changed files with 19 additions and 2 deletions

View File

@@ -19,6 +19,7 @@ var (
ignorePatterns string ignorePatterns string
configFile string configFile string
cfg *config.Config cfg *config.Config
verbose bool
) )
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
@@ -48,6 +49,8 @@ whose name is <project-directory-name>-rollup-<timestamp>.md.`,
func Execute(config *config.Config, scraperConfig scraper.Config) error { func Execute(config *config.Config, scraperConfig scraper.Config) error {
cfg = config cfg = config
scraper.SetupLogger(verbose)
scraperConfig.Verbose = verbose
return rootCmd.Execute() return rootCmd.Execute()
} }
@@ -57,6 +60,7 @@ func init() {
rootCmd.Flags().StringVarP(&codeGenPatterns, "codegen", "g", "", "Comma-separated list of glob patterns for code-generated files") rootCmd.Flags().StringVarP(&codeGenPatterns, "codegen", "g", "", "Comma-separated list of glob patterns for code-generated files")
rootCmd.Flags().StringVarP(&ignorePatterns, "ignore", "i", "", "Comma-separated list of glob patterns for files to ignore") rootCmd.Flags().StringVarP(&ignorePatterns, "ignore", "i", "", "Comma-separated list of glob patterns for files to ignore")
rootCmd.Flags().StringVarP(&configFile, "config", "f", "", "Path to the config file (default: rollup.yml in the current directory)") rootCmd.Flags().StringVarP(&configFile, "config", "f", "", "Path to the config file (default: rollup.yml in the current directory)")
rootCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Enable verbose logging")
} }
func matchGlob(pattern, path string) bool { func matchGlob(pattern, path string) bool {

View File

@@ -2,6 +2,7 @@ package scraper
import ( import (
"fmt" "fmt"
"io/ioutil"
"log" "log"
"math/rand" "math/rand"
"strings" "strings"
@@ -12,6 +13,8 @@ import (
"github.com/russross/blackfriday/v2" "github.com/russross/blackfriday/v2"
) )
var logger *log.Logger
var ( var (
pw *playwright.Playwright pw *playwright.Playwright
browser playwright.Browser browser playwright.Browser
@@ -20,11 +23,21 @@ var (
// Config holds the scraper configuration // Config holds the scraper configuration
type Config struct { type Config struct {
CSSLocator string CSSLocator string
Verbose bool
}
// SetupLogger initializes the logger based on the verbose flag
func SetupLogger(verbose bool) {
if verbose {
logger = log.New(log.Writer(), "SCRAPER: ", log.LstdFlags)
} else {
logger = log.New(ioutil.Discard, "", 0)
}
} }
// InitPlaywright initializes Playwright and launches the browser // InitPlaywright initializes Playwright and launches the browser
func InitPlaywright() error { func InitPlaywright() error {
log.Println("Initializing Playwright") logger.Println("Initializing Playwright")
var err error var err error
pw, err = playwright.Run() pw, err = playwright.Run()
if err != nil { if err != nil {
@@ -40,7 +53,7 @@ func InitPlaywright() error {
return fmt.Errorf("could not launch browser: %v", err) return fmt.Errorf("could not launch browser: %v", err)
} }
log.Println("Playwright initialized successfully") logger.Println("Playwright initialized successfully")
return nil return nil
} }