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

V-style logging with glog and check errs #18

Merged
merged 4 commits into from
Nov 29, 2018
Merged

Conversation

cxuu
Copy link
Contributor

@cxuu cxuu commented Nov 14, 2018

This PR enables level logging so http requests and responses (often verbose) are only logged when the user asks with -v flag. More information about glog can be found https://godoc.org/github.com/golang/glog.

Originally, go-runscope uses native go log, which is hard to filter and interleaves with the application logs. There is hacky workaround such as log.SetOutput(ioutil.Discard) but that does not play well with clients also using go native log.

This PR also add the checks for errors by ReadAll() and json.Unmarshal() in client.go. The recent commits also fix the Makefile with shell keywords in front of shell commands, linter issues in bucket.go, and a typo in client_test.go.

@cxuu
Copy link
Contributor Author

cxuu commented Nov 14, 2018

@ewilde PTAL

@cxuu
Copy link
Contributor Author

cxuu commented Nov 15, 2018

It seems the CI is having trouble installing the linter. On my local, make build is passing with output reproduced below.

go get github.com/golang/lint/golint
go test  -v=1 --logtostderr
=== RUN   TestCreateBucket
--- PASS: TestCreateBucket (3.42s)
=== RUN   TestDeleteBuckets
--- PASS: TestDeleteBuckets (7.89s)
=== RUN   TestListBuckets
--- PASS: TestListBuckets (6.75s)
=== RUN   TestReadBucket
--- PASS: TestReadBucket (3.85s)
=== RUN   TestBucketReadFromResponse
--- PASS: TestBucketReadFromResponse (0.00s)
=== RUN   TestDeserializeResult
--- PASS: TestDeserializeResult (0.00s)
=== RUN   TestCreateSharedEnvironment
--- PASS: TestCreateSharedEnvironment (4.76s)
=== RUN   TestCreateTestEnvironment
--- PASS: TestCreateTestEnvironment (6.53s)
=== RUN   TestReadEnvironmentFromResponse
--- PASS: TestReadEnvironmentFromResponse (0.00s)
=== RUN   TestListSharedEnvironment
--- PASS: TestListSharedEnvironment (6.97s)
=== RUN   TestCreateSchedule
--- PASS: TestCreateSchedule (7.17s)
=== RUN   TestReadSchedule
--- PASS: TestReadSchedule (7.89s)
=== RUN   TestUpdateSchedule
--- PASS: TestUpdateSchedule (9.73s)
=== RUN   TestDeleteSchedule
--- PASS: TestDeleteSchedule (7.89s)
=== RUN   TestListSchedules
--- PASS: TestListSchedules (7.32s)
=== RUN   TestListIntegration
--- PASS: TestListIntegration (0.57s)
=== RUN   TestCreateTestStep
--- PASS: TestCreateTestStep (6.25s)
=== RUN   TestReadTestStep
--- PASS: TestReadTestStep (6.76s)
=== RUN   TestUpdateTestStep
--- PASS: TestUpdateTestStep (7.15s)
=== RUN   TestDeleteTestStep
--- PASS: TestDeleteTestStep (6.67s)
=== RUN   TestValidationRequestTypeMissingMethod
--- PASS: TestValidationRequestTypeMissingMethod (0.00s)
=== RUN   TestValidationRequestTypeGetIncludesBody
--- PASS: TestValidationRequestTypeGetIncludesBody (0.00s)
=== RUN   TestCreateTest
--- PASS: TestCreateTest (4.65s)
=== RUN   TestReadTest
--- PASS: TestReadTest (5.39s)
=== RUN   TestUpdateTest
--- PASS: TestUpdateTest (5.73s)
=== RUN   TestUpdateTestUsingPartiallyFilledOutObject
--- PASS: TestUpdateTestUsingPartiallyFilledOutObject (5.53s)
=== RUN   TestReadFromResponse
--- PASS: TestReadFromResponse (0.00s)
=== RUN   TestListsTests
--- PASS: TestListsTests (6.86s)
=== RUN   TestListsTestsMoreThan10
--- PASS: TestListsTestsMoreThan10 (28.64s)
PASS
2018/11/14 15:56:50 deleting bucket name: test key: m6iovwj1pk1q 
ok  	github.com/ewilde/go-runscope	167.856s
go vet github.com/ewilde/go-runscope github.com/ewilde/go-runscope/examples
golint github.com/ewilde/go-runscope github.com/ewilde/go-runscope/examples

@canthefason
Copy link

if switching to glog is going to be a thing, to discern the action items from the request/response bodies, it would be nice to level them separately.

glog.V(1).Infof("[DEBUG] creating bucket %s", bucket.Name)
glog.V(2).Infof("[DEBUG] 	request: POST %s %#v", "/buckets", data)

@ewilde
Copy link
Owner

ewilde commented Nov 16, 2018

@charleschengxu thank you for the PR, i'm not familiar with glog please allow me some time to evaluate. We use go-runscope in https://github.com/terraform-providers/terraform-provider-runscope, so want to check if adopting this library would impact that.

@canthefason your comment seems like a good suggestions, @charleschengxu thoughts?

GitHub
Terraform runscope provider. Contribute to terraform-providers/terraform-provider-runscope development by creating an account on GitHub.

return response, fmt.Errorf("Status: %s Error reading %s: %s, reason: %q",
resp.Status, resourceType, resourceName, errorResp.ErrorMessage)
}

json.Unmarshal(bodyBytes, &response)
if err = json.Unmarshal(bodyBytes, &response); err != nil {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -52,7 +52,7 @@ func clientConfigure() *Client {
func testPreCheck(t *testing.T) {
skip := os.Getenv("RUNSCOPE_ACC") == ""
if skip {
t.Log("runscope client.go tests require setting RUNSCOPE")
t.Log("runscope client.go tests require setting RUNSCOPE_ACC")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

bucket.go Outdated
type ListTestsInput struct {
BucketName string
Count int
}

// ListBuckets lists all buckets for an account
// ListTests lists all buckets for an account

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all buckets for an account/all tests for a bucket

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@cxuu
Copy link
Contributor Author

cxuu commented Nov 16, 2018

Yes @canthefason has a good point and I added the separate leveling in the latest commit.

@ewilde The logging utility change is unlikely to impact existing clients. If command line inputs are not accessible, one could resort to something like flag.Lookup("v").Value.Set("2") and flag.Lookup("logtostderr").Value.Set("true") in the application.

@ewilde
Copy link
Owner

ewilde commented Nov 26, 2018

@charleschengxu @canthefason I've had sometime this morning to look at this. I do have some reservations introducing a logging library dependency to a package. I would like the end-user of the library to be able to pick their own favourite. The list is pretty big! https://awesome-go.com/#logging

To that end, I had a go at adding some configuration to allow the end-user to drop in their logging library of choice #19 . What do you think?

For you to configure glog, it would probably look something like

handler := func(level int, format string, args ...interface{}) {
    glog.V(level).Infof("Any custom prefix or not %s", fmt.Sprintf(format, args...)) 
}

RegisterLogHandlers(handler, handler, handler)
A curated list of awesome #Golang frameworks, libraries and software

@cxuu
Copy link
Contributor Author

cxuu commented Nov 27, 2018

@ewilde Thanks for the other PR. Once it is merged, I will rebase from it and register the logging utility.

@cxuu cxuu force-pushed the patches branch 2 times, most recently from 4e14e9a to 7935589 Compare November 28, 2018 19:24
@cxuu
Copy link
Contributor Author

cxuu commented Nov 28, 2018

@ewilde I have removed glog from this PR and kept other patches. PTAL

Copy link
Owner

@ewilde ewilde left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

@ewilde ewilde merged commit 80a0796 into ewilde:master Nov 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants