resty icon indicating copy to clipboard operation
resty copied to clipboard

Add support for go-logr/logr on resty

Open thapabishwa-plerionaut opened this issue 2 years ago • 1 comments

Currently, the go-resty/resty client does not support go-logr/logr, a popular logging library for Go applications. This issue is created to propose and discuss the addition of go-logr/logr support to the go-resty/resty client.

Motivation: go-logr/logr provides a flexible and extensible logging framework, allowing applications to use different log sinks and log levels. By adding support for go-logr/logr to the go-resty/resty client, users can have more control and flexibility over logging in their applications.

There are (non-exhaustive list of)implementations for the following logging libraries:

  • a function (can bridge to non-structured libraries): funcr
  • a testing.T (for use in Go tests, with JSON-like output): testr
  • github.com/google/glog: glogr
  • k8s.io/klog (for Kubernetes): klogr
  • a testing.T (with klog-like text output): ktesting
  • go.uber.org/zap: zapr
  • log (the Go standard library logger): stdr
  • github.com/sirupsen/logrus: logrusr
  • github.com/wojas/genericr: genericr (makes it easy to implement your own backend)
  • logfmt (Heroku style logging): logfmtr
  • github.com/rs/zerolog: zerologr
  • github.com/go-kit/log: gokitlogr (also compatible with github.com/go-kit/kit/log since v0.12.0)
  • bytes.Buffer (writing to a buffer): bufrlogr (useful for ensuring values were logged, like during testing)

Proposed Changes: Example:

package main

import (
    "github.com/go-resty/resty/v2"
    "github.com/go-logr/logr"
)

func main() {
    logger := logr.New() // Initialize a logr logger

    restyClient := resty.New()
    restyClient.SetLogger(logger) // Set the logger for resty

    // Use restyClient for HTTP requests, and it will log using logr
}

Expected Result: With this feature, users can integrate go-resty/resty seamlessly into their applications that use various implementations ofgo-logr/logr for logging. This provides consistency in the logging framework and allows for better customization and control of log messages.

Additional Context: Please note that this is just a proposal, and further discussion and development are needed to implement this feature. I encourage the community to share their thoughts, suggestions, or any potential challenges related to this feature request.

thapabishwa-plerionaut avatar Oct 26 '23 03:10 thapabishwa-plerionaut

+1 for this feature, especially for the kubernetes ecosystem

halfcrazy avatar Nov 07 '23 10:11 halfcrazy

@thapabishwa-plerionaut @halfcrazy Thanks for reaching out. I'm sorry for the delayed response.

Resty provides a Logger interface for user choice of overriding. https://github.com/go-resty/resty/blob/97187c431cf6db12100c49b68dab25bca79098c1/util.go#L31-L35

So you could assign your existing logger instance to it.

client := resty.New().SetLogger(...)

jeevatkm avatar Mar 02 '24 20:03 jeevatkm

@jeevatkm

The interface you mentioned doesn't have the ability to interoperate with loggers(like gologr, klog, slog etc) that are used in kubernetes ecosystem.

package main

import (
	"net/http"

	"github.com/go-resty/resty/v2"
	"sigs.k8s.io/controller-runtime/pkg/log"
)

func main() {

	logger := log.Log.WithName("controller").WithName("mylogger")
        /// cannot use logger (variable of type logr.Logger) as resty.Logger value in argument to resty.New().SetLogger: logr.Logger does not implement resty.Logger (missing method Debugf)
	client := resty.New().SetLogger(logger)
	cookies := []*http.Cookie{
		&http.Cookie{
			Name:  "go-resty-1",
			Value: "This is cookie 1 value",
		},
		&http.Cookie{
			Name:  "go-resty-2",
			Value: "This is cookie 2 value",
		},
	}

	client.SetCookies(cookies)

}

thapabishwa avatar Mar 13 '24 03:03 thapabishwa

logr.Logger does not implement resty.Logger (missing method Debugf)

@thapabishwa Based on the error message, it seems logr does not have a Debugf implementation. I suggest creating a tiny wrapper for the log library you would like to use.

For example, refer to - https://github.com/go-resty/resty/blob/94e70d358df997ea7434b0856520dcfc14d3cf74/util.go#L37-L66

jeevatkm avatar Mar 17 '24 20:03 jeevatkm