Skip to content

Commit 77995e7

Browse files
committed
Remove ErrorLogger from renderer
It didn't make sense to have the ErrorLogger part of the renderer interface if it was ONLY used for http.Server error handling. We don't plan to render that information to any other renderer, such as logging. Insted we'll just print the errors to the CLI output using pterm.
1 parent 77bfb4b commit 77995e7

File tree

7 files changed

+58
-37
lines changed

7 files changed

+58
-37
lines changed

pkg/renderer/printer.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package renderer
22

33
import (
44
"fmt"
5-
"log"
65
"os"
76
"sort"
87
"strings"
@@ -59,12 +58,6 @@ func (p *Printer) startText() string {
5958
return text
6059
}
6160

62-
// ErrorLogger will create a printerLog which interfaces with Logger.
63-
func (p *Printer) ErrorLogger() *log.Logger {
64-
errorLog := log.New(&printerLog{prefix: pterm.Error}, "", p.Port)
65-
return errorLog
66-
}
67-
6861
// Fatal will use the Error prefix to render the error and then exit the CLI.
6962
func (p *Printer) Fatal(err error) {
7063
p.Spinner.Stop()

pkg/renderer/printerLog.go

Lines changed: 0 additions & 18 deletions
This file was deleted.

pkg/renderer/printer_log.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package renderer
2+
3+
import "github.com/pterm/pterm"
4+
5+
// printerLog is our interface to Logger and accepts a pterm prefix
6+
type PrinterLog struct {
7+
Prefix pterm.PrefixPrinter
8+
}
9+
10+
// Write will be used by the function calling our printerLog.
11+
// If no prefix is passed, we default to Info
12+
func (pl *PrinterLog) Write(b []byte) (n int, err error) {
13+
if pl.Prefix.Prefix.Text == "" {
14+
pl.Prefix = pterm.Info
15+
}
16+
17+
str := pl.Prefix.WithShowLineNumber(false).Sprint(string(b))
18+
pterm.Println(str)
19+
20+
return len(str), nil
21+
}

pkg/renderer/printer_log_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package renderer
2+
3+
import (
4+
"testing"
5+
6+
"github.com/pterm/pterm"
7+
)
8+
9+
func TestWriteDefaultPrefix(t *testing.T) {
10+
pterm.DisableOutput()
11+
12+
pl := PrinterLog{}
13+
b := []byte("foobar")
14+
i, _ := pl.Write(b)
15+
16+
if i != 34 {
17+
t.Errorf("Expected str len %d, got %d", 34, i)
18+
}
19+
}
20+
21+
func TestWriteErrorPrefix(t *testing.T) {
22+
pterm.DisableOutput()
23+
24+
pl := PrinterLog{Prefix: pterm.Error}
25+
b := []byte("foobar")
26+
i, _ := pl.Write(b)
27+
28+
if i != 38 {
29+
t.Errorf("Expected str len %d, got %d", 38, i)
30+
}
31+
}

pkg/renderer/renderer.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package renderer
22

33
import (
4-
"log"
5-
64
"github.com/aaronvb/logrequest"
75
)
86

@@ -11,10 +9,6 @@ type Renderer interface {
119
// Start is called when we start our server.
1210
Start()
1311

14-
// ErrorLogger can be used if we need to access the logger interface.
15-
// This is useful in certain cases such as the ErrorLog when using the http.Server
16-
ErrorLogger() *log.Logger
17-
1812
// Fatal is used when we need to display a message and should always exit the CLI.
1913
Fatal(error)
2014

pkg/server/http.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package server
22

33
import (
44
"fmt"
5+
"log"
56
"net/http"
67

78
"github.com/aaronvb/logparams"
89
"github.com/aaronvb/logrequest"
910
"github.com/aaronvb/request_hole/pkg/renderer"
1011
"github.com/gorilla/mux"
12+
"github.com/pterm/pterm"
1113
)
1214

1315
type Http struct {
@@ -31,10 +33,11 @@ type Http struct {
3133
func (s *Http) Start() {
3234
s.Output.Start()
3335
addr := fmt.Sprintf("%s:%d", s.Addr, s.Port)
36+
errorLog := log.New(&renderer.PrinterLog{Prefix: pterm.Error}, "", 0)
3437

3538
srv := &http.Server{
3639
Addr: addr,
37-
ErrorLog: s.Output.ErrorLogger(),
40+
ErrorLog: errorLog,
3841
Handler: s.routes(),
3942
}
4043

pkg/server/http_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ package server
22

33
import (
44
"bytes"
5-
"log"
65
"net/http"
76
"net/http/httptest"
8-
"os"
97
"testing"
108

119
"github.com/aaronvb/logrequest"
@@ -17,10 +15,9 @@ type MockPrinter struct {
1715
headers map[string][]string
1816
}
1917

20-
func (mp *MockPrinter) Fatal(error) {}
21-
func (mp *MockPrinter) Start() {}
22-
func (mp *MockPrinter) ErrorLogger() *log.Logger { return log.New(os.Stderr, "", 0) }
2318
func (mp *MockPrinter) IncomingRequest(fields logrequest.RequestFields, params string, headers map[string][]string) {
19+
func (mp *MockPrinter) Fatal(error) {}
20+
func (mp *MockPrinter) Start() {}
2421

2522
func (mp *MockPrinter) IncomingRequest(fields logrequest.RequestFields, params string) {
2623
mp.fields = fields

0 commit comments

Comments
 (0)