File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments