forked from jianyuan/terraform-provider-sentry
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* initial idea implemented, not tested * better logging interface, jsonify objects in logs * http response logging as an other log instead of integration * add more specific header elements * fix typo
- Loading branch information
1 parent
50e08b0
commit e9baf2e
Showing
10 changed files
with
117 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package logging | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
) | ||
|
||
// logKeyValuePair is a helper struct to help organise the use of tflog. tflog | ||
// uses pairs to print key-value items/args, so this is just to help visualize the | ||
// way the logging works | ||
type logKeyValuePair struct { | ||
// Key is what will appear next to the value, e.g. keyID=... | ||
Key string | ||
// The value is what you want to log. e.g. Key=Value | ||
Value interface{} | ||
} | ||
|
||
func makePair(key string, value interface{}) logKeyValuePair { | ||
return logKeyValuePair{ | ||
Key: key, | ||
Value: value, | ||
} | ||
} | ||
|
||
// TryJsonify tries to marshall the object into a json string. | ||
// If a failure should occur, it will return the untouched object. | ||
func TryJsonify(object interface{}) interface{} { | ||
jsonObject, err := json.Marshal(object) | ||
if err == nil { | ||
return string(jsonObject) | ||
} | ||
return object | ||
} | ||
|
||
// extractHttpResponseElements is an indirection for if it is ever needed to add more elements. | ||
func extractHttpResponseElements(resp *http.Response) []logKeyValuePair { | ||
return []logKeyValuePair{ | ||
makePair("responseContentType", resp.Header.Get("content-type")), | ||
makePair("responseContentLength", resp.Header.Get("content-length")), | ||
makePair("responseStatus", resp.Status), | ||
makePair("requestUrl", resp.Request.URL.String()), | ||
} | ||
} | ||
|
||
// ExtractHttpResponse extracts key-value pairs from the http.Response object. | ||
// This is to match the args signature of the tflog package. | ||
func ExtractHttpResponse(resp *http.Response) []interface{} { | ||
elements := extractHttpResponseElements(resp) | ||
args := []interface{}{} | ||
for _, pair := range elements { | ||
args = append(args, pair.Key, pair.Value) | ||
} | ||
return args | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.