Skip to content

Commit

Permalink
fix: Error in logging system when appending strings
Browse files Browse the repository at this point in the history
  • Loading branch information
lordofthejars committed Jul 13, 2018
1 parent 78830bc commit 29e2416
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 72 deletions.
20 changes: 1 addition & 19 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 15 additions & 15 deletions core/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/lordofthejars/diferencia/metrics"
"github.com/prometheus/client_golang/prometheus"

"github.com/lordofthejars/diferencia/log"
"github.com/sirupsen/logrus"
)

// Difference algorithm
Expand Down Expand Up @@ -142,28 +142,28 @@ func (e *DiferenciaError) Error() string {
func Diferencia(r *http.Request) (bool, error) {

if !Config.AllowUnsafeOperations && !isSafeOperation(r.Method) {
log.Debug("Unsafe operations are not allowed and %s method has been received", r.Method)
logrus.Debugf("Unsafe operations are not allowed and %s method has been received", r.Method)
return false, &DiferenciaError{http.StatusMethodNotAllowed, fmt.Sprintf("Unsafe operations are not allowed and %s method has been received", r.Method)}
}

log.Debug("URL %s is going to be processed", r.URL.String())
logrus.Debugf("URL %s is going to be processed", r.URL.String())

// TODO it can be parallelized
// Get request from primary
primaryFullURL := CreateUrl(*r.URL, Config.Primary)
log.Debug("Forwarding call to %s", primaryFullURL)
logrus.Debugf("Forwarding call to %s", primaryFullURL)
primaryBodyContent, primaryStatus, primaryHeader, err := getContent(r, primaryFullURL)
if err != nil {
log.Error("Error while connecting to Primary site (%s) with %s", primaryFullURL, err.Error())
logrus.Errorf("Error while connecting to Primary site (%s) with %s", primaryFullURL, err.Error())
return false, &DiferenciaError{http.StatusServiceUnavailable, fmt.Sprintf("Error while connecting to Primary site (%s) with %s", primaryFullURL, err.Error())}
}

// Get candidate
candidateFullURL := CreateUrl(*r.URL, Config.Candidate)
log.Debug("Forwarding call to %s", candidateFullURL)
logrus.Debugf("Forwarding call to %s", candidateFullURL)
candidateBodyContent, candidateStatus, candidateHeader, err := getContent(r, candidateFullURL)
if err != nil {
log.Error("Error while connecting to Candidate site (%s) with %s", candidateFullURL, err.Error())
logrus.Errorf("Error while connecting to Candidate site (%s) with %s", candidateFullURL, err.Error())
return false, &DiferenciaError{http.StatusServiceUnavailable, fmt.Sprintf("Error while connecting to Candidate site (%s) with %s", candidateFullURL, err.Error())}
}

Expand All @@ -176,10 +176,10 @@ func Diferencia(r *http.Request) (bool, error) {
if Config.NoiseDetection {
// Get secondary to do the noise cancellation
secondaryFullURL := CreateUrl(*r.URL, Config.Secondary)
log.Debug("Forwarding call to %s", secondaryFullURL)
logrus.Debugf("Forwarding call to %s", secondaryFullURL)
secondaryBodyContent, secondaryStatus, _, err := getContent(r, secondaryFullURL)
if err != nil {
log.Error("Error while connecting to Secondary site (%s) with error %s", candidateFullURL, err.Error())
logrus.Errorf("Error while connecting to Secondary site (%s) with error %s", candidateFullURL, err.Error())
return false, &DiferenciaError{http.StatusServiceUnavailable, fmt.Sprintf("Error while connecting to Secondary site (%s) with error %s", candidateFullURL, err.Error())}
}

Expand All @@ -191,14 +191,14 @@ func Diferencia(r *http.Request) (bool, error) {
noiseOperation.Initialize(manualNoise)
err := noiseOperation.Detect(primaryBodyContent, secondaryBodyContent)
if err != nil {
log.Error("Error detecting noise between %s and %s. (%s)", primaryFullURL, secondaryFullURL, err.Error())
logrus.Error("Error detecting noise between %s and %s. (%s)", primaryFullURL, secondaryFullURL, err.Error())
return false, &DiferenciaError{http.StatusBadRequest, fmt.Sprintf("Error detecting noise between %s and %s. (%s)", primaryFullURL, secondaryFullURL, err.Error())}
}
primaryWithoutNoise, candidateWithoutNoise, err := noiseOperation.Remove(primaryBodyContent, candidateBodyContent)

result = compareResult(candidateWithoutNoise, primaryWithoutNoise, candidateStatus, primaryStatus, candidateHeader, primaryHeader)
} else {
log.Error("Status code between %s(%d) and %s(%d) are different", primaryFullURL, primaryStatus, secondaryFullURL, secondaryStatus)
logrus.Errorf("Status code between %s(%d) and %s(%d) are different", primaryFullURL, primaryStatus, secondaryFullURL, secondaryStatus)
return false, &DiferenciaError{http.StatusBadRequest, fmt.Sprintf("Status code between %s(%d) and %s(%d) are different", primaryFullURL, primaryStatus, secondaryFullURL, secondaryStatus)}
}
} else {
Expand All @@ -220,7 +220,7 @@ func Diferencia(r *http.Request) (bool, error) {
exporter.ExportToFile(Config.StoreResults, interactions)
}

log.Debug("Result of comparing %s and %s is %t", primaryFullURL, candidateFullURL, result)
logrus.Debugf("Result of comparing %s and %s is %t", primaryFullURL, candidateFullURL, result)

return result, nil

Expand All @@ -240,7 +240,7 @@ func manualNoiseDetection() []string {
lines, err := readLines(Config.IgnoreValuesFile)

if err != nil {
log.Error("Error reading %s that defines ignoring values. %s. Execution will continue ignoring this file.", Config.IgnoreValuesFile, err)
logrus.Errorf("Error reading %s that defines ignoring values. %s. Execution will continue ignoring this file.", Config.IgnoreValuesFile, err)
return pointers
}

Expand Down Expand Up @@ -338,15 +338,15 @@ func StartProxy(configuration *DiferenciaConfiguration) {
proxyMux := http.NewServeMux()
// Matches everything
proxyMux.HandleFunc("/", diferenciaHandler)
log.Error("Error starting proxy: %s", http.ListenAndServe(":"+strconv.Itoa(Config.Port), proxyMux))
logrus.Errorf("Error starting proxy: %s", http.ListenAndServe(":"+strconv.Itoa(Config.Port), proxyMux))
}()

go func() {
if Config.Prometheus {
//Initialize Prometheus endpoint
prometheusMux := http.NewServeMux()
prometheusMux.Handle("/metrics", prometheus.Handler())
log.Error("Error starting prometheus endpoint: %s", http.ListenAndServe(":"+strconv.Itoa(Config.PrometheusPort), prometheusMux))
logrus.Errorf("Error starting prometheus endpoint: %s", http.ListenAndServe(":"+strconv.Itoa(Config.PrometheusPort), prometheusMux))
}
}()

Expand Down
10 changes: 0 additions & 10 deletions log/colors.go

This file was deleted.

29 changes: 7 additions & 22 deletions log/logger.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,31 @@
package log

import (
log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
)

// Initialize log level
func Initialize(level string) {

switch level {
case "debug":
log.SetLevel(log.DebugLevel)
logrus.SetLevel(logrus.DebugLevel)
break
case "info":
log.SetLevel(log.InfoLevel)
logrus.SetLevel(logrus.InfoLevel)
break
case "error":
log.SetLevel(log.ErrorLevel)
logrus.SetLevel(logrus.ErrorLevel)
break
case "warn":
log.SetLevel(log.WarnLevel)
logrus.SetLevel(logrus.WarnLevel)
break
case "fatal":
log.SetLevel(log.FatalLevel)
logrus.SetLevel(logrus.FatalLevel)
break
case "panic":
log.SetLevel(log.PanicLevel)
logrus.SetLevel(logrus.PanicLevel)
break
}

}

// Info level
func Info(text string, arg ...interface{}) {
log.Infof(text, arg)
}

// Error level
func Error(text string, arg ...interface{}) {
log.Errorf(text, arg)
}

// Debug level
func Debug(text string, arg ...interface{}) {
log.Debugf(text, arg)
}
12 changes: 6 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/lordofthejars/diferencia/core"
"github.com/lordofthejars/diferencia/log"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -63,23 +64,23 @@ func main() {
differenceMode, err := core.NewDifference(difference)

if err != nil {
log.Error("Error while setting difference mode. %s", err.Error())
logrus.Errorf("Error while setting difference mode. %s", err.Error())
os.Exit(1)
}
config.DifferenceMode = differenceMode

if !areHttpsClientAttributesCorrect(caCert, clientCert, clientKey) {
log.Error("Https Client options should either not provided or all of them provided but not only some. caCert: %s, clientCert: %s, clientkey: %s.", caCert, clientCert, clientKey)
logrus.Errorf("Https Client options should either not provided or all of them provided but not only some. caCert: %s, clientCert: %s, clientkey: %s.", caCert, clientCert, clientKey)
os.Exit(1)
}

if noiseDetection && len(secondaryURL) == 0 {
log.Error("If Noise Detection is enabled, you need to provide a secondary URL as well")
logrus.Errorf("If Noise Detection is enabled, you need to provide a secondary URL as well")
os.Exit(1)
}

if !noiseDetection && (config.IsIgnoreValuesFileSet() || config.IsIgnoreValuesSet()) {
log.Info("ignoreValues or ignoreValuesFile attributes are set but noise detection is disabled, so they are going to be ignored.")
logrus.Infof("ignoreValues or ignoreValuesFile attributes are set but noise detection is disabled, so they are going to be ignored.")
}

if len(config.ServiceName) == 0 {
Expand All @@ -88,7 +89,6 @@ func main() {
}

log.Initialize(logLevel)

core.StartProxy(&config)
},
}
Expand Down Expand Up @@ -125,7 +125,7 @@ func main() {
rootCmd.AddCommand(cmdStart)

if err := rootCmd.Execute(); err != nil {
log.Error(err.Error())
logrus.Errorf(err.Error())
os.Exit(1)
}

Expand Down

0 comments on commit 29e2416

Please sign in to comment.