Skip to content

Commit 5b7ff90

Browse files
joeybloggsjoeybloggs
authored andcommitted
update http handler to use interface, missed during 3.0.0 commits
1 parent 4b4ebd1 commit 5b7ff90

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## log
22
<img align="right" src="https://raw.githubusercontent.com/go-playground/log/master/logo.png">
3-
![Project status](https://img.shields.io/badge/version-3.0.0-green.svg)
3+
![Project status](https://img.shields.io/badge/version-3.0.1-green.svg)
44
[![Build Status](https://semaphoreci.com/api/v1/joeybloggs/log/branches/master/badge.svg)](https://semaphoreci.com/joeybloggs/log)
55
[![Coverage Status](https://coveralls.io/repos/github/go-playground/log/badge.svg?branch=master)](https://coveralls.io/github/go-playground/log?branch=master)
66
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/log)](https://goreportcard.com/report/github.com/go-playground/log)

handlers/http/http.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
// FormatFunc is the function that the workers use to create
1717
// a new Formatter per worker allowing reusable go routine safe
1818
// variable to be used within your Formatter function.
19-
type FormatFunc func(h *HTTP) Formatter
19+
type FormatFunc func(h HTTP) Formatter
2020

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

33+
// HTTP interface to allow for defining handlers based upon this one.
34+
type HTTP interface {
35+
SetFilenameDisplay(fd log.FilenameDisplay)
36+
FilenameDisplay() log.FilenameDisplay
37+
SetBuffersAndWorkers(size uint, workers uint)
38+
SetTimestampFormat(format string)
39+
TimestampFormat() string
40+
GOPATH() string
41+
SetFormatFunc(fn FormatFunc)
42+
Run() chan<- *log.Entry
43+
}
44+
3345
// HTTP is an instance of the http logger
34-
type HTTP struct {
46+
type internalHTTP struct {
3547
buffer uint // channel buffer
3648
numWorkers uint
3749
remoteHost string
@@ -44,14 +56,16 @@ type HTTP struct {
4456
fileDisplay log.FilenameDisplay
4557
}
4658

59+
var _ HTTP = new(internalHTTP)
60+
4761
// New returns a new instance of the http logger
48-
func New(remoteHost string, method string, header stdhttp.Header) (*HTTP, error) {
62+
func New(remoteHost string, method string, header stdhttp.Header) (HTTP, error) {
4963

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

54-
return &HTTP{
68+
return &internalHTTP{
5569
buffer: 0,
5670
remoteHost: remoteHost,
5771
numWorkers: 1,
@@ -65,18 +79,18 @@ func New(remoteHost string, method string, header stdhttp.Header) (*HTTP, error)
6579
}
6680

6781
// SetFilenameDisplay tells HTTP the filename, when present, how to display
68-
func (h *HTTP) SetFilenameDisplay(fd log.FilenameDisplay) {
82+
func (h *internalHTTP) SetFilenameDisplay(fd log.FilenameDisplay) {
6983
h.fileDisplay = fd
7084
}
7185

7286
// FilenameDisplay returns Console's current filename display setting
73-
func (h *HTTP) FilenameDisplay() log.FilenameDisplay {
87+
func (h *internalHTTP) FilenameDisplay() log.FilenameDisplay {
7488
return h.fileDisplay
7589
}
7690

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

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

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

99113
// TimestampFormat returns HTTP's current timestamp output format
100-
func (h *HTTP) TimestampFormat() string {
114+
func (h *internalHTTP) TimestampFormat() string {
101115
return h.timestampFormat
102116
}
103117

104118
// GOPATH returns the GOPATH calculated by HTTP
105-
func (h *HTTP) GOPATH() string {
119+
func (h *internalHTTP) GOPATH() string {
106120
return h.gopath
107121
}
108122

109123
// SetFormatFunc sets FormatFunc each worker will call to get
110124
// a Formatter func
111-
func (h *HTTP) SetFormatFunc(fn FormatFunc) {
125+
func (h *internalHTTP) SetFormatFunc(fn FormatFunc) {
112126
h.formatFunc = fn
113127
}
114128

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

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

136-
func defaultFormatFunc(h *HTTP) Formatter {
150+
func defaultFormatFunc(h HTTP) Formatter {
137151

138152
var b []byte
139153
var file string
@@ -233,7 +247,7 @@ func defaultFormatFunc(h *HTTP) Formatter {
233247
}
234248
}
235249

236-
func (h *HTTP) handleLog(entries <-chan *log.Entry) {
250+
func (h *internalHTTP) handleLog(entries <-chan *log.Entry) {
237251
var e *log.Entry
238252
var b []byte
239253
var reader *bytes.Reader

handlers/http/http_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func TestBadValues(t *testing.T) {
124124
t.Fatalf("Expected '<nil>' Got '%s'", err)
125125
}
126126

127-
hLog.SetFormatFunc(func(h *HTTP) Formatter {
127+
hLog.SetFormatFunc(func(h HTTP) Formatter {
128128
return func(e *log.Entry) []byte {
129129
return []byte(e.Message)
130130
}

0 commit comments

Comments
 (0)