Skip to content

Commit

Permalink
refactor(HttpLogger): Exported HttpLogger struct for API use
Browse files Browse the repository at this point in the history
  • Loading branch information
kolin-newby committed Aug 24, 2021
1 parent eaf94db commit c4bf509
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 44 deletions.
18 changes: 10 additions & 8 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@

<a name="creating_loggers"/>

## Creating Loggers
## Creating Loggers

To get started, first you'll need to create a `HttpLogger` instance. Here there are options to specify a URL (for where JSON
messages will be sent) and/or a specific set of <a href="https://resurface.io/rules.html">logging rules</a> (for what privacy
To get started, first you'll need to create a `HttpLogger` instance. Here there are options to specify a URL (for where JSON
messages will be sent) and/or a specific set of <a href="https://resurface.io/rules.html">logging rules</a> (for what privacy
protections to apply). Default values will be used for either of these if specific values are not provided.

```golang
const resurfaceio = require('resurfaceio-logger');
const httpLogger = resurfaceio.HttpLogger;
const HttpLogger = resurfaceio.HttpLogger;

// with default url and rules
logger := NewHttpLogger(Options{});
Expand Down Expand Up @@ -51,14 +51,15 @@ logger := NewHttpLogger(opt);
## Logging HTTP Calls

Now that you have a logger instance, let's do some logging. Here you can pass standard request/response objects, as well
as response body and request body content when these are available.
as response body and request body content when these are available.

```golang
const HttpMessage = resurfaceio.HttpMessage;

// with standard objects
SendHttpMessage(logger, response, request, start_time)
```

Request and Response bodies are automatically logged.

If standard request and response objects aren't available in your case, create mock implementations to pass instead.
Expand Down Expand Up @@ -86,7 +87,7 @@ HttpMessage.send(logger, request, response);

## Setting Default Rules

If no <a href="https://resurface.io/rules.html">rules</a> are provided when creating a logger, the default value of
If no <a href="https://resurface.io/rules.html">rules</a> are provided when creating a logger, the default value of
`include strict` will be applied. A different default value can be specified as shown below.

```golang
Expand Down Expand Up @@ -131,8 +132,8 @@ UsageLoggers.Enable(); // enable all loggers
```

All loggers can be permanently disabled with the `USAGE_LOGGERS_DISABLE` environment variable. When set to true,
loggers will never become enabled, even if `UsageLoggers.enable()` is called by the application. This is primarily
done by automated tests to disable all logging even if other control logic exists.
loggers will never become enabled, even if `UsageLoggers.enable()` is called by the application. This is primarily
done by automated tests to disable all logging even if other control logic exists.

```bash
# from command line
Expand All @@ -143,4 +144,5 @@ heroku config:set USAGE_LOGGERS_DISABLE=true
```

---

<small>&copy; 2016-2021 <a href="https://resurface.io">Resurface Labs Inc.</a></small>
24 changes: 12 additions & 12 deletions HttpClientLogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ import (
//NetHttpClientLogger defines a struct used to log specifically from the client side of API interactions using the net/http package.
type NetHttpClientLogger struct {
http.Client
httpLogger *httpLogger
HttpLogger *HttpLogger
}

//NewNetHttpClientLoggerOptions() takes 1 argument of type logger.Options and returns 2 objects; a pointer to an instance of an NetHttpClientLogger struct and an error.
//The NetHttpClientLogger returned by this function has the given options applied.
//If there is no error, the error value returned will be nil.
func NewNetHttpClientLoggerOptions(options Options) (*NetHttpClientLogger, error) {
httpLogger, err := NewHttpLogger(options)
HttpLogger, err := NewHttpLogger(options)
if err != nil {
return nil, err
}
return &NetHttpClientLogger{
httpLogger: httpLogger,
HttpLogger: HttpLogger,
}, nil
}

Expand All @@ -33,17 +33,17 @@ func NewNetHttpClientLoggerOptions(options Options) (*NetHttpClientLogger, error
//If there is no error, the error value returned will be nil.
func NewNetHttpClientLogger() (*NetHttpClientLogger, error) {
options := Options{}
httpLogger, err := NewHttpLogger(options)
HttpLogger, err := NewHttpLogger(options)
if err != nil {
return nil, err
}
return &NetHttpClientLogger{
httpLogger: httpLogger,
HttpLogger: HttpLogger,
}, nil
}

func (logger *NetHttpClientLogger) Logger() *httpLogger {
return logger.httpLogger
func (logger *NetHttpClientLogger) Logger() *HttpLogger {
return logger.HttpLogger
}

//net.http.Client.CloseIdleConnections() wrapper
Expand All @@ -56,7 +56,7 @@ func (clientLogger *NetHttpClientLogger) Do(req *http.Request) (resp *http.Respo
// start time for logging interval
start := time.Now()

logger := clientLogger.httpLogger
logger := clientLogger.HttpLogger

// capture the response or error
resp, err = clientLogger.Client.Do(req)
Expand All @@ -76,7 +76,7 @@ func (clientLogger *NetHttpClientLogger) Get(url string) (resp *http.Response, e
// start time for logging interval
start := time.Now()

logger := clientLogger.httpLogger
logger := clientLogger.HttpLogger

// capture the response or error
// Devin 03/31/2021
Expand All @@ -98,7 +98,7 @@ func (clientLogger *NetHttpClientLogger) Head(url string) (resp *http.Response,
// start time for logging interval
start := time.Now()

logger := clientLogger.httpLogger
logger := clientLogger.HttpLogger

// capture the response or error
resp, err = clientLogger.Client.Head(url)
Expand All @@ -118,7 +118,7 @@ func (clientLogger *NetHttpClientLogger) Post(url string, contentType string, bo
// start time for logging interval
start := time.Now()

logger := clientLogger.httpLogger
logger := clientLogger.HttpLogger

// capture the response or error
resp, err = clientLogger.Client.Post(url, contentType, body)
Expand All @@ -138,7 +138,7 @@ func (clientLogger *NetHttpClientLogger) PostForm(url string, data url.Values) (
// start time for logging interval
start := time.Now()

logger := clientLogger.httpLogger
logger := clientLogger.HttpLogger

// capture the response or error
resp, err = clientLogger.Client.PostForm(url, data)
Expand Down
12 changes: 6 additions & 6 deletions HttpLogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ type Options struct {

const httpLoggerAgent string = "HttpLogger.go"

//base httpLogger definition
type httpLogger struct {
//HttpLogger is the struct contains a pointer to a baseLogger instance and a set of rules used to define the behaviour of the logger.
type HttpLogger struct {
*baseLogger
rules *HttpRules
}

// NewHttpLogger returns a pointer to a new httpLogger object, with the given options applied, and an error
func NewHttpLogger(options Options) (*httpLogger, error) {
// NewHttpLogger returns a pointer to a new HttpLogger object, with the given options applied, and an error
func NewHttpLogger(options Options) (*HttpLogger, error) {
baseLogger := newBaseLogger(httpLoggerAgent, options.Url, options.Enabled, options.Queue)

loggerRules, err := newHttpRules(options.Rules)
if err != nil {
return nil, err
}

logger := &httpLogger{
logger := &HttpLogger{
baseLogger,
loggerRules,
}
Expand All @@ -60,7 +60,7 @@ func NewHttpLogger(options Options) (*httpLogger, error) {
return logger, nil
}

func (logger *httpLogger) submitIfPassing(msg [][]string) {
func (logger *HttpLogger) submitIfPassing(msg [][]string) {
msg = logger.rules.apply(msg)

if msg == nil {
Expand Down
12 changes: 6 additions & 6 deletions HttpLoggerForMux.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
type (
// HttpLoggerForMux defines a struct used to log specifically gorilla/mux apps
HttpLoggerForMux struct {
httpLogger *httpLogger
HttpLogger *HttpLogger
startTime time.Time
interval time.Duration
response []byte
Expand All @@ -30,14 +30,14 @@ type (
// If there is no error, the error value returned will be nil.
func NewHttpLoggerForMux() (*HttpLoggerForMux, error) {

httpLogger, err := NewHttpLogger(Options{})
HttpLogger, err := NewHttpLogger(Options{})

if err != nil {
return nil, err
}

httpLoggerForMux := HttpLoggerForMux{
httpLogger: httpLogger,
HttpLogger: HttpLogger,
startTime: time.Time{},
interval: 0,
response: make([]byte, 0),
Expand All @@ -50,14 +50,14 @@ func NewHttpLoggerForMux() (*HttpLoggerForMux, error) {
// If there is no error, the error value returned will be nil.
func NewHttpLoggerForMuxOptions(options Options) (*HttpLoggerForMux, error) {

httpLogger, err := NewHttpLogger(options)
HttpLogger, err := NewHttpLogger(options)

if err != nil {
return nil, err
}

httpLoggerForMux := HttpLoggerForMux{
httpLogger: httpLogger,
HttpLogger: HttpLogger,
startTime: time.Time{},
interval: 0,
response: make([]byte, 0),
Expand Down Expand Up @@ -141,6 +141,6 @@ func (muxLogger HttpLoggerForMux) LogData(next http.Handler) http.Handler {

muxLogger.startTime = time.Now()

SendHttpMessage(muxLogger.httpLogger, loggingWriter.loggingResp, loggingReq, muxLogger.startTime)
SendHttpMessage(muxLogger.HttpLogger, loggingWriter.loggingResp, loggingReq, muxLogger.startTime)
})
}
18 changes: 9 additions & 9 deletions HttpLogger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import (
func TestCreateInstance(t *testing.T) {

//Creating a single instance
httpLogger, _ := NewHttpLogger(Options{})
assert.NotNil(t, httpLogger)
assert.Equal(t, httpLoggerAgent, httpLogger.agent)
assert.False(t, httpLogger.enableable)
assert.False(t, httpLogger.Enabled())
assert.Nil(t, httpLogger.queue)
assert.Equal(t, "", httpLogger.url)
HttpLogger, _ := NewHttpLogger(Options{})
assert.NotNil(t, HttpLogger)
assert.Equal(t, httpLoggerAgent, HttpLogger.agent)
assert.False(t, HttpLogger.enableable)
assert.False(t, HttpLogger.Enabled())
assert.Nil(t, HttpLogger.queue)
assert.Equal(t, "", HttpLogger.url)

}

Expand Down Expand Up @@ -84,13 +84,13 @@ func TestCreateMultipleInstances(t *testing.T) {

func TestHasValidAgent(t *testing.T) {
//Has Valid Agent Test
httpLogger, _ := NewHttpLogger(Options{})
HttpLogger, _ := NewHttpLogger(Options{})

assert.Greater(t, len(httpLoggerAgent), 0)
assert.Equal(t, ".go", httpLoggerAgent[len(httpLoggerAgent)-3:])
assert.NotContains(t, httpLoggerAgent, "\\")
assert.NotContains(t, httpLoggerAgent, "\"")
assert.NotContains(t, httpLoggerAgent, "'")
assert.Equal(t, httpLoggerAgent, httpLogger.agent)
assert.Equal(t, httpLoggerAgent, HttpLogger.agent)

}
4 changes: 2 additions & 2 deletions HttpMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ func buildHttpMessage(req *http.Request, resp *http.Response) [][]string {

}

// SendHttpMessage(l *httpLogger, resp *http.Response, req *http.Request, t time.Time) Uses logger l to send a log of the given resp, req, and t to the loggers url
// SendHttpMessage(l *HttpLogger, resp *http.Response, req *http.Request, t time.Time) Uses logger l to send a log of the given resp, req, and t to the loggers url
// t defines the start time of the logging process used to calculate the logging interval
func SendHttpMessage(logger *httpLogger, resp *http.Response, req *http.Request, start time.Time) {
func SendHttpMessage(logger *HttpLogger, resp *http.Response, req *http.Request, start time.Time) {

if !logger.Enabled() {
return
Expand Down
2 changes: 1 addition & 1 deletion HttpRules.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ func (rules *HttpRules) apply(details [][]string) [][]string {
}

/*
The following unexported Regexps should be treat as constants
The following unexported Regexps should be treated as constants
and remain unchanged throughout package usage
*/
var regexAllowHttpUrl *regexp.Regexp = regexp.MustCompile(`^\s*allow_http_url\s*(#.*)?$`)
Expand Down

0 comments on commit c4bf509

Please sign in to comment.