-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added optional request and response logging #6
Conversation
…ly provided logger.
Pull Request Test Coverage Report for Build 55
💛 - Coveralls |
Pull Request Test Coverage Report for Build 58
💛 - Coveralls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a worthwhile contribution. Thanks for taking the time.
Can I suggest we also add both httputil.DumpRequest and httputil.DumpResponse inside the doRequest
method? Those functions provide a more detailed account of the request and response.
If possible, can you we also add a test?
Oh! One last thing: we need to add some documentation to the docs page to show an example of how to use the logger.
helix.go
Outdated
@@ -39,6 +40,10 @@ type Client struct { | |||
|
|||
baseURL string | |||
lastResponse *Response | |||
|
|||
// Logging | |||
Debug bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This field shouldn't be exported (ie. it should have a lowercase d
).
This is actually something I want to fix in the future. There's no need to have both the Client and Options structs share the same fields. I'm probably going to add a opts field to the Client struct instead of the individual fields. Leave that to me, however, that's not a necessary for this PR.
helix.go
Outdated
|
||
// Logging | ||
Debug bool | ||
Logger *log.Logger |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above: this field shouldn't be exported.
helix.go
Outdated
@@ -52,6 +57,9 @@ type Options struct { | |||
Scopes []string | |||
HTTPClient HTTPClient | |||
RateLimitFunc RateLimitFunc | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Drop this empty line.
helix.go
Outdated
@@ -108,9 +116,18 @@ func NewClient(options *Options) (*Client, error) { | |||
c := &Client{ | |||
clientID: options.ClientID, | |||
httpClient: http.DefaultClient, | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Drop this empty line.
helix.go
Outdated
@@ -151,11 +168,19 @@ func (c *Client) sendRequest(method, path string, respData, reqData interface{}) | |||
return nil, err | |||
} | |||
|
|||
if c.Debug == true { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if c.debug {
is sufficient here. (Note the lowercase d
.)
helix.go
Outdated
err = c.doRequest(req, resp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if c.Debug == true { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above: if c.debug {
is sufficient here. (Note the lowercase d
.)
A big thank you back to you for taking the time to write and most importantly publish this library.
Sure.
I'm not very experienced when it comes to testing logging. I did some research and read that most people avoid it because it seems overly excessive. Your guidance on that topic would be welcome.
Sure, docs are super important. 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did some research and read that most people avoid it because it seems overly excessive.
That seems reasonable. I'm happy to forgo tests for this feature.
There is also a list of all Options that can be found here. Do you mind adding the two new fields you've created to that list?
helix.go
Outdated
@@ -124,7 +124,7 @@ func NewClient(options *Options) (*Client, error) { | |||
|
|||
// Use the default logger, if none was set by the user. | |||
if options.Logger == nil { | |||
c.Logger = &log.Logger{} | |||
c.logger = &log.Logger{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also need to set the default output here. If we don't, calling c.logger.Printf
will throw an error.
By default I think we should just use stdout
. For example:
c.logger = log.New(os.Stdout, "Helix: ", log.LstdFlags)
See the docs for log.New
.
docs/logging_docs.md
Outdated
@@ -0,0 +1,28 @@ | |||
# Logging Documentation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This general documentation doesn't require it's own file. I would prefer we include this at the bottom of the docs README.
docs/logging_docs.md
Outdated
## Adding a custom logger | ||
|
||
You can add a custom logger to the client that will be used to print outgoing requests and incoming responses. Debug | ||
mode must be set to `true` to enable logging. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also add:
Should you want to enable debug mode but not want to use a custom logger,
stdout
will be used as the default log output.
docs/logging_docs.md
Outdated
// Create a logger, which outputs to file. | ||
f, err := os.OpenFile("log.txt", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666) | ||
if err != nil { | ||
log.Fatalf("error opening file: %v", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's replace this line with // handle error
to be consistent with rest of the documentation.
docs/README.md
Outdated
@@ -10,6 +10,7 @@ Follow the links below to their respective API usage examples: | |||
- [Clips](clips_docs.md) | |||
- [Entitlement Grants](entitlement_grants_docs.md) | |||
- [Games](games_docs.md) | |||
- [Logging](logging_docs.md) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that we're moving the logging docs to this same file, this line can be dropped.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a few minor comments regarding the example. Once those are fixed I think we'll be good to get this merged.
// Create a logger, which outputs to file. | ||
f, err := os.OpenFile("log.txt", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666) | ||
if err != nil { | ||
log.Fatalf("error opening file: %v", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line should be // handle error
debug: true, | ||
}) | ||
if err != nil { | ||
log.Fatalf("error opening file: %v", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line should be // handle error
fileLogger.SetOutput(f) | ||
|
||
client, err := helix.NewClient(&helix.Options{ | ||
logger: fileLogger, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logger
needs to be capitalised (ie. Logger
).
|
||
client, err := helix.NewClient(&helix.Options{ | ||
logger: fileLogger, | ||
debug: true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
debug
needs to be capitalised (ie. Debug
).
@cihantas How you going with this? Did you need any assistance? I'd Iike to get this merged if possible. |
Closing. Please re-open if you would like these change reconsidered. |
The user can set a field
Debug bool
to true upon creation of ahelix.Client
. An optional custom logger of typelog.Logger
can also be set. If none is set, a default logger will be used.Closes #2