forked from pbnjay/harhar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
55 additions
and
1 deletion.
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 |
---|---|---|
@@ -1,4 +1,58 @@ | ||
harhar | ||
====== | ||
|
||
Simple Transparent HAR logging for server-side Go http.Client usage. | ||
Simple, transparent HAR logging for Go code using the http.Client interface. | ||
For existing code that already uses the `net/http` package, updating it to | ||
produce HAR logs is typically only 2 lines of code. | ||
|
||
Getting Started | ||
--------------- | ||
|
||
First, convert your existing http.Client instance (or http.DefaultClient) to | ||
a harhar.Client: | ||
|
||
// before | ||
webClient := &http.Client{} | ||
|
||
// after | ||
httpClient := &http.Client{} | ||
webClient := harhar.NewClient(httpClient) | ||
|
||
Then, whenever you're ready to generate the HAR output, call WriteLog: | ||
|
||
webClient.WriteLog("output.har") | ||
|
||
That's it! harhar.Client implements all the same methods as http.Client, so no | ||
other code will need to be changed. However, if you set Timeouts, Cookies, etc. | ||
dynamically then you will want to retain a copy of the wrapped http.Client. | ||
harhar.Client only stores the pointer, so changes to the underlying http.Client | ||
will be used immediately. | ||
|
||
Optional periodic logging | ||
------------------------- | ||
|
||
To dynamically enable or disable HAR logging, code can use harhar.ClientInterface | ||
to represent either an http.Client or harhar.Client. When using this interface, | ||
you can write logs (if enabled) by using this simple block of code: | ||
|
||
if harCli, ok := myClient.(*harhar.Client); ok { | ||
harCli.WriteLog("output.har") | ||
} | ||
|
||
When combined with a long-running process, the interface makes it possible to | ||
toggle logging off and on, and periodically write to disk throughout a processes | ||
lifetime. An example is the following (never-ending) goroutine: | ||
|
||
go func(){ | ||
for _ = range time.Tick(time.Minute*5) { | ||
if harCli, ok := myClient.(*harhar.Client); ok { | ||
sz, err := harCli.WriteLog("output.har") | ||
if err!=nil { | ||
log.Println("error writing .har log:", err) | ||
} else { | ||
log.Printf("wrote .har log (%.1fkb)\n", float64(sz)/1024.0) | ||
} | ||
} | ||
} | ||
}() | ||
|