-
Notifications
You must be signed in to change notification settings - Fork 34
/
main.go
121 lines (101 loc) · 2.77 KB
/
main.go
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/logdyhq/logdy-core/logdy"
)
func main() {
switch os.Args[1] {
case "with-webserver":
// go run main.go with-webserver
exampleWithWebserver()
case "with-serve-mux":
// go run main.go with-serve-mux
exampleWithServeMux()
case "without-webserver":
// go run main.go without-webserver
exampleWithoutWebserver()
}
}
func exampleWithoutWebserver() {
logdyLogger := logdy.InitializeLogdy(logdy.Config{
ServerIp: "127.0.0.1",
ServerPort: "8080",
UiPass: "foobar12345",
HttpPathPrefix: "_logdy-ui",
LogLevel: logdy.LOG_LEVEL_NORMAL,
LogInterceptor: func(entry *logdy.LogEntry) {
log.Println("Logdy internal log message intercepted", entry.Message, entry.Data, entry.Time)
},
}, nil)
go func() {
for {
logdyLogger.Log(logdy.Fields{
"foo": "bar",
"time": time.Now(),
})
logdyLogger.LogString("This is just a string " + time.Now().String())
time.Sleep(1000 * time.Millisecond)
}
}()
<-context.Background().Done()
}
func exampleWithWebserver() {
var logger logdy.Logdy
http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
logger.Log(logdy.Fields{
"url": r.URL.String(),
"ua": r.Header.Get("user-agent"),
})
fmt.Fprintf(w, "Hello world!")
})
logger = logdy.InitializeLogdy(logdy.Config{
HttpPathPrefix: "/_logdy-ui",
LogLevel: logdy.LOG_LEVEL_NORMAL,
}, nil)
log.Fatal(http.ListenAndServe(":8080", nil))
}
// Logger is a middleware handler that does request logging
type Logger struct {
logdy logdy.Logdy
handler http.Handler
}
// ServeHTTP handles the request by passing it to the real
// handler and logging the request details
func (l *Logger) ServeHTTP(w http.ResponseWriter, r *http.Request) {
start := time.Now()
l.handler.ServeHTTP(w, r)
// If this is a request to Logdy backend, ignore it
if strings.HasPrefix(r.URL.Path, l.logdy.Config().HttpPathPrefix) {
return
}
l.logdy.Log(logdy.Fields{
"ua": r.Header.Get("user-agent"),
"method": r.Method,
"path": r.URL.Path,
"query": r.URL.RawQuery,
"time": time.Since(start),
})
}
func exampleWithServeMux() {
mux := http.NewServeMux()
mux.HandleFunc("/v1/hello", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, World!"))
})
mux.HandleFunc("/v1/time", func(w http.ResponseWriter, r *http.Request) {
curTime := time.Now().Format(time.Kitchen)
w.Write([]byte(fmt.Sprintf("the current time is %v", curTime)))
})
logger := logdy.InitializeLogdy(logdy.Config{
HttpPathPrefix: "/_logdy-ui",
LogLevel: logdy.LOG_LEVEL_NORMAL,
}, mux)
addr := ":8082"
log.Printf("server is listening at %s", addr)
log.Fatal(http.ListenAndServe(addr, &Logger{logdy: logger, handler: mux}))
}