Add logging

This commit is contained in:
Mike Cheng 2015-05-04 20:03:17 -04:00
parent be38e512b3
commit 75b0734bf8
4 changed files with 52 additions and 20 deletions

View file

@ -116,8 +116,6 @@ func (api API) DeleteDNSRecord(domain, recordId string) error {
return err return err
} }
fmt.Println("Body:", string(b))
resp, err := api.performRequest( resp, err := api.performRequest(
"POST", "POST",
fmt.Sprintf("%s%s%s", api.baseUrl, "api/dns/delete/", domain), fmt.Sprintf("%s%s%s", api.baseUrl, "api/dns/delete/", domain),
@ -127,8 +125,6 @@ func (api API) DeleteDNSRecord(domain, recordId string) error {
return err return err
} }
fmt.Println("Response:", string(resp))
var result struct { var result struct {
Result resultResponse Result resultResponse
} }

View file

@ -1,8 +1,8 @@
package dyndns package dyndns
import ( import (
"fmt"
"github.com/mfycheng/name-dyndns/api" "github.com/mfycheng/name-dyndns/api"
"github.com/mfycheng/name-dyndns/log"
"sync" "sync"
"time" "time"
) )
@ -10,14 +10,13 @@ import (
var wg sync.WaitGroup var wg sync.WaitGroup
func updateDNSRecord(a api.API, domain, recordId string, newRecord api.DNSRecord) error { func updateDNSRecord(a api.API, domain, recordId string, newRecord api.DNSRecord) error {
fmt.Println("Deleting record...") log.Logger.Printf("Deleting DNS record for %s.%s.\n", newRecord.Name, domain)
err := a.DeleteDNSRecord(domain, newRecord.RecordId) err := a.DeleteDNSRecord(domain, newRecord.RecordId)
if err != nil { if err != nil {
return err return err
} }
// Does a /create/ overwrite? or do we have to delete first? log.Logger.Printf("Creating DNS record for %s.%s: %s\n", newRecord.Name, domain, newRecord)
fmt.Println("Creating record")
return a.CreateDNSRecord(domain, newRecord) return a.CreateDNSRecord(domain, newRecord)
} }
@ -26,14 +25,15 @@ func runConfig(c api.Config, daemon bool) {
a := api.NewAPIFromConfig(c) a := api.NewAPIFromConfig(c)
for { for {
log.Logger.Printf("Running update check for %s.", c.Domain)
ip, err := GetExternalIP() ip, err := GetExternalIP()
if err != nil { if err != nil {
fmt.Print("Fail to retreive IP: ") log.Logger.Print("Failed to retreive IP: ")
if daemon { if daemon {
fmt.Println("Will retry...") log.Logger.Println("Will retry...")
continue continue
} else { } else {
fmt.Println("Giving up.") log.Logger.Println("Giving up.")
break break
} }
} }
@ -44,28 +44,30 @@ func runConfig(c api.Config, daemon bool) {
// update it. // update it.
records, err := a.GetRecords(c.Domain) records, err := a.GetRecords(c.Domain)
if err != nil { if err != nil {
fmt.Print("Failed to retreive records for:%s\n", c.Domain) log.Logger.Printf("Failed to retreive records for%s\n", c.Domain)
if daemon { if daemon {
fmt.Println("Will retry...") log.Logger.Print("Will retry...")
continue continue
} else { } else {
fmt.Println("Giving up.") log.Logger.Print("Giving up.")
break break
} }
} }
for _, r := range records { for _, r := range records {
if r.Content != ip { if r.Content != ip {
fmt.Printf("Updating record %s for %s\n", r.RecordId, c.Domain)
r.Content = ip r.Content = ip
err = updateDNSRecord(a, c.Domain, r.RecordId, r) err = updateDNSRecord(a, c.Domain, r.RecordId, r)
if err != nil { if err != nil {
fmt.Println("Failed to update record.", err) log.Logger.Printf("Failed to update record %s [%s.%s] with IP: %s\n\t%s\n", r.RecordId, r.Name, c.Domain, ip, err)
} else {
log.Logger.Printf("Attempting to update record %s [%s.%s] with IP: %s\n", r.RecordId, r.Name, c.Domain, ip)
} }
} }
} }
if !daemon { if !daemon {
log.Logger.Println("Non daemon mode, stopping.")
return return
} }

12
log/log.go Normal file
View file

@ -0,0 +1,12 @@
package log
import (
"io"
"log"
)
var Logger *log.Logger
func Init(writer io.Writer) {
Logger = log.New(writer, "", log.Ldate|log.Ltime)
}

30
main.go
View file

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"github.com/mfycheng/name-dyndns/api" "github.com/mfycheng/name-dyndns/api"
"github.com/mfycheng/name-dyndns/dyndns" "github.com/mfycheng/name-dyndns/dyndns"
"github.com/mfycheng/name-dyndns/log"
"os" "os"
) )
@ -22,17 +23,38 @@ func main() {
configPath := flag.String("config", "./config.json", "Specify the configuration file") configPath := flag.String("config", "./config.json", "Specify the configuration file")
daemon := flag.Bool("daemon", false, "Operate in daemon mode.") daemon := flag.Bool("daemon", false, "Operate in daemon mode.")
dev := flag.Bool("dev", false, "Use development configurations instead.") dev := flag.Bool("dev", false, "Use development configurations instead.")
logFile := flag.String("log", "", "Specify a logfile. If no file is provided, uses stdout.")
flag.Parse() flag.Parse()
var file *os.File
defer file.Close()
if *logFile == "" {
file = os.Stdout
} else {
var err error
file, err = os.OpenFile(*logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
fmt.Println("Could not open log for reading")
os.Exit(1)
}
}
log.Init(file)
configs, err := api.LoadConfigs(*configPath) configs, err := api.LoadConfigs(*configPath)
if err != nil { if err != nil {
fmt.Println("Error loading config:", err) log.Logger.Fatalln("Error loading config:", err)
os.Exit(1) }
return
for _, config := range configs {
if config.Domain == "" || len(config.Hostnames) == 0 {
log.Logger.Fatalf("Empty configuration detected. Exiting.")
}
} }
configs = filterConfigs(configs, *dev) configs = filterConfigs(configs, *dev)
fmt.Printf("Successfully loaded %d configs\n", len(configs)) log.Logger.Printf("Successfully loaded %d configs\n", len(configs))
dyndns.Run(configs, *daemon) dyndns.Run(configs, *daemon)
} }