|
| 1 | +package renderer |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "sort" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/aaronvb/logrequest" |
| 11 | + "github.com/pterm/pterm" |
| 12 | +) |
| 13 | + |
| 14 | +// Logger outputs to a log file. |
| 15 | +type Logger struct { |
| 16 | + // File points to the file that we write to. |
| 17 | + File string |
| 18 | + |
| 19 | + // Fields for startText |
| 20 | + Port int |
| 21 | + Addr string |
| 22 | +} |
| 23 | + |
| 24 | +// 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) |
| 31 | + if err != nil { |
| 32 | + l.Fatal(err) |
| 33 | + } |
| 34 | + |
| 35 | + defer f.Close() |
| 36 | + |
| 37 | + str := fmt.Sprintf("%s: %s\n", time.Now().Format("2006/02/01 15:04:05"), l.startText()) |
| 38 | + f.WriteString(str) |
| 39 | +} |
| 40 | + |
| 41 | +func (l *Logger) startText() string { |
| 42 | + return fmt.Sprintf("Listening on http://%s:%d", l.Addr, l.Port) |
| 43 | +} |
| 44 | + |
| 45 | +// Fatal will use the Error prefix to render the error and then exit the CLI. |
| 46 | +func (l *Logger) Fatal(err error) { |
| 47 | + pterm.Error.WithShowLineNumber(false).Println(err) |
| 48 | + os.Exit(1) |
| 49 | +} |
| 50 | + |
| 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) |
| 60 | + } |
| 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) |
| 70 | +} |
| 71 | + |
| 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 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// incomingRequestHeaders takes the headers from the request, sorts them alphabetically, |
| 94 | +// joins the values, and creates a new map |
| 95 | +func (l *Logger) incomingRequestHeaders(headers map[string][]string) (map[string]string, []string) { |
| 96 | + headersWithJoinedValues := make(map[string]string) |
| 97 | + for key, val := range headers { |
| 98 | + value := strings.Join(val, ",") |
| 99 | + headersWithJoinedValues[key] = value |
| 100 | + } |
| 101 | + |
| 102 | + keys := make([]string, 0, len(headers)) |
| 103 | + for key := range headers { |
| 104 | + keys = append(keys, key) |
| 105 | + } |
| 106 | + |
| 107 | + sort.Strings(keys) |
| 108 | + |
| 109 | + return headersWithJoinedValues, keys |
| 110 | +} |
0 commit comments