Skip to content

Commit

Permalink
Add example GAE app
Browse files Browse the repository at this point in the history
  • Loading branch information
mtraver committed Apr 1, 2020
1 parent 930c97b commit 2b7692e
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
6 changes: 6 additions & 0 deletions example/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
runtime: go113

handlers:
- url: /.*
script: auto
secure: always
71 changes: 71 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"fmt"
"log"
"net/http"
"os"

"github.com/mtraver/gaelog"
)

// wrappedHandler must be wrapped using gaelog.Wrap or gaelog.WrapWithID so that the
// request context can be used with the package-level logging functions.
type wrappedHandler struct{}

func (h wrappedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}

ctx := r.Context()

gaelog.Debugf(ctx, "Debug")
gaelog.Infof(ctx, "Info")
gaelog.Noticef(ctx, "Notice")
gaelog.Warningf(ctx, "Warning")
gaelog.Errorf(ctx, "Error")
gaelog.Criticalf(ctx, "Critical")
gaelog.Alertf(ctx, "Alert")
gaelog.Emergencyf(ctx, "Emergency")

message := struct {
Places []string
}{
[]string{"Kings Canyon", "Sequoia", "Yosemite", "Death Valley"},
}

gaelog.Info(ctx, message)

fmt.Fprintf(w, "Hello!")
}

// manualHandler creates and closes a logger manually. This usage does not require
// gaelog.Wrap or gaelog.WrapWithID.
func manualHandler(w http.ResponseWriter, r *http.Request) {
lg, err := gaelog.New(r)
if err != nil {
// The returned logger is valid despite the error. It falls back to logging
// via the standard library's "log" package.
lg.Errorf("Failed to make logger: %v", err)
}
defer lg.Close()

lg.Warningf("Some important info right here, that's for sure")

fmt.Fprintf(w, "Hello!")
}

func main() {
// Wrap the handler.
http.Handle("/", gaelog.Wrap(wrappedHandler{}))

http.HandleFunc("/manual", manualHandler)

port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}

0 comments on commit 2b7692e

Please sign in to comment.