Skip to content

Commit 8a6f1cc

Browse files
committed
Add channel support to logger
- Added a channel and receive from it. - Move headers back into the incoming request. We now use a struct, RequestPayload, which contains all the incoming request data.
1 parent fdeed0a commit 8a6f1cc

File tree

2 files changed

+53
-53
lines changed

2 files changed

+53
-53
lines changed

pkg/renderer/logger.go

Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,89 +5,87 @@ import (
55
"os"
66
"sort"
77
"strings"
8+
"sync"
89
"time"
910

10-
"github.com/aaronvb/logrequest"
11+
"github.com/aaronvb/request_hole/pkg/protocol"
1112
"github.com/pterm/pterm"
1213
)
1314

1415
// Logger outputs to a log file.
1516
type Logger struct {
16-
// File points to the file that we write to.
17-
File string
17+
// FilePathis the path to the log file which we write to.
18+
// Default is blank unless passed as a flag to the CLI.
19+
FilePath string
1820

19-
// Fields for startText
20-
Port int
21+
// Details will log the headers with the request. Default is false unless the
22+
// flag is passed.
23+
Details bool
24+
25+
// Address and Port are used for the start text
2126
Addr string
27+
Port int
28+
29+
// LogFile is the open log file
30+
logFile *os.File
2231
}
2332

2433
// Start writes the initial server start to the log file.
25-
func (l *Logger) Start() {
26-
if l.File == "" {
27-
return
28-
}
29-
30-
f, err := os.OpenFile(l.File, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
34+
func (l *Logger) Start(wg *sync.WaitGroup, rp chan protocol.RequestPayload, q chan int) {
35+
f, err := os.OpenFile(l.FilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
3136
if err != nil {
32-
l.Fatal(err)
37+
fatal(err)
3338
}
3439

3540
defer f.Close()
41+
defer wg.Done()
3642

3743
str := fmt.Sprintf("%s: %s\n", time.Now().Format("2006/02/01 15:04:05"), l.startText())
3844
f.WriteString(str)
45+
46+
// Set logFile to open file
47+
l.logFile = f
48+
49+
// Receive incoming requests on RequestPayload channel or
50+
// exit blocking select if quit is received from protocol
51+
for {
52+
select {
53+
case r := <-rp:
54+
l.incomingRequest(r)
55+
case <-q:
56+
return
57+
}
58+
}
3959
}
4060

61+
// startText returns the starting log string
4162
func (l *Logger) startText() string {
4263
return fmt.Sprintf("Listening on http://%s:%d", l.Addr, l.Port)
4364
}
4465

45-
// Fatal will use the Error prefix to render the error and then exit the CLI.
46-
func (l *Logger) Fatal(err error) {
66+
// fatal will use the Error prefix to render the error and then exit the CLI.
67+
func fatal(err error) {
4768
pterm.Error.WithShowLineNumber(false).Println(err)
4869
os.Exit(1)
4970
}
5071

51-
// IncomingRequest writes the incoming requests to the log file.
52-
func (l *Logger) IncomingRequest(fields logrequest.RequestFields, params string) {
53-
if l.File == "" {
54-
return
55-
}
56-
57-
f, err := os.OpenFile(l.File, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
58-
if err != nil {
59-
l.Fatal(err)
72+
// incomingRequest handles the log output for incoming requests to the protocol..
73+
func (l *Logger) incomingRequest(r protocol.RequestPayload) {
74+
str := fmt.Sprintf("%s: %s\n", time.Now().Format("2006/02/01 15:04:05"), l.incomingRequestText(r))
75+
l.logFile.WriteString(str)
76+
77+
if l.Details {
78+
headersWithJoinedValues, keys := l.incomingRequestHeaders(r.Headers)
79+
for _, key := range keys {
80+
str := fmt.Sprintf("%s: %s: %s\n", time.Now().Format("2006/02/01 15:04:05"), key, headersWithJoinedValues[key])
81+
l.logFile.WriteString(str)
82+
}
6083
}
61-
62-
defer f.Close()
63-
64-
str := fmt.Sprintf("%s: %s\n", time.Now().Format("2006/02/01 15:04:05"), l.incomingRequestText(fields, params))
65-
f.WriteString(str)
66-
}
67-
68-
func (l *Logger) incomingRequestText(fields logrequest.RequestFields, params string) string {
69-
return fmt.Sprintf("%s %s %s", fields.Method, fields.Url, params)
7084
}
7185

72-
// IncomingRequestHeaders writes the incoming request headers to the log file
73-
func (l *Logger) IncomingRequestHeaders(headers map[string][]string) {
74-
if l.File == "" {
75-
return
76-
}
77-
78-
f, err := os.OpenFile(l.File, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
79-
if err != nil {
80-
l.Fatal(err)
81-
}
82-
83-
defer f.Close()
84-
85-
headersWithJoinedValues, keys := l.incomingRequestHeaders(headers)
86-
87-
for _, key := range keys {
88-
str := fmt.Sprintf("%s: %s: %s\n", time.Now().Format("2006/02/01 15:04:05"), key, headersWithJoinedValues[key])
89-
f.WriteString(str)
90-
}
86+
// incomingRequestText converts the RequestPayload into a printable string.
87+
func (l *Logger) incomingRequestText(r protocol.RequestPayload) string {
88+
return fmt.Sprintf("%s %s %s", r.Fields.Method, r.Fields.Url, r.Params)
9189
}
9290

9391
// incomingRequestHeaders takes the headers from the request, sorts them alphabetically,

pkg/renderer/logger_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import (
66
"testing"
77

88
"github.com/aaronvb/logrequest"
9+
"github.com/aaronvb/request_hole/pkg/protocol"
910
)
1011

1112
func TestLoggerStartText(t *testing.T) {
12-
logger := Logger{Port: 123, Addr: "foo.bar"}
13+
logger := Logger{Addr: "localhost", Port: 1234}
1314
text := logger.startText()
1415
expected := fmt.Sprintf("Listening on http://%s:%d", logger.Addr, logger.Port)
1516

@@ -25,7 +26,8 @@ func TestLoggerIncomingRequest(t *testing.T) {
2526
Url: "/foobar",
2627
}
2728
params := "{\"foo\" => \"bar\"}"
28-
text := logger.incomingRequestText(fields, params)
29+
rp := protocol.RequestPayload{Fields: fields, Params: params}
30+
text := logger.incomingRequestText(rp)
2931
expected := fmt.Sprintf("%s %s %s", fields.Method, fields.Url, params)
3032

3133
if text != expected {

0 commit comments

Comments
 (0)