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
configFile string
cfg *config.Config
verbose bool
)
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 {
cfg = config
scraper.SetupLogger(verbose)
scraperConfig.Verbose = verbose
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(&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().BoolVarP(&verbose, "verbose", "v", false, "Enable verbose logging")
}
func matchGlob(pattern, path string) bool {

View File

@@ -2,6 +2,7 @@ package scraper
import (
"fmt"
"io/ioutil"
"log"
"math/rand"
"strings"
@@ -12,6 +13,8 @@ import (
"github.com/russross/blackfriday/v2"
)
var logger *log.Logger
var (
pw *playwright.Playwright
browser playwright.Browser
@@ -20,11 +23,21 @@ var (
// Config holds the scraper configuration
type Config struct {
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
func InitPlaywright() error {
log.Println("Initializing Playwright")
logger.Println("Initializing Playwright")
var err error
pw, err = playwright.Run()
if err != nil {
@@ -40,7 +53,7 @@ func InitPlaywright() error {
return fmt.Errorf("could not launch browser: %v", err)
}
log.Println("Playwright initialized successfully")
logger.Println("Playwright initialized successfully")
return nil
}