Skip to content
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

Closed
wants to merge 9 commits into from

Conversation

cihantas
Copy link

@cihantas cihantas commented Oct 14, 2018

The user can set a field Debug bool to true upon creation of a helix.Client. An optional custom logger of type log.Logger can also be set. If none is set, a default logger will be used.

Closes #2

@coveralls
Copy link

Pull Request Test Coverage Report for Build 55

  • 10 of 14 (71.43%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.5%) to 91.032%

Changes Missing Coverage Covered Lines Changed/Added Lines %
helix.go 10 14 71.43%
Totals Coverage Status
Change from base Build 53: -0.5%
Covered Lines: 538
Relevant Lines: 591

💛 - Coveralls

@coveralls
Copy link

coveralls commented Oct 14, 2018

Pull Request Test Coverage Report for Build 58

  • 12 of 26 (46.15%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-2.0%) to 89.552%

Changes Missing Coverage Covered Lines Changed/Added Lines %
helix.go 12 26 46.15%
Totals Coverage Status
Change from base Build 53: -2.0%
Covered Lines: 540
Relevant Lines: 603

💛 - Coveralls

Copy link
Owner

@nicklaw5 nicklaw5 left a 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
Copy link
Owner

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
Copy link
Owner

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

Copy link
Owner

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,

Copy link
Owner

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 {
Copy link
Owner

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 {
Copy link
Owner

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

@cihantas
Copy link
Author

This looks like a worthwhile contribution. Thanks for taking the time.

A big thank you back to you for taking the time to write and most importantly publish this library.

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.

Sure.

If possible, can you we also add a test?

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.

Oh! One last thing: we need to add some documentation to the docs page to show an example of how to use the logger.

Sure, docs are super important. 👍

@nicklaw5 nicklaw5 added the enhancement New feature or request label Oct 14, 2018
Copy link
Owner

@nicklaw5 nicklaw5 left a 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{}
Copy link
Owner

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.

@@ -0,0 +1,28 @@
# Logging Documentation
Copy link
Owner

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.

## 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.
Copy link
Owner

@nicklaw5 nicklaw5 Oct 14, 2018

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.

// 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)
Copy link
Owner

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)
Copy link
Owner

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.

Copy link
Owner

@nicklaw5 nicklaw5 left a 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)
Copy link
Owner

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)
Copy link
Owner

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,
Copy link
Owner

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,
Copy link
Owner

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

@nicklaw5
Copy link
Owner

@cihantas How you going with this? Did you need any assistance? I'd Iike to get this merged if possible.

@nicklaw5
Copy link
Owner

Closing. Please re-open if you would like these change reconsidered.

@nicklaw5 nicklaw5 closed this Oct 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add an option for logging
3 participants