Move to log instead of fmt.Print

This commit is contained in:
GRMrGecko 2024-08-01 09:01:40 -05:00
parent cc9e7d4f22
commit 4ab5a7fd1d
2 changed files with 8 additions and 6 deletions

11
main.go
View File

@ -5,6 +5,7 @@ import (
"crypto/md5" "crypto/md5"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"log"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
@ -17,7 +18,7 @@ import (
const ( const (
serviceName = "nginx-cache-purge" serviceName = "nginx-cache-purge"
serviceDescription = "Tool to help purge Nginx cache " serviceDescription = "Tool to help purge Nginx cache "
serviceVersion = "0.1.1" serviceVersion = "0.1.2"
) )
// App structure to access global app variables. // App structure to access global app variables.
@ -62,7 +63,7 @@ func (a *App) PurgeCache(CachePath string, Key string, ExcludeKeys []string) err
if !globRegex.MatchString(Key) { if !globRegex.MatchString(Key) {
// If excluded, skip the key. // If excluded, skip the key.
if keyIsExcluded(Key) { if keyIsExcluded(Key) {
fmt.Println("Key", Key, "is excluded, will not purge.") log.Println("Key", Key, "is excluded, will not purge.")
return nil return nil
} }
@ -82,7 +83,7 @@ func (a *App) PurgeCache(CachePath string, Key string, ExcludeKeys []string) err
} }
// If this file matches our key hash then delete. // If this file matches our key hash then delete.
if info.Name() == keyHash { if info.Name() == keyHash {
fmt.Printf("Purging %s as it matches the key %s requested to be purged.\n", filePath, Key) log.Printf("Purging %s as it matches the key %s requested to be purged.\n", filePath, Key)
err := os.Remove(filePath) err := os.Remove(filePath)
if err != nil { if err != nil {
return err return err
@ -127,12 +128,12 @@ func (a *App) PurgeCache(CachePath string, Key string, ExcludeKeys []string) err
if g.Match(keyRead) { if g.Match(keyRead) {
// If excluded, skip the key. // If excluded, skip the key.
if keyIsExcluded(keyRead) { if keyIsExcluded(keyRead) {
fmt.Println("Key", keyRead, "is excluded, will not purge.") log.Println("Key", keyRead, "is excluded, will not purge.")
return nil return nil
} }
// Delete the file. // Delete the file.
fmt.Printf("Purging %s as it matches the key %s requested to be purged.\n", filePath, Key) log.Printf("Purging %s as it matches the key %s requested to be purged.\n", filePath, Key)
err := os.Remove(filePath) err := os.Remove(filePath)
if err != nil { if err != nil {
return err return err

View File

@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"io" "io"
"log"
"net" "net"
"net/http" "net/http"
"os" "os"
@ -66,7 +67,7 @@ func (a *ServerCmd) Run() error {
defer listener.Close() defer listener.Close()
// Start the FastCGI server. // Start the FastCGI server.
fmt.Println("Starting server at", unixSocket) log.Println("Starting server at", unixSocket)
http.HandleFunc("/", a.ServeHTTP) http.HandleFunc("/", a.ServeHTTP)
err = http.Serve(listener, nil) err = http.Serve(listener, nil)