Skip to content

Commit

Permalink
Merge pull request #35 from hudl/DPL-UpdateLoggingPackage
Browse files Browse the repository at this point in the history
Change loggin function to have f postfix when using formated version
  • Loading branch information
damtur committed Feb 26, 2016
2 parents ae8b023 + e51e8de commit 196ad6d
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 62 deletions.
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type eureka struct {
func ReadConfig(loc string) (conf Config, err error) {
err = gcfg.ReadFileInto(&conf, loc)
if err != nil {
log.Critical("Unable to read config file Error: %s", err.Error())
log.Criticalf("Unable to read config file Error: %s", err.Error())
return conf, err
}
conf.fillDefaults()
Expand Down
6 changes: 3 additions & 3 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func choice(options []string) string {
func NewConnFromConfigFile(location string) (c EurekaConnection, err error) {
cfg, err := ReadConfig(location)
if err != nil {
log.Error("Problem reading config %s error: %s", location, err.Error())
log.Errorf("Problem reading config %s error: %s", location, err.Error())
return c, err
}
return NewConnFromConfig(cfg), nil
Expand Down Expand Up @@ -84,10 +84,10 @@ func NewConn(address ...string) (e EurekaConnection) {
func (e *EurekaConnection) UpdateApp(app *Application) {
go func() {
for {
log.Notice("Updating app %s", app.Name)
log.Noticef("Updating app %s", app.Name)
err := e.readAppInto(app)
if err != nil {
log.Error("Failure updating %s in goroutine", app.Name)
log.Errorf("Failure updating %s in goroutine", app.Name)
}
<-time.After(time.Duration(e.PollInterval) * time.Second)
}
Expand Down
14 changes: 7 additions & 7 deletions dns_discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func retryingFindTXT(fqdn string) (records []string, ttl time.Duration, err erro
func() error {
records, ttl, err = findTXT(fqdn)
if err != nil {
log.Error("Retrying DNS query. Query failed with: %s", err.Error())
log.Errorf("Retrying DNS query. Query failed with: %s", err.Error())
}
return err
}, backoff.NewExponentialBackOff())
Expand All @@ -61,23 +61,23 @@ func findTXT(fqdn string) ([]string, time.Duration, error) {
query.SetQuestion(fqdn, dns.TypeTXT)
dnsServerAddr, err := findDnsServerAddr()
if err != nil {
log.Error("Failure finding DNS server, err=%s", err.Error())
log.Errorf("Failure finding DNS server, err=%s", err.Error())
return nil, defaultTTL, err
}

response, err := dns.Exchange(query, dnsServerAddr)
if err != nil {
log.Error("Failure resolving name %s err=%s", fqdn, err.Error())
log.Errorf("Failure resolving name %s err=%s", fqdn, err.Error())
return nil, defaultTTL, err
}
if len(response.Answer) < 1 {
err := fmt.Errorf("no Eureka discovery TXT record returned for name=%s", fqdn)
log.Error("no answer for name=%s err=%s", fqdn, err.Error())
log.Errorf("no answer for name=%s err=%s", fqdn, err.Error())
return nil, defaultTTL, err
}
if response.Answer[0].Header().Rrtype != dns.TypeTXT {
err := fmt.Errorf("did not receive TXT record back from query specifying TXT record. This should never happen.")
log.Error("Failure resolving name %s err=%s", fqdn, err.Error())
log.Errorf("Failure resolving name %s err=%s", fqdn, err.Error())
return nil, defaultTTL, err
}
txt := response.Answer[0].(*dns.TXT)
Expand All @@ -93,7 +93,7 @@ func findDnsServerAddr() (string, error) {
// Find a DNS server using the OS resolv.conf
config, err := dns.ClientConfigFromFile("/etc/resolv.conf")
if err != nil {
log.Error("Failure finding DNS server address from /etc/resolv.conf, err = %s", err)
log.Errorf("Failure finding DNS server address from /etc/resolv.conf, err = %s", err)
return "", err
} else {
return config.Servers[0] + ":" + config.Port, nil
Expand All @@ -103,7 +103,7 @@ func findDnsServerAddr() (string, error) {
func region() (string, error) {
zone, err := availabilityZone()
if err != nil {
log.Error("Could not retrieve availability zone err=%s", err.Error())
log.Errorf("Could not retrieve availability zone err=%s", err.Error())
return "us-east-1", err
}
return zone[:len(zone)-1], nil
Expand Down
6 changes: 3 additions & 3 deletions marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type getAppsResponseSingle struct {
// UnmarshalJSON is a custom JSON unmarshaler for GetAppsResponse to deal with
// sometimes non-wrapped Application arrays when there is only a single Application item.
func (r *GetAppsResponse) UnmarshalJSON(b []byte) error {
marshalLog.Debug("GetAppsResponse.UnmarshalJSON b:%s\n", string(b))
marshalLog.Debugf("GetAppsResponse.UnmarshalJSON b:%s\n", string(b))
var err error

// Normal array case
Expand Down Expand Up @@ -52,7 +52,7 @@ type applicationSingle struct {
// UnmarshalJSON is a custom JSON unmarshaler for Application to deal with
// sometimes non-wrapped Instance array when there is only a single Instance item.
func (a *Application) UnmarshalJSON(b []byte) error {
marshalLog.Debug("Application.UnmarshalJSON b:%s\n", string(b))
marshalLog.Debugf("Application.UnmarshalJSON b:%s\n", string(b))
var err error

// Normal array case
Expand Down Expand Up @@ -96,7 +96,7 @@ func (i *Instance) UnmarshalJSON(b []byte) error {
func parsePort(s string) int {
n, err := strconv.Atoi(s)
if err != nil {
log.Warning("Invalid port error: %s", err.Error())
log.Warningf("Invalid port error: %s", err.Error())
}
return n
}
Expand Down
8 changes: 4 additions & 4 deletions metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (a *Application) ParseAllMetadata() error {
for _, instance := range a.Instances {
err := instance.Metadata.parse()
if err != nil {
log.Error("Failed parsing metadata for Instance=%s of Application=%s: %s",
log.Errorf("Failed parsing metadata for Instance=%s of Application=%s: %s",
instance.HostName, a.Name, err.Error())
return err
}
Expand All @@ -35,21 +35,21 @@ func (im *InstanceMetadata) parse() error {
log.Debug("len(Metadata)==0. Quitting parsing.")
return nil
}
metadataLog.Debug("InstanceMetadata.parse: %s", im.Raw)
metadataLog.Debugf("InstanceMetadata.parse: %s", im.Raw)

if len(im.Raw) > 0 && im.Raw[0] == '{' {
// JSON
err := json.Unmarshal(im.Raw, &im.parsed)
if err != nil {
log.Error("Error unmarshalling: %s", err.Error())
log.Errorf("Error unmarshalling: %s", err.Error())
return fmt.Errorf("error unmarshalling: %s", err.Error())
}
} else {
// XML: wrap in a BS xml tag so all metadata tags are pulled
fullDoc := append(append([]byte("<d>"), im.Raw...), []byte("</d>")...)
parsedDoc, err := x2j.ByteDocToMap(fullDoc, true)
if err != nil {
log.Error("Error unmarshalling: %s", err.Error())
log.Errorf("Error unmarshalling: %s", err.Error())
return fmt.Errorf("error unmarshalling: %s", err.Error())
}
im.parsed = parsedDoc["d"].(map[string]interface{})
Expand Down
62 changes: 31 additions & 31 deletions net.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (e *EurekaConnection) marshal(v interface{}) ([]byte, error) {
if err != nil {
// marshal the JSON *with* indents so it's readable in the error message
out, _ := json.MarshalIndent(v, "", " ")
log.Error("Error marshalling JSON value=%v. Error:\"%s\" JSON body=\"%s\"", v, err.Error(), string(out))
log.Errorf("Error marshalling JSON value=%v. Error:\"%s\" JSON body=\"%s\"", v, err.Error(), string(out))
return nil, err
}
return out, nil
Expand All @@ -30,7 +30,7 @@ func (e *EurekaConnection) marshal(v interface{}) ([]byte, error) {
if err != nil {
// marshal the XML *with* indents so it's readable in the error message
out, _ := xml.MarshalIndent(v, "", " ")
log.Error("Error marshalling XML value=%v. Error:\"%s\" JSON body=\"%s\"", v, err.Error(), string(out))
log.Errorf("Error marshalling XML value=%v. Error:\"%s\" JSON body=\"%s\"", v, err.Error(), string(out))
return nil, err
}
return out, nil
Expand All @@ -41,18 +41,18 @@ func (e *EurekaConnection) marshal(v interface{}) ([]byte, error) {
func (e *EurekaConnection) GetApp(name string) (*Application, error) {
slug := fmt.Sprintf("%s/%s", EurekaURLSlugs["Apps"], name)
reqURL := e.generateURL(slug)
log.Debug("Getting app %s from url %s", name, reqURL)
log.Debugf("Getting app %s from url %s", name, reqURL)
out, rcode, err := getBody(reqURL, e.UseJson)
if err != nil {
log.Error("Couldn't get app %s, error: %s", name, err.Error())
log.Errorf("Couldn't get app %s, error: %s", name, err.Error())
return nil, err
}
if rcode == 404 {
log.Error("App %s not found (received 404)", name)
log.Errorf("App %s not found (received 404)", name)
return nil, AppNotFoundError{specific: name}
}
if rcode > 299 || rcode < 200 {
log.Warning("Non-200 rcode of %d", rcode)
log.Warningf("Non-200 rcode of %d", rcode)
}

var v *Application
Expand All @@ -64,7 +64,7 @@ func (e *EurekaConnection) GetApp(name string) (*Application, error) {
err = xml.Unmarshal(out, &v)
}
if err != nil {
log.Error("Unmarshalling error: %s", err.Error())
log.Errorf("Unmarshalling error: %s", err.Error())
return nil, err
}

Expand All @@ -84,14 +84,14 @@ func (e *EurekaConnection) readAppInto(app *Application) error {
func (e *EurekaConnection) GetApps() (map[string]*Application, error) {
slug := EurekaURLSlugs["Apps"]
reqURL := e.generateURL(slug)
log.Debug("Getting all apps from url %s", reqURL)
log.Debugf("Getting all apps from url %s", reqURL)
body, rcode, err := getBody(reqURL, e.UseJson)
if err != nil {
log.Error("Couldn't get apps, error: %s", err.Error())
log.Errorf("Couldn't get apps, error: %s", err.Error())
return nil, err
}
if rcode > 299 || rcode < 200 {
log.Warning("Non-200 rcode of %d", rcode)
log.Warningf("Non-200 rcode of %d", rcode)
}

var r *GetAppsResponse
Expand All @@ -103,7 +103,7 @@ func (e *EurekaConnection) GetApps() (map[string]*Application, error) {
err = xml.Unmarshal(body, &r)
}
if err != nil {
log.Error("Unmarshalling error: %s", err.Error())
log.Errorf("Unmarshalling error: %s", err.Error())
return nil, err
}

Expand All @@ -112,7 +112,7 @@ func (e *EurekaConnection) GetApps() (map[string]*Application, error) {
apps[a.Name] = r.Applications[i]
}
for name, app := range apps {
log.Debug("Parsing metadata for app %s", name)
log.Debugf("Parsing metadata for app %s", name)
app.ParseAllMetadata()
}
return apps, nil
Expand All @@ -124,18 +124,18 @@ func (e *EurekaConnection) GetApps() (map[string]*Application, error) {
func (e *EurekaConnection) RegisterInstance(ins *Instance) error {
slug := fmt.Sprintf("%s/%s", EurekaURLSlugs["Apps"], ins.App)
reqURL := e.generateURL(slug)
log.Debug("Registering instance with url %s", reqURL)
log.Debugf("Registering instance with url %s", reqURL)
_, rcode, err := getBody(reqURL+"/"+ins.Id(), e.UseJson)
if err != nil {
log.Error("Failed check if Instance=%s exists in app=%s, error: %s",
log.Errorf("Failed check if Instance=%s exists in app=%s, error: %s",
ins.Id(), ins.App, err.Error())
return err
}
if rcode == 200 {
log.Notice("Instance=%s already exists in App=%s, aborting registration", ins.Id(), ins.App)
log.Noticef("Instance=%s already exists in App=%s, aborting registration", ins.Id(), ins.App)
return nil
}
log.Notice("Instance=%s not yet registered with App=%s, registering.", ins.Id(), ins.App)
log.Noticef("Instance=%s not yet registered with App=%s, registering.", ins.Id(), ins.App)
return e.ReregisterInstance(ins)
}

Expand All @@ -158,11 +158,11 @@ func (e *EurekaConnection) ReregisterInstance(ins *Instance) error {

body, rcode, err := postBody(reqURL, out, e.UseJson)
if err != nil {
log.Error("Could not complete registration, error: %s", err.Error())
log.Errorf("Could not complete registration, error: %s", err.Error())
return err
}
if rcode != 204 {
log.Warning("HTTP returned %d registering Instance=%s App=%s Body=\"%s\"", rcode,
log.Warningf("HTTP returned %d registering Instance=%s App=%s Body=\"%s\"", rcode,
ins.Id(), ins.App, string(body))
return fmt.Errorf("http returned %d possible failure registering instance\n", rcode)
}
Expand All @@ -177,7 +177,7 @@ func (e *EurekaConnection) ReregisterInstance(ins *Instance) error {
func (e *EurekaConnection) GetInstance(app, insId string) (*Instance, error) {
slug := fmt.Sprintf("%s/%s/%s", EurekaURLSlugs["Apps"], app, insId)
reqURL := e.generateURL(slug)
log.Debug("Getting instance with url %s", reqURL)
log.Debugf("Getting instance with url %s", reqURL)
body, rcode, err := getBody(reqURL, e.UseJson)
if err != nil {
return nil, err
Expand Down Expand Up @@ -210,15 +210,15 @@ func (e *EurekaConnection) readInstanceInto(ins *Instance) error {
func (e *EurekaConnection) DeregisterInstance(ins *Instance) error {
slug := fmt.Sprintf("%s/%s/%s", EurekaURLSlugs["Apps"], ins.App, ins.Id())
reqURL := e.generateURL(slug)
log.Debug("Deregistering instance with url %s", reqURL)
log.Debugf("Deregistering instance with url %s", reqURL)

rcode, err := deleteReq(reqURL)
if err != nil {
log.Error("Could not complete deregistration, error: %s", err.Error())
log.Errorf("Could not complete deregistration, error: %s", err.Error())
return err
}
if rcode != 204 {
log.Warning("HTTP returned %d deregistering Instance=%s App=%s", rcode, ins.Id(), ins.App)
log.Warningf("HTTP returned %d deregistering Instance=%s App=%s", rcode, ins.Id(), ins.App)
return fmt.Errorf("http returned %d possible failure deregistering instance\n", rcode)
}

Expand All @@ -232,14 +232,14 @@ func (e EurekaConnection) AddMetadataString(ins *Instance, key, value string) er

params := map[string]string{key: value}

log.Debug("Updating instance metadata url=%s metadata=%s", reqURL, params)
log.Debugf("Updating instance metadata url=%s metadata=%s", reqURL, params)
body, rcode, err := putKV(reqURL, params)
if err != nil {
log.Error("Could not complete update, error: %s", err.Error())
log.Errorf("Could not complete update, error: %s", err.Error())
return err
}
if rcode < 200 || rcode >= 300 {
log.Warning("HTTP returned %d updating metadata Instance=%s App=%s Body=\"%s\"", rcode,
log.Warningf("HTTP returned %d updating metadata Instance=%s App=%s Body=\"%s\"", rcode,
ins.Id(), ins.App, string(body))
return fmt.Errorf("http returned %d possible failure updating instance metadata ", rcode)
}
Expand All @@ -254,14 +254,14 @@ func (e EurekaConnection) UpdateInstanceStatus(ins *Instance, status StatusType)

params := map[string]string{"value": string(status)}

log.Debug("Updating instance status url=%s value=%s", reqURL, status)
log.Debugf("Updating instance status url=%s value=%s", reqURL, status)
body, rcode, err := putKV(reqURL, params)
if err != nil {
log.Error("Could not complete update, error: ", err.Error())
return err
}
if rcode < 200 || rcode >= 300 {
log.Warning("HTTP returned %d updating status Instance=%s App=%s Body=\"%s\"", rcode,
log.Warningf("HTTP returned %d updating status Instance=%s App=%s Body=\"%s\"", rcode,
ins.Id(), ins.App, string(body))
return fmt.Errorf("http returned %d possible failure updating instance status ", rcode)
}
Expand All @@ -273,19 +273,19 @@ func (e EurekaConnection) UpdateInstanceStatus(ins *Instance, status StatusType)
func (e *EurekaConnection) HeartBeatInstance(ins *Instance) error {
slug := fmt.Sprintf("%s/%s/%s", EurekaURLSlugs["Apps"], ins.App, ins.Id())
reqURL := e.generateURL(slug)
log.Debug("Sending heartbeat with url %s", reqURL)
log.Debugf("Sending heartbeat with url %s", reqURL)
req, err := http.NewRequest("PUT", reqURL, nil)
if err != nil {
log.Error("Could not create request for heartbeat, error: %s", err.Error())
log.Errorf("Could not create request for heartbeat, error: %s", err.Error())
return err
}
_, rcode, err := netReq(req)
if err != nil {
log.Error("Error sending heartbeat for Instance=%s App=%s, error: %s", ins.Id(), ins.App, err.Error())
log.Errorf("Error sending heartbeat for Instance=%s App=%s, error: %s", ins.Id(), ins.App, err.Error())
return err
}
if rcode != 200 {
log.Error("Sending heartbeat for Instance=%s App=%s returned code %d", ins.Id(), ins.App, rcode)
log.Errorf("Sending heartbeat for Instance=%s App=%s returned code %d", ins.Id(), ins.App, rcode)
return fmt.Errorf("heartbeat returned code %d\n", rcode)
}
return nil
Expand Down
Loading

0 comments on commit 196ad6d

Please sign in to comment.