github.com/blend/go-sdk@v1.20220411.3/examples/logger/json_logs/main.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package main 9 10 import ( 11 "fmt" 12 "log" 13 "net/http" 14 _ "net/http/pprof" 15 "os" 16 "time" 17 18 "github.com/blend/go-sdk/logger" 19 "github.com/blend/go-sdk/webutil" 20 ) 21 22 func createRequestEvent(req *http.Request, rw webutil.ResponseWriter, start time.Time) webutil.HTTPRequestEvent { 23 return webutil.NewHTTPRequestEvent(req, 24 webutil.OptHTTPRequestStatusCode(rw.StatusCode()), 25 webutil.OptHTTPRequestContentLength(rw.ContentLength()), 26 webutil.OptHTTPRequestElapsed(time.Since(start)), 27 ) 28 } 29 30 func logged(log logger.Log, handler http.HandlerFunc) http.HandlerFunc { 31 return func(res http.ResponseWriter, req *http.Request) { 32 start := time.Now() 33 rw := webutil.NewStatusResponseWriter(res) 34 handler(rw, req) 35 log.TriggerContext(req.Context(), createRequestEvent(req, rw, start)) 36 } 37 } 38 39 func stdoutLogged(handler http.HandlerFunc) http.HandlerFunc { 40 return func(res http.ResponseWriter, req *http.Request) { 41 start := time.Now() 42 handler(res, req) 43 fmt.Printf("%s %s %s %s %s %s %s\n", 44 time.Now().UTC().Format(time.RFC3339), 45 "web.request", 46 req.Method, 47 req.URL.Path, 48 "200", 49 time.Since(start).String(), 50 "??", 51 ) 52 } 53 } 54 55 func indexHandler(res http.ResponseWriter, req *http.Request) { 56 res.WriteHeader(http.StatusOK) 57 res.Write([]byte(`{"status":"ok!"}`)) 58 } 59 60 func fatalErrorHandler(res http.ResponseWriter, req *http.Request) { 61 res.WriteHeader(http.StatusInternalServerError) 62 res.Write([]byte(`{"status":"not ok."}`)) 63 } 64 65 func errorHandler(res http.ResponseWriter, req *http.Request) { 66 res.WriteHeader(http.StatusInternalServerError) 67 res.Write([]byte(`{"status":"not ok."}`)) 68 } 69 70 func warningHandler(res http.ResponseWriter, req *http.Request) { 71 res.WriteHeader(http.StatusBadRequest) 72 res.Write([]byte(`{"status":"not ok."}`)) 73 } 74 75 func subContextHandler(res http.ResponseWriter, req *http.Request) { 76 res.WriteHeader(http.StatusOK) 77 res.Write([]byte(`{"status":"did sub-context things"}`)) 78 } 79 80 func auditHandler(res http.ResponseWriter, req *http.Request) { 81 res.WriteHeader(http.StatusOK) 82 res.Write([]byte(`{"status":"audit logged!"}`)) 83 } 84 85 func port() string { 86 envPort := os.Getenv("PORT") 87 if len(envPort) > 0 { 88 return envPort 89 } 90 return "8888" 91 } 92 93 func main() { 94 go func() { 95 log.Println(http.ListenAndServe("localhost:6060", nil)) 96 }() 97 98 log := logger.Prod(logger.OptJSON()) 99 100 http.HandleFunc("/", logged(log, indexHandler)) 101 102 http.HandleFunc("/sub-context", logged(log, subContextHandler)) 103 http.HandleFunc("/fatalerror", logged(log, fatalErrorHandler)) 104 http.HandleFunc("/error", logged(log, errorHandler)) 105 http.HandleFunc("/warning", logged(log, warningHandler)) 106 http.HandleFunc("/audit", logged(log, auditHandler)) 107 108 http.HandleFunc("/bench/logged", logged(log, indexHandler)) 109 http.HandleFunc("/bench/stdout", stdoutLogged(indexHandler)) 110 111 log.Infof("Listening on :%s", port()) 112 log.Infof("Events %s", log.Flags.String()) 113 log.Fatal(http.ListenAndServe(":"+port(), nil)) 114 }