go-resty / resty

Simple HTTP and REST client library for Go
MIT License
9.89k stars 696 forks source link

Add support for go-logr/logr on resty #741

Closed thapabishwa-plerionaut closed 6 months ago

thapabishwa-plerionaut commented 10 months ago

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:

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.

halfcrazy commented 10 months ago

+1 for this feature, especially for the kubernetes ecosystem

jeevatkm commented 6 months ago

@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(...)
thapabishwa commented 5 months ago

@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)

}
jeevatkm commented 5 months ago

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