Super simple structured logging mechanism for Go projects with Stackdriver format compatibility
go get -u github.com/macuenca/loggerpackage main
import (
"github.com/macuenca/logger"
)
// There should be a LOG_LEVEL environment variable set, which is read by the library// If no value is set, the default LOG_LEVEL will be INFOfuncmain() {
// Stackdriver requires a project name and version to be set. Use your environment for these values.// SERVICE should be your GCP project-id, e.g. robokiller-146813// VERSION is an arbitrary valuelog:=log.New()
// You can also initialize the logger with a context, the values will persisted throughout the scope of the logger instancelog:=log.New().With(log.Fields{
"user": "+1234567890",
"action": "create-account",
})
// A metric is an INFO log entry without a payloadlog.Metric("CUSTOM_METRIC_ENTRY")
// Log a DEBUG message, only visible in when LOG_LEVEL is set to DEBUGlog.With(log.Fields{"key":"val"}).Debug("debug message goes here")
log.With(log.Fields{"key":"val"}).Debugf("debug message with %s", param)
// Log an INFO messagelog.With(log.Fields{"key":"val"}).Info("info message goes here")
log.With(log.Fields{"key":"val"}).Infof("info message with %s", param)
// Log a WARN messagelog.With(log.Fields{"key":"val"}).Warn("warn message goes here")
log.With(log.Fields{"key":"val"}).Warnf("warn message with %s", param)
// Error() prints the stacktrace as part of the payload for each entry and sends the// data to Stackdriver Error Reporting servicelog.With(log.Fields{"key":"val"}).Error("error message goes here")
log.With(log.Fields{"key":"val"}).Errorf("error message with %s", param)
}