-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Accepts a CLI flag `--log <filename>` which will write the CLI output to a log file.
- Loading branch information
Showing
7 changed files
with
211 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package renderer | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"sort" | ||
"strings" | ||
"time" | ||
|
||
"github.com/aaronvb/logrequest" | ||
"github.com/pterm/pterm" | ||
) | ||
|
||
// Logger outputs to a log file. | ||
type Logger struct { | ||
// File points to the file that we write to. | ||
File string | ||
|
||
// Fields for startText | ||
Port int | ||
Addr string | ||
} | ||
|
||
// Start writes the initial server start to the log file. | ||
func (l *Logger) Start() { | ||
if l.File == "" { | ||
return | ||
} | ||
|
||
f, err := os.OpenFile(l.File, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | ||
if err != nil { | ||
l.Fatal(err) | ||
} | ||
|
||
defer f.Close() | ||
|
||
str := fmt.Sprintf("%s: %s\n", time.Now().Format("2006/02/01 15:04:05"), l.startText()) | ||
f.WriteString(str) | ||
} | ||
|
||
func (l *Logger) startText() string { | ||
return fmt.Sprintf("Listening on http:https://%s:%d", l.Addr, l.Port) | ||
} | ||
|
||
// Fatal will use the Error prefix to render the error and then exit the CLI. | ||
func (l *Logger) Fatal(err error) { | ||
pterm.Error.WithShowLineNumber(false).Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
// IncomingRequest writes the incoming requests to the log file. | ||
func (l *Logger) IncomingRequest(fields logrequest.RequestFields, params string) { | ||
if l.File == "" { | ||
return | ||
} | ||
|
||
f, err := os.OpenFile(l.File, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | ||
if err != nil { | ||
l.Fatal(err) | ||
} | ||
|
||
defer f.Close() | ||
|
||
str := fmt.Sprintf("%s: %s\n", time.Now().Format("2006/02/01 15:04:05"), l.incomingRequestText(fields, params)) | ||
f.WriteString(str) | ||
} | ||
|
||
func (l *Logger) incomingRequestText(fields logrequest.RequestFields, params string) string { | ||
return fmt.Sprintf("%s %s %s", fields.Method, fields.Url, params) | ||
} | ||
|
||
// IncomingRequestHeaders writes the incoming request headers to the log file | ||
func (l *Logger) IncomingRequestHeaders(headers map[string][]string) { | ||
if l.File == "" { | ||
return | ||
} | ||
|
||
f, err := os.OpenFile(l.File, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | ||
if err != nil { | ||
l.Fatal(err) | ||
} | ||
|
||
defer f.Close() | ||
|
||
headersWithJoinedValues, keys := l.incomingRequestHeaders(headers) | ||
|
||
for _, key := range keys { | ||
str := fmt.Sprintf("%s: %s: %s\n", time.Now().Format("2006/02/01 15:04:05"), key, headersWithJoinedValues[key]) | ||
f.WriteString(str) | ||
} | ||
} | ||
|
||
// incomingRequestHeaders takes the headers from the request, sorts them alphabetically, | ||
// joins the values, and creates a new map | ||
func (l *Logger) incomingRequestHeaders(headers map[string][]string) (map[string]string, []string) { | ||
headersWithJoinedValues := make(map[string]string) | ||
for key, val := range headers { | ||
value := strings.Join(val, ",") | ||
headersWithJoinedValues[key] = value | ||
} | ||
|
||
keys := make([]string, 0, len(headers)) | ||
for key := range headers { | ||
keys = append(keys, key) | ||
} | ||
|
||
sort.Strings(keys) | ||
|
||
return headersWithJoinedValues, keys | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package renderer | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/aaronvb/logrequest" | ||
) | ||
|
||
func TestLoggerStartText(t *testing.T) { | ||
logger := Logger{Port: 123, Addr: "foo.bar"} | ||
text := logger.startText() | ||
expected := fmt.Sprintf("Listening on http:https://%s:%d", logger.Addr, logger.Port) | ||
|
||
if text != expected { | ||
t.Errorf("Expected %s, got %s", expected, text) | ||
} | ||
} | ||
|
||
func TestLoggerIncomingRequest(t *testing.T) { | ||
logger := Logger{} | ||
fields := logrequest.RequestFields{ | ||
Method: "GET", | ||
Url: "/foobar", | ||
} | ||
params := "{\"foo\" => \"bar\"}" | ||
text := logger.incomingRequestText(fields, params) | ||
expected := fmt.Sprintf("%s %s %s", fields.Method, fields.Url, params) | ||
|
||
if text != expected { | ||
t.Errorf("Expected %s, got %s", expected, text) | ||
} | ||
} | ||
|
||
func TestIncomingRequestHeadersText(t *testing.T) { | ||
logger := Logger{} | ||
headers := map[string][]string{ | ||
"hello": {"world", "foobar"}, | ||
"foo": {"bar"}, | ||
} | ||
exepectedHeaders := map[string]string{ | ||
"hello": "world,foobar", | ||
"foo": "bar", | ||
} | ||
expectedSortedKeys := []string{ | ||
"foo", "hello", | ||
} | ||
|
||
headersWithJoinedValues, keys := logger.incomingRequestHeaders(headers) | ||
|
||
if !reflect.DeepEqual(headersWithJoinedValues, exepectedHeaders) { | ||
t.Errorf("Expected %s, got %s", exepectedHeaders, headersWithJoinedValues) | ||
} | ||
|
||
if !reflect.DeepEqual(keys, expectedSortedKeys) { | ||
t.Errorf("Expected %s, got %s", expectedSortedKeys, keys) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters