Go中来自GCF的日志不包含日志级别

I am trying send info/error logs to StackDriver Logging on GCP from Cloud Function written in Go, however all logs dosen't have log level assignment.

I have created function from https://github.com/GoogleCloudPlatform/golang-samples/blob/master/functions/helloworld/hello_logging.go to demonstrate problem.

link to image with logs

Cloud Support here! What you are trying to do is not possible, as specified in the documentation:

  • Logs to stdout or stderr do not have an associated log level.

  • Internal system messages have the DEBUG log level.

What you probably need is to use Logging API, specifically the Log Levels section.

If this is not working for you either you could try using node.js instead of go as it currently emits logs to INFO and ERROR levels using console.log() and console.error() respectively.

you can use external library which allow you to set the log level. Here an example of what I use. Use logrus for setting the minimal log level (you can improve this code by providing log level in env var) and joonix for fluentd formatter.(line 25)

A point of attention. Line 11, I rename the logrus package log log "github.com/sirupsen/logrus" Thus, don't use log standard library, but logrus library. It's sometime boring... you can simply replace log by logrus for avoiding all confusion.

Joonix is a fluentD formatter for transforming the logs into an ingestable format for Stackdriver.