Skip to content

Commit

Permalink
Separated functions for formatted and unformatted log
Browse files Browse the repository at this point in the history
Also fixed some function comments

Fixed log usages


Fix govet


Fixed log functions usages


Should use unformatted log method
  • Loading branch information
arknable committed Jul 1, 2021
1 parent 16bd9d1 commit 7814b8c
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 66 deletions.
12 changes: 6 additions & 6 deletions aws/aws_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func (p *AWS) CreateInstance(ctx *lepton.Context) error {
}

if vpc == nil {
ctx.Logger().Debug("creating vpc with name %s", cloudConfig.VPC)
ctx.Logger().Debugf("creating vpc with name %s", cloudConfig.VPC)
vpc, err = p.CreateVPC(ctx, svc)
if err != nil {
return err
Expand All @@ -218,13 +218,13 @@ func (p *AWS) CreateInstance(ctx *lepton.Context) error {
var sg *ec2.SecurityGroup

if cloudConfig.SecurityGroup != "" && cloudConfig.VPC != "" {
ctx.Logger().Debug("getting security group with name %s", cloudConfig.SecurityGroup)
ctx.Logger().Debugf("getting security group with name %s", cloudConfig.SecurityGroup)
sg, err = p.GetSecurityGroup(ctx, svc, vpc)
if err != nil {
return err
}
} else {
ctx.Logger().Debug("creating new security group in vpc %s", *vpc.VpcId)
ctx.Logger().Debugf("creating new security group in vpc %s", *vpc.VpcId)
sg, err = p.CreateSG(ctx, svc, imgName, *vpc.VpcId)
if err != nil {
return err
Expand Down Expand Up @@ -273,10 +273,10 @@ func (p *AWS) CreateInstance(ctx *lepton.Context) error {
}

// Specify the details of the instance that you want to create.
ctx.Logger().Debug("running instance with input %v", instanceInput)
ctx.Logger().Debugf("running instance with input %v", instanceInput)
_, err = svc.RunInstances(instanceInput)
if err != nil {
log.Errorf("Could not create instance", err)
log.Errorf("Could not create instance %v", err)
return err
}

Expand All @@ -298,7 +298,7 @@ func (p *AWS) CreateInstance(ctx *lepton.Context) error {
}

if len(instance.PublicIps) != 0 {
ctx.Logger().Debug("creating dns record %s with ip %s", cloudConfig.DomainName, instance.PublicIps[0])
ctx.Logger().Debugf("creating dns record %s with ip %s", cloudConfig.DomainName, instance.PublicIps[0])
err := lepton.CreateDNSRecord(ctx.Config(), instance.PublicIps[0], p)
if err != nil {
return err
Expand Down
10 changes: 5 additions & 5 deletions aws/aws_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ func (p *AWS) GetVPC(ctx *lepton.Context, svc *ec2.EC2) (*ec2.Vpc, error) {
vpcIDRegexp, _ := regexp.Compile("^vpc-.+")

if vpcIDRegexp.Match([]byte(vpcName)) {
ctx.Logger().Debug("no vpcs with name %s found", vpcName)
ctx.Logger().Debug("getting vpcs filtered by id %s", vpcName)
ctx.Logger().Debugf("no vpcs with name %s found", vpcName)
ctx.Logger().Debugf("getting vpcs filtered by id %s", vpcName)
input = &ec2.DescribeVpcsInput{
VpcIds: aws.StringSlice([]string{vpcName}),
}
Expand All @@ -178,7 +178,7 @@ func (p *AWS) GetVPC(ctx *lepton.Context, svc *ec2.EC2) (*ec2.Vpc, error) {
return nil, fmt.Errorf("unable to describe VPCs, %v", err)
}
} else {
ctx.Logger().Debug("getting vpcs filtered by name %s", vpcName)
ctx.Logger().Debugf("getting vpcs filtered by name %s", vpcName)
var filters []*ec2.Filter

filters = append(filters, &ec2.Filter{Name: aws.String("tag:Name"), Values: aws.StringSlice([]string{vpcName})})
Expand All @@ -192,7 +192,7 @@ func (p *AWS) GetVPC(ctx *lepton.Context, svc *ec2.EC2) (*ec2.Vpc, error) {
}
}

ctx.Logger().Debug("found %d vpcs that match the criteria %s", len(result.Vpcs), vpcName)
ctx.Logger().Debugf("found %d vpcs that match the criteria %s", len(result.Vpcs), vpcName)

if len(result.Vpcs) != 0 {
return result.Vpcs[0], nil
Expand All @@ -218,7 +218,7 @@ func (p *AWS) GetVPC(ctx *lepton.Context, svc *ec2.EC2) (*ec2.Vpc, error) {
if vpc == nil && len(result.Vpcs) != 0 {
ctx.Logger().Debug("no default vpc found")
vpc = result.Vpcs[0]
ctx.Logger().Debug("picking vpc %+v", vpc)
ctx.Logger().Debugf("picking vpc %+v", vpc)
}
}

Expand Down
16 changes: 8 additions & 8 deletions azure/azure_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (a *Azure) CreateInstance(ctx *lepton.Context) error {
location := a.getLocation(ctx.Config())

vmName := ctx.Config().RunConfig.InstanceName
ctx.Logger().Log("spinning up:\t%s", vmName)
ctx.Logger().Logf("spinning up:\t%s", vmName)

// create virtual network
var vnet *network.VirtualNetwork
Expand All @@ -72,7 +72,7 @@ func (a *Azure) CreateInstance(ctx *lepton.Context) error {
return fmt.Errorf("error getting virtual network with id %s", configVPC)
}
} else {
ctx.Logger().Info("creating virtual network with id %s", vmName)
ctx.Logger().Infof("creating virtual network with id %s", vmName)
vnet, err = a.CreateVirtualNetwork(context.TODO(), location, vmName)
if err != nil {
ctx.Logger().Error(err)
Expand All @@ -90,7 +90,7 @@ func (a *Azure) CreateInstance(ctx *lepton.Context) error {
return errors.New("error getting security group")
}
} else {
ctx.Logger().Info("creating network security group with id %s", vmName)
ctx.Logger().Infof("creating network security group with id %s", vmName)
nsg, err = a.CreateNetworkSecurityGroup(context.TODO(), location, vmName, c)
if err != nil {
ctx.Logger().Error(err)
Expand All @@ -108,7 +108,7 @@ func (a *Azure) CreateInstance(ctx *lepton.Context) error {
return errors.New("error getting subnet")
}
} else {
ctx.Logger().Info("creating subnet with id %s", vmName)
ctx.Logger().Infof("creating subnet with id %s", vmName)
subnet, err = a.CreateSubnetWithNetworkSecurityGroup(context.TODO(), *vnet.Name, vmName, "10.0.0.0/24", *nsg.Name)
if err != nil {
ctx.Logger().Error(err)
Expand All @@ -117,7 +117,7 @@ func (a *Azure) CreateInstance(ctx *lepton.Context) error {
}

// create ip
ctx.Logger().Info("creating public ip with id %s", vmName)
ctx.Logger().Infof("creating public ip with id %s", vmName)
ip, err := a.CreatePublicIP(context.TODO(), location, vmName)
if err != nil {
ctx.Logger().Error(err)
Expand All @@ -126,7 +126,7 @@ func (a *Azure) CreateInstance(ctx *lepton.Context) error {

// create nic
// pass vnet, subnet, ip, nicname
ctx.Logger().Info("creating network interface controller with id %s", vmName)
ctx.Logger().Infof("creating network interface controller with id %s", vmName)
nic, err := a.CreateNIC(context.TODO(), location, *vnet.Name, *subnet.Name, *nsg.Name, *ip.Name, vmName)
if err != nil {
ctx.Logger().Error(err)
Expand Down Expand Up @@ -353,13 +353,13 @@ func (a *Azure) DeleteInstance(ctx *lepton.Context, instancename string) error {

vmClient := a.getVMClient()

ctx.Logger().Info("Getting vm with ID %s...", instancename)
ctx.Logger().Infof("Getting vm with ID %s...", instancename)
vm, err := a.GetVM(context.TODO(), instancename)
if err != nil {
return err
}

ctx.Logger().Info("Deleting vm with ID %s...", instancename)
ctx.Logger().Infof("Deleting vm with ID %s...", instancename)
future, err := vmClient.Delete(context.TODO(), a.groupName, instancename)
if err != nil {
ctx.Logger().Error(err)
Expand Down
10 changes: 5 additions & 5 deletions azure/azure_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (a *Azure) DeleteNIC(ctx *lepton.Context, nic *network.Interface) error {

nicClient := a.getNicClient()

logger.Info("Deleting %s...", *nic.ID)
logger.Infof("Deleting %s...", *nic.ID)
nicName := getAzureResourceNameFromID(*nic.ID)
nicDeleteTask, err := nicClient.Delete(context.TODO(), a.groupName, nicName)
if err != nil {
Expand All @@ -125,7 +125,7 @@ func (a *Azure) DeleteIP(ctx *lepton.Context, ipConfiguration *network.Interface

ipClient := a.getIPClient()

logger.Info("Deleting public IP %s...", ipID)
logger.Infof("Deleting public IP %s...", ipID)
deleteIPTask, err := ipClient.Delete(context.TODO(), a.groupName, ipID)
if err != nil {
logger.Error(err)
Expand Down Expand Up @@ -167,7 +167,7 @@ func (a *Azure) DeleteNetworkSecurityGroup(ctx *lepton.Context, securityGroupID
} else if securityGroup.Subnets != nil && len(*securityGroup.Subnets) != 0 {
return errors.New("existing subnetworks are using this security group")
} else {
logger.Info("Deleting %s...", *securityGroup.ID)
logger.Infof("Deleting %s...", *securityGroup.ID)
nsgTask, err := nsgClient.Delete(context.TODO(), a.groupName, *securityGroup.Name)
if err != nil {
logger.Error(err)
Expand Down Expand Up @@ -214,7 +214,7 @@ func (a *Azure) DeleteSubnetwork(ctx *lepton.Context, subnetID string) error {
}

if hasAzureOpsTags(virtualNetwork.Tags) && (subnet.IPConfigurations == nil || len(*subnet.IPConfigurations) == 0) {
logger.Info("Deleting %s...", *subnet.ID)
logger.Infof("Deleting %s...", *subnet.ID)
subnetDeleteTask, err := subnetsClient.Delete(context.TODO(), a.groupName, subnetName, subnetName)
if err != nil {
logger.Error(err)
Expand All @@ -227,7 +227,7 @@ func (a *Azure) DeleteSubnetwork(ctx *lepton.Context, subnetID string) error {
return errors.New("error waiting for subnet deletion")
}

logger.Info("Deleting virtualNetworks/%s", vnName)
logger.Infof("Deleting virtualNetworks/%s", vnName)
vnDeleteTask, err := vnetClient.Delete(context.TODO(), a.groupName, vnName)
if err != nil {
logger.Error(err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/cmd_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func deployCommandHandler(cmd *cobra.Command, args []string) {

for _, i := range instances {
if i.Image == c.CloudConfig.ImageName {
ctx.Logger().Debug("deleting instance %s", i.Name)
ctx.Logger().Debugf("deleting instance %s", i.Name)
err := p.DeleteInstance(ctx, i.Name)
if err != nil {
exitWithError(err.Error())
Expand Down
2 changes: 1 addition & 1 deletion cmd/cmd_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func updateCommandHandler(cmd *cobra.Command, args []string) {
log.Info("Checking for updates...")
err := api.DoUpdate(fmt.Sprintf(api.OpsReleaseURL, runtime.GOOS))
if err != nil {
log.Errorf("Failed to update.", err)
log.Errorf("Failed to update. %v", err)
} else {
log.Info("Updates ops to latest release.")
}
Expand Down
2 changes: 1 addition & 1 deletion digitalocean/digital_ocean_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (s *Spaces) CopyToBucket(config *types.Config, archPath string) error {

log.Info("Uploaded", "my-objectname", " of size: ", n, "Successfully.")

log.Info("Successfully uploaded %q to %q\n", config.CloudConfig.ImageName, bucket)
log.Infof("Successfully uploaded %q to %q\n", config.CloudConfig.ImageName, bucket)

policy := `{"Version": "2012-10-17","Statement": [{"Action": ["s3:GetObject"],"Effect": "Allow","Principal": {"AWS": ["*"]},"Resource": ["arn:aws:s3:::ops/` + key + `"],"Sid": ""}]}`

Expand Down
2 changes: 1 addition & 1 deletion gcp/gcp_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (p *GCloud) CreateInstance(ctx *lepton.Context) error {
cinstance := p.convertToCloudInstance(instance)

if len(cinstance.PublicIps) != 0 {
ctx.Logger().Info("Assigning IP %s to %s", cinstance.PublicIps[0], c.CloudConfig.DomainName)
ctx.Logger().Infof("Assigning IP %s to %s", cinstance.PublicIps[0], c.CloudConfig.DomainName)
err := lepton.CreateDNSRecord(ctx.Config(), cinstance.PublicIps[0], p)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion gcp/gcp_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (p *GCloud) findOrCreateVPC(ctx *lepton.Context, computeService *compute.Se
if err != nil && strings.Contains(err.Error(), "notFound") {
ctx.Logger().Warn(err.Error())

ctx.Logger().Info("Creating vpc with name %s", vpcName)
ctx.Logger().Infof("Creating vpc with name %s", vpcName)
network, err = p.CreateVPC(computeService, c.CloudConfig.ProjectID, vpcName)
if err != nil {
ctx.Logger().Error(err)
Expand Down
62 changes: 49 additions & 13 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package log
import (
"fmt"
"io"
"strings"
)

// Logger filters and prints messages to a destination
Expand Down Expand Up @@ -39,38 +40,73 @@ func (l *Logger) SetDebug(value bool) {
l.debug = value
}

// Log writes a message to the specified output
func (l *Logger) Log(message string, a ...interface{}) {
fmt.Fprintf(l.output, message+"\n", a...)
// Logf writes a formatted message to the specified output
func (l *Logger) Logf(format string, a ...interface{}) {
if !strings.HasSuffix(format, "\n") {
format = format + "\n"
}
fmt.Fprintf(l.output, format, a...)
}

// Log writes message to the specified output
func (l *Logger) Log(a ...interface{}) {
fmt.Fprintln(l.output, a...)
}

// Calls Log with foreground color set
func (l *Logger) logWithColor(color string, a ...interface{}) {
msg := fmt.Sprintln(a...)
l.Log(color + strings.TrimSuffix(msg, "\n") + ConsoleColors.Reset())
}

// Info checks info level is activated to write the message
func (l *Logger) Info(message string, a ...interface{}) {
func (l *Logger) Info(a ...interface{}) {
if l.info == true {
l.Log(ConsoleColors.Blue()+message+ConsoleColors.Reset(), a...)
l.logWithColor(ConsoleColors.Blue(), a...)
}
}

// Infof checks info level is activated to write the formatted message
func (l *Logger) Infof(format string, a ...interface{}) {
if l.info == true {
l.Logf(ConsoleColors.Blue()+format+ConsoleColors.Reset(), a...)
}
}

// Warn checks warn level is activated to write the message
func (l *Logger) Warn(message string, a ...interface{}) {
func (l *Logger) Warn(a ...interface{}) {
if l.warn == true {
l.Log(ConsoleColors.Yellow()+message+ConsoleColors.Reset(), a...)
l.logWithColor(ConsoleColors.Yellow(), a...)
}
}

// Errorf checks error level is activated to write the formatted message
func (l *Logger) Errorf(message string, a ...interface{}) {
l.Log(ConsoleColors.Red()+message+ConsoleColors.Reset(), a...)
// Warnf checks warn level is activated to write the formatted message
func (l *Logger) Warnf(format string, a ...interface{}) {
if l.warn == true {
l.Logf(ConsoleColors.Yellow()+format+ConsoleColors.Reset(), a...)
}
}

// Error checks error level is activated to write error object
func (l *Logger) Error(err error) {
l.Log(ConsoleColors.Red() + err.Error() + ConsoleColors.Reset())
l.logWithColor(ConsoleColors.Red(), err.Error())
}

// Errorf checks error level is activated to write the formatted message
func (l *Logger) Errorf(format string, a ...interface{}) {
l.Logf(ConsoleColors.Red()+format+ConsoleColors.Reset(), a...)
}

// Debug checks debug level is activated to write the message
func (l *Logger) Debug(message string, a ...interface{}) {
func (l *Logger) Debug(a ...interface{}) {
if l.debug == true {
l.logWithColor(ConsoleColors.Cyan(), a...)
}
}

// Debugf checks debug level is activated to write the message
func (l *Logger) Debugf(format string, a ...interface{}) {
if l.debug == true {
l.Log(ConsoleColors.Cyan()+message+ConsoleColors.Reset(), a...)
l.Logf(ConsoleColors.Cyan()+format+ConsoleColors.Reset(), a...)
}
}
Loading

0 comments on commit 7814b8c

Please sign in to comment.