gaelog

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 6, 2019 License: Apache-2.0 Imports: 7 Imported by: 5

README

Easy Stackdriver Logging on Google App Engine Standard second generation runtimes

GoDoc Go Report Card

Using Stackdriver Logging on App Engine Standard is complicated. It doesn't have to be that way.

package main

import (
  "fmt"
  "log"
  "net/http"
  "os"

  "github.com/mtraver/gaelog"
)

func index(w http.ResponseWriter, r *http.Request) {
  lg, err := gaelog.New(r)
  if err != nil {
    // The returned logger is valid despite the error. It falls back to logging
    // via the standard library's "log" package.
    lg.Errorf("Failed to make logger: %v", err)
  }
  defer lg.Close()

  lg.Debugf("Debug")
  lg.Infof("Info")
  lg.Noticef("Notice")
  lg.Warningf("Warning")
  lg.Errorf("Error")
  lg.Criticalf("Critical")
  lg.Alertf("Alert")
  lg.Emergencyf("Emergency")

  message := struct {
    Places []string
  }{
    []string{"Kings Canyon", "Sequoia", "Yosemite", "Death Valley"},
  }

  lg.Info(message)
}

func main() {
  http.HandleFunc("/", index)

  port := os.Getenv("PORT")
  if port == "" {
    port = "8080"
  }
  log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}

Screenshot of logs in Stackdriver UI

Documentation

Overview

Package gaelog provides easy Stackdriver Logging on Google App Engine Standard second generation runtimes.

Index

Constants

View Source
const (
	// DefaultLogID is the default log ID of the underlying Stackdriver Logging logger. Request
	// logs are logged under the ID "request_log", so use "app_log" for consistency. To use a
	// different ID create your logger with NewWithID.
	DefaultLogID = "app_log"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

A Logger logs messages to Stackdriver Logging (though in certain cases it may fall back to the standard library's "log" package; see New). Logs will be correlated with requests in Stackdriver.

func New

func New(r *http.Request, options ...logging.LoggerOption) (*Logger, error)

New is identical to NewWithID with the exception that it uses the default log ID.

func NewWithID

func NewWithID(r *http.Request, logID string, options ...logging.LoggerOption) (*Logger, error)

NewWithID creates a new Logger. The Logger is initialized using environment variables that are present on App Engine:

  • GOOGLE_CLOUD_PROJECT
  • GAE_SERVICE
  • GAE_VERSION

The given log ID will be passed through to the underlying Stackdriver Logging logger.

Additionally, options (of type LoggerOption, from cloud.google.com/go/logging) will be passed through to the underlying Stackdriver Logging logger. Note that the option CommonResource will have no effect because the MonitoredResource is set when each log entry is made, thus overriding any value set with CommonResource. This is intended: much of the value of this package is in setting up the MonitoredResource so that log entries correlate with requests.

The Logger will be valid in all cases, even when the error is non-nil. In the case of a non-nil error the Logger will fall back to the standard library's "log" package. There are three cases in which the error will be non-nil:

  1. Any of the aforementioned environment variables are not set.
  2. The given http.Request does not have the X-Cloud-Trace-Context header.
  3. Initialization of the underlying Stackdriver Logging client produced an error.

func (*Logger) Alert

func (lg *Logger) Alert(v interface{})

Alert calls Log with alert severity.

func (*Logger) Alertf

func (lg *Logger) Alertf(format string, v ...interface{})

Alertf calls Logf with alert severity.

func (*Logger) Close

func (lg *Logger) Close() error

Close closes the Logger, ensuring all logs are flushed and closing the underlying Stackdriver Logging client.

func (*Logger) Critical

func (lg *Logger) Critical(v interface{})

Critical calls Log with critical severity.

func (*Logger) Criticalf

func (lg *Logger) Criticalf(format string, v ...interface{})

Criticalf calls Logf with critical severity.

func (*Logger) Debug

func (lg *Logger) Debug(v interface{})

Debug calls Log with debug severity.

func (*Logger) Debugf

func (lg *Logger) Debugf(format string, v ...interface{})

Debugf calls Logf with debug severity.

func (*Logger) Emergency

func (lg *Logger) Emergency(v interface{})

Emergency calls Log with emergency severity.

func (*Logger) Emergencyf

func (lg *Logger) Emergencyf(format string, v ...interface{})

Emergencyf calls Logf with emergency severity.

func (*Logger) Error

func (lg *Logger) Error(v interface{})

Error calls Log with error severity.

func (*Logger) Errorf

func (lg *Logger) Errorf(format string, v ...interface{})

Errorf calls Logf with error severity.

func (*Logger) Info

func (lg *Logger) Info(v interface{})

Info calls Log with info severity.

func (*Logger) Infof

func (lg *Logger) Infof(format string, v ...interface{})

Infof calls Logf with info severity.

func (*Logger) Log

func (lg *Logger) Log(severity logging.Severity, v interface{})

Log logs with the given severity. v must be either a string, or something that marshals via the encoding/json package to a JSON object (and not any other type of JSON value).

func (*Logger) Logf

func (lg *Logger) Logf(severity logging.Severity, format string, v ...interface{})

Logf logs with the given severity. Remaining arguments are handled in the manner of fmt.Printf.

func (*Logger) Notice

func (lg *Logger) Notice(v interface{})

Notice calls Log with notice severity.

func (*Logger) Noticef

func (lg *Logger) Noticef(format string, v ...interface{})

Noticef calls Logf with notice severity.

func (*Logger) Warning

func (lg *Logger) Warning(v interface{})

Warning calls Log with warning severity.

func (*Logger) Warningf

func (lg *Logger) Warningf(format string, v ...interface{})

Warningf calls Logf with warning severity.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳