Skip to content

Commit

Permalink
update http handler to use interface, missed during 3.0.0 commits
Browse files Browse the repository at this point in the history
  • Loading branch information
joeybloggs authored and joeybloggs committed Jun 6, 2016
1 parent 4b4ebd1 commit 5b7ff90
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## log
<img align="right" src="https://raw.githubusercontent.com/go-playground/log/master/logo.png">
![Project status](https://img.shields.io/badge/version-3.0.0-green.svg)
![Project status](https://img.shields.io/badge/version-3.0.1-green.svg)
[![Build Status](https://semaphoreci.com/api/v1/joeybloggs/log/branches/master/badge.svg)](https://semaphoreci.com/joeybloggs/log)
[![Coverage Status](https://coveralls.io/repos/github/go-playground/log/badge.svg?branch=master)](https://coveralls.io/github/go-playground/log?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/log)](https://goreportcard.com/report/github.com/go-playground/log)
Expand Down
42 changes: 28 additions & 14 deletions handlers/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// FormatFunc is the function that the workers use to create
// a new Formatter per worker allowing reusable go routine safe
// variable to be used within your Formatter function.
type FormatFunc func(h *HTTP) Formatter
type FormatFunc func(h HTTP) Formatter

// Formatter is the function used to format the HTTP entry
type Formatter func(e *log.Entry) []byte
Expand All @@ -30,8 +30,20 @@ const (
gopath = "GOPATH"
)

// HTTP interface to allow for defining handlers based upon this one.
type HTTP interface {
SetFilenameDisplay(fd log.FilenameDisplay)
FilenameDisplay() log.FilenameDisplay
SetBuffersAndWorkers(size uint, workers uint)
SetTimestampFormat(format string)
TimestampFormat() string
GOPATH() string
SetFormatFunc(fn FormatFunc)
Run() chan<- *log.Entry
}

// HTTP is an instance of the http logger
type HTTP struct {
type internalHTTP struct {
buffer uint // channel buffer
numWorkers uint
remoteHost string
Expand All @@ -44,14 +56,16 @@ type HTTP struct {
fileDisplay log.FilenameDisplay
}

var _ HTTP = new(internalHTTP)

// New returns a new instance of the http logger
func New(remoteHost string, method string, header stdhttp.Header) (*HTTP, error) {
func New(remoteHost string, method string, header stdhttp.Header) (HTTP, error) {

if _, err := url.Parse(remoteHost); err != nil {
return nil, err
}

return &HTTP{
return &internalHTTP{
buffer: 0,
remoteHost: remoteHost,
numWorkers: 1,
Expand All @@ -65,18 +79,18 @@ func New(remoteHost string, method string, header stdhttp.Header) (*HTTP, error)
}

// SetFilenameDisplay tells HTTP the filename, when present, how to display
func (h *HTTP) SetFilenameDisplay(fd log.FilenameDisplay) {
func (h *internalHTTP) SetFilenameDisplay(fd log.FilenameDisplay) {
h.fileDisplay = fd
}

// FilenameDisplay returns Console's current filename display setting
func (h *HTTP) FilenameDisplay() log.FilenameDisplay {
func (h *internalHTTP) FilenameDisplay() log.FilenameDisplay {
return h.fileDisplay
}

// SetBuffersAndWorkers sets the channels buffer size and number of concurrent workers.
// These settings should be thought about together, hence setting both in the same function.
func (h *HTTP) SetBuffersAndWorkers(size uint, workers uint) {
func (h *internalHTTP) SetBuffersAndWorkers(size uint, workers uint) {
h.buffer = size

if workers == 0 {
Expand All @@ -92,28 +106,28 @@ func (h *HTTP) SetBuffersAndWorkers(size uint, workers uint) {

// SetTimestampFormat sets HTTP's timestamp output format
// Default is : "2006-01-02T15:04:05.000000000Z07:00"
func (h *HTTP) SetTimestampFormat(format string) {
func (h *internalHTTP) SetTimestampFormat(format string) {
h.timestampFormat = format
}

// TimestampFormat returns HTTP's current timestamp output format
func (h *HTTP) TimestampFormat() string {
func (h *internalHTTP) TimestampFormat() string {
return h.timestampFormat
}

// GOPATH returns the GOPATH calculated by HTTP
func (h *HTTP) GOPATH() string {
func (h *internalHTTP) GOPATH() string {
return h.gopath
}

// SetFormatFunc sets FormatFunc each worker will call to get
// a Formatter func
func (h *HTTP) SetFormatFunc(fn FormatFunc) {
func (h *internalHTTP) SetFormatFunc(fn FormatFunc) {
h.formatFunc = fn
}

// Run starts the logger consuming on the returned channed
func (h *HTTP) Run() chan<- *log.Entry {
func (h *internalHTTP) Run() chan<- *log.Entry {

// pre-setup
if h.fileDisplay == log.Llongfile {
Expand All @@ -133,7 +147,7 @@ func (h *HTTP) Run() chan<- *log.Entry {
return ch
}

func defaultFormatFunc(h *HTTP) Formatter {
func defaultFormatFunc(h HTTP) Formatter {

var b []byte
var file string
Expand Down Expand Up @@ -233,7 +247,7 @@ func defaultFormatFunc(h *HTTP) Formatter {
}
}

func (h *HTTP) handleLog(entries <-chan *log.Entry) {
func (h *internalHTTP) handleLog(entries <-chan *log.Entry) {
var e *log.Entry
var b []byte
var reader *bytes.Reader
Expand Down
2 changes: 1 addition & 1 deletion handlers/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestBadValues(t *testing.T) {
t.Fatalf("Expected '<nil>' Got '%s'", err)
}

hLog.SetFormatFunc(func(h *HTTP) Formatter {
hLog.SetFormatFunc(func(h HTTP) Formatter {
return func(e *log.Entry) []byte {
return []byte(e.Message)
}
Expand Down

0 comments on commit 5b7ff90

Please sign in to comment.