问题
I'm trying to get stackdriver logging working for a simple Go app running in Google Cloud Run (fully managed).
I've created simplest possible demo app based on "official" stackdriver golang example
Cloud Run docs states that no additional actions should be performed to write stackdriver logs
My service uses default service account
I'm using Go 1.13 to compile code (Dockerfile is copied from Cloud Run example "as-is")
I've tried to deploy it to different regions, with no success
When running container locally, using service account credentials, stackdriver log message does not appear in local terminal or stackdriver console
No matter what, on app start I see only "Before stackdriver logging"
followed by "After stackdriver logging"
with no other messages\errors in the middle
Here's part of logging code (for full source, Dockerfile and instructions to build and run use link above):
import (
"context"
"log"
"os"
...
"cloud.google.com/go/compute/metadata"
"cloud.google.com/go/logging"
)
func main() {
loggingClient, err := stackdriverClient()
...
log.Println("Before stackdriver logging")
logger.StandardLogger(logging.Info).Println("Stackdriver log")
if err = logger.Flush(); err != nil {
log.Fatalf("Failed to flush client: %v", err)
}
if err = loggingClient.Close(); err != nil {
log.Fatalf("Failed to close client: %v", err)
}
log.Println("After stackdriver logging")
...
}
func stackdriverClient() (client *logging.Client, err error) {
var projectID string
if projectID, err = metadata.ProjectID(); err == nil {
client, err = logging.NewClient(context.Background(), projectID)
}
return
}
回答1:
It turns out that log entries are written successfully
But default Cloud Run filter in logviewer web UI does not include them
Filter expression below worked for me to get all the logs:
resource.type = "project" OR resource.type = "cloud_run_revision"
(service name, location, severity omitted)
"stdout\stderr" log entries match resource.type="cloud_run_revision"
, while stackdriver log entries match resource.type="project"
Update: I've created a ticket for this in google tracker
来源:https://stackoverflow.com/questions/58900506/how-to-use-stackdriver-logging-on-cloud-run