Reputation: 2695
I find tw kind of go library for GAE:
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
Upvotes: 3
Views: 1581
Reputation: 1102
You could also go the structured logging route, where you don't depend on the above client libraries.
// Entry defines a log entry.
type Entry struct {
Message string `json:"message"`
Severity string `json:"severity,omitempty"`
Trace string `json:"logging.googleapis.com/trace,omitempty"`
// Cloud Log Viewer allows filtering and display of this as `jsonPayload.component`.
Component string `json:"component,omitempty"`
}
// String renders an entry structure to the JSON format expected by Cloud Logging.
func (e Entry) String() string {
if e.Severity == "" {
e.Severity = "INFO"
}
out, err := json.Marshal(e)
if err != nil {
log.Printf("json.Marshal: %v", err)
}
return string(out)
}
and then log using the built-in log
package:
log.Println(Entry{
Severity: "NOTICE",
Message: "This is the default display field.",
Component: "arbitrary-property",
Trace: trace,
})
Here is a full example: https://github.com/GoogleCloudPlatform/golang-samples/blob/master/run/logging-manual/main.go
Upvotes: 2
Reputation: 3192
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.
Upvotes: 5