Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which logging library should I use for Go in GAE?

I find tw kind of go library for GAE:

  1. "google.golang.org/appengine/log"
  2. "cloud.google.com/go/logging"

Which one should I use ? By the way I use both logging library in my app. In local dev mode , I can see log like this.

2019/01/08 06:57:34 INFO: Search keyword="test" idOnly=bool
2019/01/08 06:57:34 INFO: Search:"test"

But when I deploy to production GAE, I cannot see any log.

No matter using stackdriver by

resource.type="gae_app" resource.labels.module_id="default" 

Or gcloud command

 gcloud app logs tail -s default
like image 667
howie Avatar asked Nov 18 '25 08:11

howie


1 Answers

If you want the logs to appear in Stackdriver Logging, the correct approach would be to use the "google.golang.org/appengine/log" package.

However, as per the documentation on the Go1.11 runtime, it is recommended not to use the App Engine specific API's and use the Google Cloud client library.

In regards to logging, this means that instead of using "google.golang.org/appengine/log", the recommended approach is to use the "log" package instead. An example:

app.yaml

runtime: go111

hello.go

package main

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

func main() {
        http.HandleFunc("/", indexHandler)

        port := os.Getenv("PORT")
        if port == "" {
                port = "8080"
        }

        log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}

func indexHandler(w http.ResponseWriter, r *http.Request) {
        //Create the log and write it
        log.Printf("Hello world!")
        fmt.Fprint(w, "Log written in Stackdriver!")
}

This log will appear in Stackdriver Logging under:

resource.type="gae_app"
resource.labels.module_id="default"
logName="projects/<YOUR_PROJECT_NAME>/logs/stderr"

Or by selecting stderr in the log filter drop down list.

However, if you wish, you can still use the "google.golang.org/appengine/log" package, but you will have to add the "google.golang.org/appengine" package as well, and add the appengine.Main() entrypoint in the main() function.

like image 91
Joan Grau Noël Avatar answered Nov 21 '25 01:11

Joan Grau Noël



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!