-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.go
More file actions
88 lines (69 loc) · 1.82 KB
/
logger.go
File metadata and controls
88 lines (69 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"fmt"
"strings"
)
type Color string
const (
Reset Color = "\033[0m"
Red Color = "\033[31m"
Green Color = "\033[32m"
Yellow Color = "\033[33m"
Blue Color = "\033[34m"
)
const tableWidth int = 100
type Logger struct {
}
func NewLogger() Logger {
return Logger{}
}
func (l *Logger) Sep() {
fmt.Println(strings.Repeat("-", tableWidth))
}
func (l *Logger) PrintHeading() {
l.Sep()
fmt.Println("| Method | Uri |")
l.Sep()
}
func (l *Logger) Col(content string, length int, color Color) string {
paddingLeft := 0
paddingRight := 0
if len(content) < length {
paddingNeeded := length - len(content)
// standard padding
paddingLeft++
paddingNeeded--
paddingRight += paddingNeeded
paddingNeeded = 0
}
builder := strings.Builder{}
builder.WriteString(string(color))
builder.WriteString(strings.Repeat(" ", paddingLeft))
builder.WriteString(content)
builder.WriteString(strings.Repeat(" ", paddingRight))
builder.WriteString(string(Reset))
return builder.String()
}
func (l *Logger) PrintRequest(method, uri string) {
color := Reset
switch (method) {
case "GET": color = Green
case "POST": color = Blue
case "PUT": color = Yellow
case "PATCH": color = Red
}
methodColumnWidth := 8
colCount := 2
builder := strings.Builder{}
builder.WriteString("|")
builder.WriteString(l.Col(method, methodColumnWidth, color))
builder.WriteString("|")
builder.WriteString(l.Col(uri, tableWidth - methodColumnWidth - (colCount + 1), Reset))
builder.WriteString("|")
fmt.Println(builder.String())
l.Sep()
builder.Reset()
builder.WriteString("|")
builder.WriteString("|")
fmt.Println(builder.String())
}