Remove withFields

Signed-off-by: Jean-Laurent de Morlhon <jeanlaurent@morlhon.net>
This commit is contained in:
Jean-Laurent de Morlhon 2015-12-03 17:22:36 +01:00
parent 2e79f67e93
commit 1079735af8
8 changed files with 37 additions and 108 deletions

View File

@ -477,7 +477,7 @@ func (c *GenericClient) Authenticate(d *Driver) error {
return nil
}
log.WithFields(log.Fields{
log.Debug("Authenticating...", map[string]interface{}{
"AuthUrl": d.AuthUrl,
"Insecure": d.Insecure,
"DomainID": d.DomainID,
@ -485,7 +485,7 @@ func (c *GenericClient) Authenticate(d *Driver) error {
"Username": d.Username,
"TenantName": d.TenantName,
"TenantID": d.TenantId,
}).Debug("Authenticating...")
})
opts := gophercloud.AuthOptions{
IdentityEndpoint: d.AuthUrl,

View File

@ -278,7 +278,7 @@ func (d *Driver) GetIP() (string, error) {
return d.IPAddress, nil
}
log.WithField("MachineId", d.MachineId).Debug("Looking for the IP address...")
log.Debug("Looking for the IP address...", map[string]string{"MachineId": d.MachineId})
if err := d.initCompute(); err != nil {
return "", err
@ -306,7 +306,7 @@ func (d *Driver) GetIP() (string, error) {
}
func (d *Driver) GetState() (state.State, error) {
log.WithField("MachineId", d.MachineId).Debug("Get status for OpenStack instance...")
log.Debug("Get status for OpenStack instance...", map[string]string{"MachineId": d.MachineId})
if err := d.initCompute(); err != nil {
return state.None, err
}
@ -316,10 +316,10 @@ func (d *Driver) GetState() (state.State, error) {
return state.None, err
}
log.WithFields(log.Fields{
log.Debug("State for OpenStack instance", map[string]string{
"MachineId": d.MachineId,
"State": s,
}).Debug("State for OpenStack instance")
})
switch s {
case "ACTIVE":
@ -365,7 +365,7 @@ func (d *Driver) Create() error {
}
func (d *Driver) Start() error {
log.WithField("MachineId", d.MachineId).Info("Starting OpenStack instance...")
log.Debug("Starting OpenStack instance...", map[string]string{"MachineId": d.MachineId})
if err := d.initCompute(); err != nil {
return err
}
@ -376,7 +376,7 @@ func (d *Driver) Start() error {
}
func (d *Driver) Stop() error {
log.WithField("MachineId", d.MachineId).Info("Stopping OpenStack instance...")
log.Debug("Stopping OpenStack instance...", map[string]string{"MachineId": d.MachineId})
if err := d.initCompute(); err != nil {
return err
}
@ -388,7 +388,7 @@ func (d *Driver) Stop() error {
}
func (d *Driver) Remove() error {
log.WithField("MachineId", d.MachineId).Debug("deleting instance...")
log.Debug("deleting instance...", map[string]string{"MachineId": d.MachineId})
log.Info("Deleting OpenStack instance...")
if err := d.initCompute(); err != nil {
return err
@ -396,7 +396,7 @@ func (d *Driver) Remove() error {
if err := d.client.DeleteInstance(d); err != nil {
return err
}
log.WithField("Name", d.KeyPairName).Debug("deleting key pair...")
log.Debug("deleting key pair...", map[string]string{"Name": d.KeyPairName})
if err := d.client.DeleteKeyPair(d, d.KeyPairName); err != nil {
return err
}
@ -404,7 +404,7 @@ func (d *Driver) Remove() error {
}
func (d *Driver) Restart() error {
log.WithField("MachineId", d.MachineId).Info("Restarting OpenStack instance...")
log.Info("Restarting OpenStack instance...", map[string]string{"MachineId": d.MachineId})
if err := d.initCompute(); err != nil {
return err
}
@ -483,10 +483,10 @@ func (d *Driver) resolveIds() error {
}
d.NetworkId = networkID
log.WithFields(log.Fields{
log.Debug("Found network id using its name", map[string]string{
"Name": d.NetworkName,
"ID": d.NetworkId,
}).Debug("Found network id using its name")
})
}
if d.FlavorName != "" {
@ -504,10 +504,10 @@ func (d *Driver) resolveIds() error {
}
d.FlavorId = flavorID
log.WithFields(log.Fields{
log.Debug("Found flavor id using its name", map[string]string{
"Name": d.FlavorName,
"ID": d.FlavorId,
}).Debug("Found flavor id using its name")
})
}
if d.ImageName != "" {
@ -525,10 +525,10 @@ func (d *Driver) resolveIds() error {
}
d.ImageId = imageID
log.WithFields(log.Fields{
log.Debug("Found image id using its name", map[string]string{
"Name": d.ImageName,
"ID": d.ImageId,
}).Debug("Found image id using its name")
})
}
if d.FloatingIpPool != "" && !d.ComputeNetwork {
@ -546,10 +546,10 @@ func (d *Driver) resolveIds() error {
}
d.FloatingIpPoolId = f
log.WithFields(log.Fields{
log.Debug("Found floating IP pool id using its name", map[string]string{
"Name": d.FloatingIpPool,
"ID": d.FloatingIpPoolId,
}).Debug("Found floating IP pool id using its name")
})
}
return nil
@ -577,7 +577,7 @@ func (d *Driver) initNetwork() error {
func (d *Driver) createSSHKey() error {
sanitizeKeyPairName(&d.KeyPairName)
log.WithField("Name", d.KeyPairName).Debug("Creating Key Pair...")
log.Debug("Creating Key Pair...", map[string]string{"Name": d.KeyPairName})
if err := ssh.GenerateSSHKey(d.GetSSHKeyPath()); err != nil {
return err
}
@ -596,10 +596,10 @@ func (d *Driver) createSSHKey() error {
}
func (d *Driver) createMachine() error {
log.WithFields(log.Fields{
log.Debug("Creating OpenStack instance...", map[string]string{
"FlavorId": d.FlavorId,
"ImageId": d.ImageId,
}).Debug("Creating OpenStack instance...")
})
if err := d.initCompute(); err != nil {
return err
@ -632,17 +632,17 @@ func (d *Driver) assignFloatingIP() error {
var floatingIP *FloatingIP
log.WithFields(log.Fields{
log.Debugf("Looking for an available floating IP", map[string]string{
"MachineId": d.MachineId,
"Pool": d.FloatingIpPool,
}).Debugf("Looking for an available floating IP")
})
for _, ip := range ips {
if ip.PortId == "" {
log.WithFields(log.Fields{
log.Debug("Available floating IP found", map[string]string{
"MachineId": d.MachineId,
"IP": ip.Ip,
}).Debugf("Available floating IP found")
})
floatingIP = &ip
break
}
@ -650,9 +650,9 @@ func (d *Driver) assignFloatingIP() error {
if floatingIP == nil {
floatingIP = &FloatingIP{}
log.WithField("MachineId", d.MachineId).Debugf("No available floating IP found. Allocating a new one...")
log.Debug("No available floating IP found. Allocating a new one...", map[string]string{"MachineId": d.MachineId})
} else {
log.WithField("MachineId", d.MachineId).Debugf("Assigning floating IP to the instance")
log.Debug("Assigning floating IP to the instance", map[string]string{"MachineId": d.MachineId})
}
if err := d.client.AssignFloatingIP(d, floatingIP); err != nil {
@ -663,7 +663,7 @@ func (d *Driver) assignFloatingIP() error {
}
func (d *Driver) waitForInstanceActive() error {
log.WithField("MachineId", d.MachineId).Debug("Waiting for the OpenStack instance to be ACTIVE...")
log.Debug("Waiting for the OpenStack instance to be ACTIVE...", map[string]string{"MachineId": d.MachineId})
if err := d.client.WaitForInstanceStatus(d, "ACTIVE"); err != nil {
return err
}
@ -676,10 +676,10 @@ func (d *Driver) lookForIPAddress() error {
return err
}
d.IPAddress = ip
log.WithFields(log.Fields{
log.Debug("IP address found", map[string]string{
"IP": ip,
"MachineId": d.MachineId,
}).Debug("IP address found")
})
return nil
}

View File

@ -26,9 +26,9 @@ func (c *Client) Authenticate(d *openstack.Driver) error {
return nil
}
log.WithFields(log.Fields{
log.Debug("Authenticating to Rackspace.", map[string]string{
"Username": d.Username,
}).Debug("Authenticating to Rackspace.")
})
apiKey := c.driver.APIKey
opts := gophercloud.AuthOptions{

View File

@ -82,9 +82,7 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
// NewDriver instantiates a Rackspace driver.
func NewDriver(machineName, storePath string) drivers.Driver {
log.WithFields(log.Fields{
"machineName": machineName,
}).Debug("Instantiating Rackspace driver.")
log.Debug("Instantiating Rackspace driver.", map[string]string{"machineName": machineName})
inner := openstack.NewDerivedDriver(machineName, storePath)
driver := &Driver{

View File

@ -15,6 +15,4 @@ type Logger interface {
Fatal(...interface{})
Fatalf(string, ...interface{})
WithFields(Fields) Logger
}

View File

@ -14,8 +14,6 @@ var (
IsDebug = false
)
type Fields map[string]interface{}
// RedirectStdOutToStdErr prevents any log from corrupting the output
func RedirectStdOutToStdErr() {
l.OutWriter = l.ErrWriter
@ -61,14 +59,6 @@ func Warnf(fmtString string, args ...interface{}) {
l.Warnf(fmtString, args...)
}
func WithField(fieldName string, field interface{}) Logger {
return l.WithFields(Fields{
fieldName: field,
})
}
func WithFields(fields Fields) Logger {
return l.WithFields(fields)
func GetStandardLogger() *StandardLogger {
return &l
}

View File

@ -1,19 +0,0 @@
package log
import "testing"
func TestStandardLoggerWithFields(t *testing.T) {
logger := StandardLogger{}
withFieldsLogger := logger.WithFields(Fields{
"foo": "bar",
"spam": "eggs",
})
withFieldsStandardLogger, ok := withFieldsLogger.(StandardLogger)
if !ok {
t.Fatal("Type assertion to StandardLogger failed")
}
expectedOutFields := "\t\t foo=bar spam=eggs"
if withFieldsStandardLogger.fieldOut != expectedOutFields {
t.Fatalf("Expected %q, got %q", expectedOutFields, withFieldsStandardLogger.fieldOut)
}
}

View File

@ -4,13 +4,10 @@ import (
"fmt"
"io"
"os"
"sort"
"sync"
)
type StandardLogger struct {
// fieldOut is used to do log.WithFields correctly
fieldOut string
OutWriter io.Writer
ErrWriter io.Writer
mu *sync.Mutex
@ -20,8 +17,7 @@ func (t StandardLogger) log(args ...interface{}) {
defer t.mu.Unlock()
t.mu.Lock()
fmt.Fprint(t.OutWriter, args...)
fmt.Fprint(t.OutWriter, t.fieldOut, "\n")
t.fieldOut = ""
fmt.Fprint(t.OutWriter, "\n")
}
func (t StandardLogger) logf(fmtString string, args ...interface{}) {
@ -29,23 +25,20 @@ func (t StandardLogger) logf(fmtString string, args ...interface{}) {
t.mu.Lock()
fmt.Fprintf(t.OutWriter, fmtString, args...)
fmt.Fprint(t.OutWriter, "\n")
t.fieldOut = ""
}
func (t StandardLogger) err(args ...interface{}) {
defer t.mu.Unlock()
t.mu.Lock()
fmt.Fprint(t.ErrWriter, args...)
fmt.Fprint(t.ErrWriter, t.fieldOut, "\n")
t.fieldOut = ""
fmt.Fprint(t.ErrWriter, "\n")
}
func (t StandardLogger) errf(fmtString string, args ...interface{}) {
defer t.mu.Unlock()
t.mu.Lock()
fmt.Fprintf(t.ErrWriter, fmtString, args...)
fmt.Fprint(t.ErrWriter, t.fieldOut, "\n")
t.fieldOut = ""
fmt.Fprint(t.ErrWriter, "\n")
}
func (t StandardLogger) Debug(args ...interface{}) {
@ -103,34 +96,3 @@ func (t StandardLogger) Warnf(fmtString string, args ...interface{}) {
fmt.Print("WARNING >>> ")
t.logf(fmtString, args...)
}
func (t StandardLogger) WithFields(fields Fields) Logger {
// When the user calls WithFields, we make a string which gets appended
// to the output of the final [Info|Warn|Error] call for the
// descriptive fields. Because WithFields returns the proper Logger
// (with the fieldOut string set correctly), the logrus syntax will
// still work.
kvpairs := []string{}
// Why the string slice song and dance? Because Go's map iteration
// order is random, we will get inconsistent results if we don't sort
// the fields (or their resulting string K/V pairs, like we have here).
// Otherwise, we couldn't test this reliably.
for k, v := range fields {
kvpairs = append(kvpairs, fmt.Sprintf("%s=%v", k, v))
}
sort.Strings(kvpairs)
// TODO:
// 1. Is this thread-safe?
// 2. Add more tabs?
t.fieldOut = "\t\t"
for _, s := range kvpairs {
// TODO: Is %v the correct format string here?
t.fieldOut = fmt.Sprintf("%s %s", t.fieldOut, s)
}
return t
}