Skip to content

Commit 196ad6d

Browse files
committed
Merge pull request #35 from hudl/DPL-UpdateLoggingPackage
Change loggin function to have f postfix when using formated version
2 parents ae8b023 + e51e8de commit 196ad6d

File tree

7 files changed

+62
-62
lines changed

7 files changed

+62
-62
lines changed

config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type eureka struct {
4646
func ReadConfig(loc string) (conf Config, err error) {
4747
err = gcfg.ReadFileInto(&conf, loc)
4848
if err != nil {
49-
log.Critical("Unable to read config file Error: %s", err.Error())
49+
log.Criticalf("Unable to read config file Error: %s", err.Error())
5050
return conf, err
5151
}
5252
conf.fillDefaults()

connection.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func choice(options []string) string {
4646
func NewConnFromConfigFile(location string) (c EurekaConnection, err error) {
4747
cfg, err := ReadConfig(location)
4848
if err != nil {
49-
log.Error("Problem reading config %s error: %s", location, err.Error())
49+
log.Errorf("Problem reading config %s error: %s", location, err.Error())
5050
return c, err
5151
}
5252
return NewConnFromConfig(cfg), nil
@@ -84,10 +84,10 @@ func NewConn(address ...string) (e EurekaConnection) {
8484
func (e *EurekaConnection) UpdateApp(app *Application) {
8585
go func() {
8686
for {
87-
log.Notice("Updating app %s", app.Name)
87+
log.Noticef("Updating app %s", app.Name)
8888
err := e.readAppInto(app)
8989
if err != nil {
90-
log.Error("Failure updating %s in goroutine", app.Name)
90+
log.Errorf("Failure updating %s in goroutine", app.Name)
9191
}
9292
<-time.After(time.Duration(e.PollInterval) * time.Second)
9393
}

dns_discover.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func retryingFindTXT(fqdn string) (records []string, ttl time.Duration, err erro
4848
func() error {
4949
records, ttl, err = findTXT(fqdn)
5050
if err != nil {
51-
log.Error("Retrying DNS query. Query failed with: %s", err.Error())
51+
log.Errorf("Retrying DNS query. Query failed with: %s", err.Error())
5252
}
5353
return err
5454
}, backoff.NewExponentialBackOff())
@@ -61,23 +61,23 @@ func findTXT(fqdn string) ([]string, time.Duration, error) {
6161
query.SetQuestion(fqdn, dns.TypeTXT)
6262
dnsServerAddr, err := findDnsServerAddr()
6363
if err != nil {
64-
log.Error("Failure finding DNS server, err=%s", err.Error())
64+
log.Errorf("Failure finding DNS server, err=%s", err.Error())
6565
return nil, defaultTTL, err
6666
}
6767

6868
response, err := dns.Exchange(query, dnsServerAddr)
6969
if err != nil {
70-
log.Error("Failure resolving name %s err=%s", fqdn, err.Error())
70+
log.Errorf("Failure resolving name %s err=%s", fqdn, err.Error())
7171
return nil, defaultTTL, err
7272
}
7373
if len(response.Answer) < 1 {
7474
err := fmt.Errorf("no Eureka discovery TXT record returned for name=%s", fqdn)
75-
log.Error("no answer for name=%s err=%s", fqdn, err.Error())
75+
log.Errorf("no answer for name=%s err=%s", fqdn, err.Error())
7676
return nil, defaultTTL, err
7777
}
7878
if response.Answer[0].Header().Rrtype != dns.TypeTXT {
7979
err := fmt.Errorf("did not receive TXT record back from query specifying TXT record. This should never happen.")
80-
log.Error("Failure resolving name %s err=%s", fqdn, err.Error())
80+
log.Errorf("Failure resolving name %s err=%s", fqdn, err.Error())
8181
return nil, defaultTTL, err
8282
}
8383
txt := response.Answer[0].(*dns.TXT)
@@ -93,7 +93,7 @@ func findDnsServerAddr() (string, error) {
9393
// Find a DNS server using the OS resolv.conf
9494
config, err := dns.ClientConfigFromFile("/etc/resolv.conf")
9595
if err != nil {
96-
log.Error("Failure finding DNS server address from /etc/resolv.conf, err = %s", err)
96+
log.Errorf("Failure finding DNS server address from /etc/resolv.conf, err = %s", err)
9797
return "", err
9898
} else {
9999
return config.Servers[0] + ":" + config.Port, nil
@@ -103,7 +103,7 @@ func findDnsServerAddr() (string, error) {
103103
func region() (string, error) {
104104
zone, err := availabilityZone()
105105
if err != nil {
106-
log.Error("Could not retrieve availability zone err=%s", err.Error())
106+
log.Errorf("Could not retrieve availability zone err=%s", err.Error())
107107
return "us-east-1", err
108108
}
109109
return zone[:len(zone)-1], nil

marshal.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type getAppsResponseSingle struct {
1919
// UnmarshalJSON is a custom JSON unmarshaler for GetAppsResponse to deal with
2020
// sometimes non-wrapped Application arrays when there is only a single Application item.
2121
func (r *GetAppsResponse) UnmarshalJSON(b []byte) error {
22-
marshalLog.Debug("GetAppsResponse.UnmarshalJSON b:%s\n", string(b))
22+
marshalLog.Debugf("GetAppsResponse.UnmarshalJSON b:%s\n", string(b))
2323
var err error
2424

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

5858
// Normal array case
@@ -96,7 +96,7 @@ func (i *Instance) UnmarshalJSON(b []byte) error {
9696
func parsePort(s string) int {
9797
n, err := strconv.Atoi(s)
9898
if err != nil {
99-
log.Warning("Invalid port error: %s", err.Error())
99+
log.Warningf("Invalid port error: %s", err.Error())
100100
}
101101
return n
102102
}

metadata.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func (a *Application) ParseAllMetadata() error {
1313
for _, instance := range a.Instances {
1414
err := instance.Metadata.parse()
1515
if err != nil {
16-
log.Error("Failed parsing metadata for Instance=%s of Application=%s: %s",
16+
log.Errorf("Failed parsing metadata for Instance=%s of Application=%s: %s",
1717
instance.HostName, a.Name, err.Error())
1818
return err
1919
}
@@ -35,21 +35,21 @@ func (im *InstanceMetadata) parse() error {
3535
log.Debug("len(Metadata)==0. Quitting parsing.")
3636
return nil
3737
}
38-
metadataLog.Debug("InstanceMetadata.parse: %s", im.Raw)
38+
metadataLog.Debugf("InstanceMetadata.parse: %s", im.Raw)
3939

4040
if len(im.Raw) > 0 && im.Raw[0] == '{' {
4141
// JSON
4242
err := json.Unmarshal(im.Raw, &im.parsed)
4343
if err != nil {
44-
log.Error("Error unmarshalling: %s", err.Error())
44+
log.Errorf("Error unmarshalling: %s", err.Error())
4545
return fmt.Errorf("error unmarshalling: %s", err.Error())
4646
}
4747
} else {
4848
// XML: wrap in a BS xml tag so all metadata tags are pulled
4949
fullDoc := append(append([]byte("<d>"), im.Raw...), []byte("</d>")...)
5050
parsedDoc, err := x2j.ByteDocToMap(fullDoc, true)
5151
if err != nil {
52-
log.Error("Error unmarshalling: %s", err.Error())
52+
log.Errorf("Error unmarshalling: %s", err.Error())
5353
return fmt.Errorf("error unmarshalling: %s", err.Error())
5454
}
5555
im.parsed = parsedDoc["d"].(map[string]interface{})

net.go

+31-31
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (e *EurekaConnection) marshal(v interface{}) ([]byte, error) {
2121
if err != nil {
2222
// marshal the JSON *with* indents so it's readable in the error message
2323
out, _ := json.MarshalIndent(v, "", " ")
24-
log.Error("Error marshalling JSON value=%v. Error:\"%s\" JSON body=\"%s\"", v, err.Error(), string(out))
24+
log.Errorf("Error marshalling JSON value=%v. Error:\"%s\" JSON body=\"%s\"", v, err.Error(), string(out))
2525
return nil, err
2626
}
2727
return out, nil
@@ -30,7 +30,7 @@ func (e *EurekaConnection) marshal(v interface{}) ([]byte, error) {
3030
if err != nil {
3131
// marshal the XML *with* indents so it's readable in the error message
3232
out, _ := xml.MarshalIndent(v, "", " ")
33-
log.Error("Error marshalling XML value=%v. Error:\"%s\" JSON body=\"%s\"", v, err.Error(), string(out))
33+
log.Errorf("Error marshalling XML value=%v. Error:\"%s\" JSON body=\"%s\"", v, err.Error(), string(out))
3434
return nil, err
3535
}
3636
return out, nil
@@ -41,18 +41,18 @@ func (e *EurekaConnection) marshal(v interface{}) ([]byte, error) {
4141
func (e *EurekaConnection) GetApp(name string) (*Application, error) {
4242
slug := fmt.Sprintf("%s/%s", EurekaURLSlugs["Apps"], name)
4343
reqURL := e.generateURL(slug)
44-
log.Debug("Getting app %s from url %s", name, reqURL)
44+
log.Debugf("Getting app %s from url %s", name, reqURL)
4545
out, rcode, err := getBody(reqURL, e.UseJson)
4646
if err != nil {
47-
log.Error("Couldn't get app %s, error: %s", name, err.Error())
47+
log.Errorf("Couldn't get app %s, error: %s", name, err.Error())
4848
return nil, err
4949
}
5050
if rcode == 404 {
51-
log.Error("App %s not found (received 404)", name)
51+
log.Errorf("App %s not found (received 404)", name)
5252
return nil, AppNotFoundError{specific: name}
5353
}
5454
if rcode > 299 || rcode < 200 {
55-
log.Warning("Non-200 rcode of %d", rcode)
55+
log.Warningf("Non-200 rcode of %d", rcode)
5656
}
5757

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

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

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

@@ -112,7 +112,7 @@ func (e *EurekaConnection) GetApps() (map[string]*Application, error) {
112112
apps[a.Name] = r.Applications[i]
113113
}
114114
for name, app := range apps {
115-
log.Debug("Parsing metadata for app %s", name)
115+
log.Debugf("Parsing metadata for app %s", name)
116116
app.ParseAllMetadata()
117117
}
118118
return apps, nil
@@ -124,18 +124,18 @@ func (e *EurekaConnection) GetApps() (map[string]*Application, error) {
124124
func (e *EurekaConnection) RegisterInstance(ins *Instance) error {
125125
slug := fmt.Sprintf("%s/%s", EurekaURLSlugs["Apps"], ins.App)
126126
reqURL := e.generateURL(slug)
127-
log.Debug("Registering instance with url %s", reqURL)
127+
log.Debugf("Registering instance with url %s", reqURL)
128128
_, rcode, err := getBody(reqURL+"/"+ins.Id(), e.UseJson)
129129
if err != nil {
130-
log.Error("Failed check if Instance=%s exists in app=%s, error: %s",
130+
log.Errorf("Failed check if Instance=%s exists in app=%s, error: %s",
131131
ins.Id(), ins.App, err.Error())
132132
return err
133133
}
134134
if rcode == 200 {
135-
log.Notice("Instance=%s already exists in App=%s, aborting registration", ins.Id(), ins.App)
135+
log.Noticef("Instance=%s already exists in App=%s, aborting registration", ins.Id(), ins.App)
136136
return nil
137137
}
138-
log.Notice("Instance=%s not yet registered with App=%s, registering.", ins.Id(), ins.App)
138+
log.Noticef("Instance=%s not yet registered with App=%s, registering.", ins.Id(), ins.App)
139139
return e.ReregisterInstance(ins)
140140
}
141141

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

159159
body, rcode, err := postBody(reqURL, out, e.UseJson)
160160
if err != nil {
161-
log.Error("Could not complete registration, error: %s", err.Error())
161+
log.Errorf("Could not complete registration, error: %s", err.Error())
162162
return err
163163
}
164164
if rcode != 204 {
165-
log.Warning("HTTP returned %d registering Instance=%s App=%s Body=\"%s\"", rcode,
165+
log.Warningf("HTTP returned %d registering Instance=%s App=%s Body=\"%s\"", rcode,
166166
ins.Id(), ins.App, string(body))
167167
return fmt.Errorf("http returned %d possible failure registering instance\n", rcode)
168168
}
@@ -177,7 +177,7 @@ func (e *EurekaConnection) ReregisterInstance(ins *Instance) error {
177177
func (e *EurekaConnection) GetInstance(app, insId string) (*Instance, error) {
178178
slug := fmt.Sprintf("%s/%s/%s", EurekaURLSlugs["Apps"], app, insId)
179179
reqURL := e.generateURL(slug)
180-
log.Debug("Getting instance with url %s", reqURL)
180+
log.Debugf("Getting instance with url %s", reqURL)
181181
body, rcode, err := getBody(reqURL, e.UseJson)
182182
if err != nil {
183183
return nil, err
@@ -210,15 +210,15 @@ func (e *EurekaConnection) readInstanceInto(ins *Instance) error {
210210
func (e *EurekaConnection) DeregisterInstance(ins *Instance) error {
211211
slug := fmt.Sprintf("%s/%s/%s", EurekaURLSlugs["Apps"], ins.App, ins.Id())
212212
reqURL := e.generateURL(slug)
213-
log.Debug("Deregistering instance with url %s", reqURL)
213+
log.Debugf("Deregistering instance with url %s", reqURL)
214214

215215
rcode, err := deleteReq(reqURL)
216216
if err != nil {
217-
log.Error("Could not complete deregistration, error: %s", err.Error())
217+
log.Errorf("Could not complete deregistration, error: %s", err.Error())
218218
return err
219219
}
220220
if rcode != 204 {
221-
log.Warning("HTTP returned %d deregistering Instance=%s App=%s", rcode, ins.Id(), ins.App)
221+
log.Warningf("HTTP returned %d deregistering Instance=%s App=%s", rcode, ins.Id(), ins.App)
222222
return fmt.Errorf("http returned %d possible failure deregistering instance\n", rcode)
223223
}
224224

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

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

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

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

257-
log.Debug("Updating instance status url=%s value=%s", reqURL, status)
257+
log.Debugf("Updating instance status url=%s value=%s", reqURL, status)
258258
body, rcode, err := putKV(reqURL, params)
259259
if err != nil {
260260
log.Error("Could not complete update, error: ", err.Error())
261261
return err
262262
}
263263
if rcode < 200 || rcode >= 300 {
264-
log.Warning("HTTP returned %d updating status Instance=%s App=%s Body=\"%s\"", rcode,
264+
log.Warningf("HTTP returned %d updating status Instance=%s App=%s Body=\"%s\"", rcode,
265265
ins.Id(), ins.App, string(body))
266266
return fmt.Errorf("http returned %d possible failure updating instance status ", rcode)
267267
}
@@ -273,19 +273,19 @@ func (e EurekaConnection) UpdateInstanceStatus(ins *Instance, status StatusType)
273273
func (e *EurekaConnection) HeartBeatInstance(ins *Instance) error {
274274
slug := fmt.Sprintf("%s/%s/%s", EurekaURLSlugs["Apps"], ins.App, ins.Id())
275275
reqURL := e.generateURL(slug)
276-
log.Debug("Sending heartbeat with url %s", reqURL)
276+
log.Debugf("Sending heartbeat with url %s", reqURL)
277277
req, err := http.NewRequest("PUT", reqURL, nil)
278278
if err != nil {
279-
log.Error("Could not create request for heartbeat, error: %s", err.Error())
279+
log.Errorf("Could not create request for heartbeat, error: %s", err.Error())
280280
return err
281281
}
282282
_, rcode, err := netReq(req)
283283
if err != nil {
284-
log.Error("Error sending heartbeat for Instance=%s App=%s, error: %s", ins.Id(), ins.App, err.Error())
284+
log.Errorf("Error sending heartbeat for Instance=%s App=%s, error: %s", ins.Id(), ins.App, err.Error())
285285
return err
286286
}
287287
if rcode != 200 {
288-
log.Error("Sending heartbeat for Instance=%s App=%s returned code %d", ins.Id(), ins.App, rcode)
288+
log.Errorf("Sending heartbeat for Instance=%s App=%s returned code %d", ins.Id(), ins.App, rcode)
289289
return fmt.Errorf("heartbeat returned code %d\n", rcode)
290290
}
291291
return nil

0 commit comments

Comments
 (0)