diff --git a/Gopkg.lock b/Gopkg.lock index 5e572c7..ee989e5 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -13,12 +13,6 @@ revision = "afac545df32f2287a079e2dfb7ba2745a643747e" version = "v3.0.0" -[[projects]] - name = "github.com/fatih/color" - packages = ["."] - revision = "5b77d2a35fb0ede96d138fc9a99f5c9b6aef11b4" - version = "v1.7.0" - [[projects]] name = "github.com/golang/protobuf" packages = ["proto"] @@ -43,18 +37,6 @@ packages = ["."] revision = "81af80346b1a01caae0cbc27fd3c1ba5b11e189f" -[[projects]] - name = "github.com/mattn/go-colorable" - packages = ["."] - revision = "167de6bfdfba052fa6b2d3664c8f5272e23c9072" - version = "v0.0.9" - -[[projects]] - name = "github.com/mattn/go-isatty" - packages = ["."] - revision = "0360b2af4f38e8d38c7fce2a9f4e702702d73a39" - version = "v0.0.3" - [[projects]] name = "github.com/matttproud/golang_protobuf_extensions" packages = ["pbutil"] @@ -212,6 +194,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "da715094a48a3c17e2a399112e56a20052d2c1a48252d48d5297926d51daa884" + inputs-digest = "f041746a9ea9fa5826ec62c28dc9f0b5dbb14942bcc32fd7424ef52ec34cc5bc" solver-name = "gps-cdcl" solver-version = 1 diff --git a/core/proxy.go b/core/proxy.go index d2a5438..abcbfa5 100644 --- a/core/proxy.go +++ b/core/proxy.go @@ -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 @@ -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())} } @@ -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())} } @@ -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 { @@ -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 @@ -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 } @@ -338,7 +338,7 @@ 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() { @@ -346,7 +346,7 @@ func StartProxy(configuration *DiferenciaConfiguration) { //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)) } }() diff --git a/log/colors.go b/log/colors.go deleted file mode 100644 index 95683f1..0000000 --- a/log/colors.go +++ /dev/null @@ -1,10 +0,0 @@ -package log - -import ( - "github.com/fatih/color" -) - -var colorInfo = color.New(color.FgGreen).SprintFunc() -var colorWarn = color.New(color.FgYellow).SprintFunc() -var colorErr = color.New(color.FgRed).SprintFunc() -var colorDebug = color.New(color.FgBlue).SprintFunc() diff --git a/log/logger.go b/log/logger.go index 7827602..fa8f2b8 100644 --- a/log/logger.go +++ b/log/logger.go @@ -1,7 +1,7 @@ package log import ( - log "github.com/sirupsen/logrus" + "github.com/sirupsen/logrus" ) // Initialize log level @@ -9,38 +9,23 @@ 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) -} diff --git a/main.go b/main.go index 3722388..211428f 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "github.com/lordofthejars/diferencia/core" "github.com/lordofthejars/diferencia/log" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -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 { @@ -88,7 +89,6 @@ func main() { } log.Initialize(logLevel) - core.StartProxy(&config) }, } @@ -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) }