feat: Add file-based result logging with date-stamped filename
This commit is contained in:
parent
1d1425aa9a
commit
797ad3c30a
31
main.go
31
main.go
|
@ -4,6 +4,8 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -20,10 +22,19 @@ func main() {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("\nTotal pages checked: %d\n\n", checker.pagesChecked)
|
// Create results file with current date
|
||||||
|
filename := fmt.Sprintf("result-%s.txt", time.Now().Format("2006-01-02"))
|
||||||
|
f, err := os.Create(filename)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
fmt.Fprintf(f, "\nTotal pages checked: %d\n\n", checker.pagesChecked)
|
||||||
|
|
||||||
if len(brokenLinks) == 0 {
|
if len(brokenLinks) == 0 {
|
||||||
fmt.Println("No issues found!")
|
fmt.Fprintf(f, "No issues found!\n")
|
||||||
|
fmt.Printf("Results written to %s\n", filename)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,14 +43,14 @@ func main() {
|
||||||
for _, link := range brokenLinks {
|
for _, link := range brokenLinks {
|
||||||
if link.Redirect != nil {
|
if link.Redirect != nil {
|
||||||
if redirectCount == 0 {
|
if redirectCount == 0 {
|
||||||
fmt.Println("Redirects found:")
|
fmt.Fprintf(f, "Redirects found:\n")
|
||||||
}
|
}
|
||||||
fmt.Printf("- %s (Redirect %d -> %s)\n", link.URL, link.StatusCode, link.Redirect.ToURL)
|
fmt.Fprintf(f, "- %s (Redirect %d -> %s)\n", link.URL, link.StatusCode, link.Redirect.ToURL)
|
||||||
redirectCount++
|
redirectCount++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if redirectCount > 0 {
|
if redirectCount > 0 {
|
||||||
fmt.Println()
|
fmt.Fprintf(f, "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Then list broken links
|
// Then list broken links
|
||||||
|
@ -47,17 +58,19 @@ func main() {
|
||||||
for _, link := range brokenLinks {
|
for _, link := range brokenLinks {
|
||||||
if link.Redirect == nil {
|
if link.Redirect == nil {
|
||||||
if brokenCount == 0 {
|
if brokenCount == 0 {
|
||||||
fmt.Println("Broken links found:")
|
fmt.Fprintf(f, "Broken links found:\n")
|
||||||
}
|
}
|
||||||
if link.Error != "" {
|
if link.Error != "" {
|
||||||
fmt.Printf("- %s (Error: %s)\n", link.URL, link.Error)
|
fmt.Fprintf(f, "- %s (Error: %s)\n", link.URL, link.Error)
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("- %s (Status: %d)\n", link.URL, link.StatusCode)
|
fmt.Fprintf(f, "- %s (Status: %d)\n", link.URL, link.StatusCode)
|
||||||
}
|
}
|
||||||
brokenCount++
|
brokenCount++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("\nTotal issues: %d (%d redirects, %d broken)\n",
|
fmt.Fprintf(f, "\nTotal issues: %d (%d redirects, %d broken)\n",
|
||||||
len(brokenLinks), redirectCount, brokenCount)
|
len(brokenLinks), redirectCount, brokenCount)
|
||||||
|
|
||||||
|
fmt.Printf("Results written to %s\n", filename)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue