nephe
nephe

Reputation: 21

Logs from GCF in Go doesn't contains log level

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

Upvotes: 2

Views: 2727

Answers (5)

Nuno Cruces
Nuno Cruces

Reputation: 1743

I created a package to do exactly this: github.com/ncruces/go-gcp/glog

It works on App Engine, Kubernetes Engine, Cloud Run, and Cloud Functions. Supports setting logging levels, request/tracing metadata, and structured logging.

Usage:

func HelloWorld(w http.ResponseWriter, r *http.Request) {
    glog.Infoln("Hello logs")
    glog.Errorln("Hello logs")
    // or, to set request metadata:
    logger := glog.ForRequest(r)
    logger.Infoln("Hello logs")
}

Upvotes: 2

kubanczyk
kubanczyk

Reputation: 5931

I recommend the https://github.com/apsystole/log. It's also log- and logrus-compatible, but a small zero-dependency module unlike the libraries used in two existing answers which bring 400+ modules as their dependencies (gasp... I am simply looking at go mod graph).

Upvotes: 0

Douglas Manley
Douglas Manley

Reputation: 1332

I, too, took a stab at creating a package for this. If you use logrus (https://github.com/sirupsen/logrus), then this may be helpful:

https://github.com/tekkamanendless/gcfhook

I'm using this in production, and it's working just fine.

Upvotes: 1

Miguel
Miguel

Reputation: 996

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.

Upvotes: 2

guillaume blaquiere
guillaume blaquiere

Reputation: 75715

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.

Upvotes: 1

Related Questions