Skip to content

Commit a434c26

Browse files
committed
add: example for WebHook etc
1 parent 65f25f0 commit a434c26

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

example/with_func/main.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
7+
log "github.com/gcp-kit/stalog"
8+
)
9+
10+
func main() {
11+
mux := http.NewServeMux()
12+
13+
projectId := "my-gcp-project"
14+
15+
// Make config for this library
16+
config := log.NewConfig(projectId)
17+
config.AdditionalData = log.AdditionalData{ // set additional fields for all logs
18+
"service": "foo",
19+
"version": 1.0,
20+
}
21+
22+
// Set request handler
23+
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
24+
// Get request context logger
25+
log.RequestLoggingWithFunc(config, w, r, index)
26+
})
27+
28+
// Run server
29+
fmt.Println("Waiting requests on port 8080...")
30+
if err := http.ListenAndServe(":8080", mux); err != nil {
31+
panic(err)
32+
}
33+
}
34+
35+
func index(w http.ResponseWriter, r *http.Request) {
36+
logger := log.RequestContextLogger(r)
37+
38+
// These logs are grouped with the request log
39+
logger.Debugf("Hi")
40+
logger.Infof("Hello")
41+
logger.Warnf("World")
42+
43+
_, _ = fmt.Fprintf(w, "OK\n")
44+
}

0 commit comments

Comments
 (0)