Skip to content

Commit

Permalink
updating readme with some details
Browse files Browse the repository at this point in the history
  • Loading branch information
pbnjay committed Nov 1, 2014
1 parent c110dd4 commit fdf61e4
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion README.md
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)
}
}
}
}()

0 comments on commit fdf61e4

Please sign in to comment.