slog

package
v0.12.1 Latest Latest
Warning

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

Go to latest
Published: May 26, 2022 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package slog implements a logger formatted to work with Stackdriver structured logs.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Alert

func Alert(v ...interface{})

Alert sends a message to the default logger with severity Alert. Arguments are handled in the manner of fmt.Print.

func Alertf

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

Alertf sends a message to the default logger with severity Alert. Arguments are handled in the manner of fmt.Printf.

func Critical

func Critical(v ...interface{})

Critical sends a message to the default logger with severity Critical. Arguments are handled in the manner of fmt.Print.

func Criticalf

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

Criticalf sends a message to the default logger with severity Critical. Arguments are handled in the manner of fmt.Printf.

func Debug

func Debug(v ...interface{})

Debug sends a message to the default logger with severity Debug. Arguments are handled in the manner of fmt.Print.

func Debugf

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

Debugf sends a message to the default logger with severity Debug. Arguments are handled in the manner of fmt.Printf.

func Emergency

func Emergency(v ...interface{})

Emergency sends a message to the default logger with severity Emergency. Arguments are handled in the manner of fmt.Print.

func Emergencyf

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

Emergencyf sends a message to the default logger with severity Emergency. Arguments are handled in the manner of fmt.Printf.

func Error

func Error(v ...interface{})

Error sends a message to the default logger with severity Error. Arguments are handled in the manner of fmt.Print.

func Errorf

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

Errorf sends a message to the default logger with severity Error. Arguments are handled in the manner of fmt.Printf.

func Info

func Info(v ...interface{})

Info sends a message to the default logger with severity Info. Arguments are handled in the manner of fmt.Print.

func Infof

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

Infof sends a message to the default logger with severity Info. Arguments are handled in the manner of fmt.Printf.

func NewContext added in v0.5.0

func NewContext(ctx context.Context, entry *Entry) context.Context

NewContext returns a new Context that carries an entry.

func Notice

func Notice(v ...interface{})

Notice sends a message to the default logger with severity Notice. Arguments are handled in the manner of fmt.Print.

func Noticef

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

Noticef sends a message to the default logger with severity Notice. Arguments are handled in the manner of fmt.Printf.

func SetIncludeSources added in v0.2.0

func SetIncludeSources(include bool)

SetIncludeSources for the package-level logger. Will include file, line and func.

func SetOutput

func SetOutput(w io.Writer)

SetOutput destination for the package-level logger.

func SetProject

func SetProject(project string)

SetProject for the package-level logger. Used for things such as traces that require project to be included.

func Warn

func Warn(v ...interface{})

Warn sends a message to the default logger with severity Warn. Arguments are handled in the manner of fmt.Print.

func Warnf

func Warnf(format string, v ...interface{})

Warnf sends a message to the default logger with severity Warn. Arguments are handled in the manner of fmt.Printf.

Types

type Entry

type Entry struct {
	Message        string            `json:"message"`
	Severity       severity          `json:"severity,omitempty"`
	Labels         map[string]string `json:"logging.googleapis.com/labels,omitempty"`
	SourceLocation *SourceLocation   `json:"logging.googleapis.com/sourceLocation,omitempty"`
	Operation      *Operation        `json:"logging.googleapis.com/operation,omitempty"`
	Trace          string            `json:"logging.googleapis.com/trace,omitempty"`
	SpanID         string            `json:"logging.googleapis.com/spanId,omitempty"`
	TraceSampled   bool              `json:"logging.googleapis.com/trace_sampled,omitempty"`
	Details        Fields            `json:"details,omitempty"`
	Err            string            `json:"error,omitempty"`
	StackTrace     string            `json:"exception,omitempty"`
	// contains filtered or unexported fields
}

Entry with additional metadata included. See https://cloud.google.com/logging/docs/agent/configuration#special-fields for reference.

func FromContext added in v0.5.0

func FromContext(ctx context.Context) *Entry

FromContext returns the Entry value stored in ctx, or a new Entry if none exists.

Example
entry := &Entry{}
ctx := NewContext(context.Background(), entry)

entry = FromContext(ctx)
entry.Info("entry pulled from context")
Output:

func StartOperation

func StartOperation(id, producer string) *Entry

StartOperation with a given ID and producer. Will log the start of the operation at Notice level.

Example
id := "operationId"
producer := "producerName"
entry := StartOperation(id, producer)
entry.Info("entry logged under new operation")

entry.WithOperation(id, producer)
entry.Info("entry logged under existing operation")

entry.EndOperation()
Output:

func WithDetail added in v0.6.0

func WithDetail(k string, v interface{}) *Entry

WithDetail for a given Entry. Will create a child entry.

func WithDetails added in v0.3.0

func WithDetails(details Fields) *Entry

WithDetails for a given Entry. Will create a child entry.

Example
entry := WithDetail("key", "value")
entry.Info("entry with single detail")

details := Fields{
	"detailOne": "one",
	"detailTwo": 2,
}
entry = entry.WithDetails(details)
entry.Info("entry with details")
Output:

func WithError added in v0.6.0

func WithError(err error) *Entry

WithError for a given Entry. Will create a child entry.

Example
err := errors.New("error!")
entry := WithError(err)
entry.Error("entry with error")
Output:

func WithLabels added in v0.2.0

func WithLabels(labels Fields) *Entry

WithLabels for a given Entry. Will create a child entry.

Example
labels := Fields{
	"labelOne": "hello",
	"labelTwo": "world",
}
entry := WithLabels(labels)
entry.Info("entry with labels")
Output:

func WithOperation

func WithOperation(id, producer string) *Entry

WithOperation details included in all logs written for a given Entry.

func WithSpan

func WithSpan(sc trace.SpanContext) *Entry

WithSpan details included for a given Trace. Will create a child entry.

Example
_, span := trace.StartSpan(context.Background(), "spanName")
entry := WithSpan(span.SpanContext())
entry.Info("entry with span")
Output:

func WithStack added in v0.12.1

func WithStack() *Entry

WithStack included. Will create a child entry.

func (*Entry) Alert

func (e *Entry) Alert(v ...interface{})

Alert sends a message to the logger associated with this entry with severity Alert. Arguments are handled in the manner of fmt.Print.

func (*Entry) Alertf

func (e *Entry) Alertf(format string, v ...interface{})

Alertf sends a message to the logger associated with this entry with severity Alert. Arguments are handled in the manner of fmt.Printf.

func (*Entry) Critical

func (e *Entry) Critical(v ...interface{})

Critical sends a message to the logger associated with this entry with severity Critical. Arguments are handled in the manner of fmt.Print.

func (*Entry) Criticalf

func (e *Entry) Criticalf(format string, v ...interface{})

Criticalf sends a message to the logger associated with this entry with severity Critical. Arguments are handled in the manner of fmt.Printf.

func (*Entry) Debug

func (e *Entry) Debug(v ...interface{})

Debug sends a message to the logger associated with this entry with severity Debug. Arguments are handled in the manner of fmt.Print.

func (*Entry) Debugf

func (e *Entry) Debugf(format string, v ...interface{})

Debugf sends a message to the logger associated with this entry with severity Debug. Arguments are handled in the manner of fmt.Printf.

func (*Entry) Emergency

func (e *Entry) Emergency(v ...interface{})

Emergency sends a message to the logger associated with this entry with severity Emergency. Arguments are handled in the manner of fmt.Print.

func (*Entry) Emergencyf

func (e *Entry) Emergencyf(format string, v ...interface{})

Emergencyf sends a message to the logger associated with this entry with severity Emergency. Arguments are handled in the manner of fmt.Printf.

func (*Entry) EndOperation

func (e *Entry) EndOperation()

EndOperation stops any current operation and further logs will no longer include. Will log the end of the operation at Notice level.

func (*Entry) Error

func (e *Entry) Error(v ...interface{})

Error sends a message to the logger associated with this entry with severity Error. Arguments are handled in the manner of fmt.Print.

func (*Entry) Errorf

func (e *Entry) Errorf(format string, v ...interface{})

Errorf sends a message to the logger associated with this entry with severity Error. Arguments are handled in the manner of fmt.Printf.

func (*Entry) Info

func (e *Entry) Info(v ...interface{})

Info sends a message to the logger associated with this entry with severity Info. Arguments are handled in the manner of fmt.Print.

func (*Entry) Infof

func (e *Entry) Infof(format string, v ...interface{})

Infof sends a message to the logger associated with this entry with severity Info. Arguments are handled in the manner of fmt.Printf.

func (*Entry) Notice

func (e *Entry) Notice(v ...interface{})

Notice sends a message to the logger associated with this entry with severity Notice. Arguments are handled in the manner of fmt.Print.

func (*Entry) Noticef

func (e *Entry) Noticef(format string, v ...interface{})

Noticef sends a message to the logger associated with this entry with severity Notice. Arguments are handled in the manner of fmt.Printf.

func (*Entry) StartOperation

func (e *Entry) StartOperation(id, producer string) *Entry

StartOperation with a given ID and producer. Will log the start of the operation at Notice level.

func (*Entry) Warn

func (e *Entry) Warn(v ...interface{})

Warn sends a message to the logger associated with this entry with severity Warn. Arguments are handled in the manner of fmt.Print.

func (*Entry) Warnf

func (e *Entry) Warnf(format string, v ...interface{})

Warnf sends a message to the logger associated with this entry with severity Warn. Arguments are handled in the manner of fmt.Printf.

func (*Entry) WithDetail added in v0.6.0

func (e *Entry) WithDetail(k string, v interface{}) *Entry

WithDetail for a given Entry. Will create a child entry.

func (*Entry) WithDetails added in v0.3.0

func (e *Entry) WithDetails(details Fields) *Entry

WithDetails for a given Entry. Will create a child entry.

func (*Entry) WithError added in v0.6.0

func (e *Entry) WithError(err error) *Entry

WithError for a given Entry. Will create a child entry.

func (*Entry) WithLabels added in v0.2.0

func (e *Entry) WithLabels(labels Fields) *Entry

WithLabels for a given Entry. Will create a child entry.

func (*Entry) WithOperation

func (e *Entry) WithOperation(id, producer string) *Entry

WithOperation details included in all logs written for a given Entry.

func (*Entry) WithSpan

func (e *Entry) WithSpan(sc trace.SpanContext) *Entry

WithSpan details included for a given Trace. Will create a child entry.

func (*Entry) WithStack added in v0.12.1

func (e *Entry) WithStack() *Entry

WithStack included. Will create a child entry.

type Fields added in v0.6.0

type Fields map[string]interface{}

type Logger

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

Logger used to write structured logs in a thread-safe manner to a given output.

func (*Logger) Alert

func (l *Logger) Alert(v ...interface{})

Alert sends a message to the logger with severity Alert. Arguments are handled in the manner of fmt.Print.

func (*Logger) Alertf

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

Alertf sends a message to the logger with severity Alert. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Critical

func (l *Logger) Critical(v ...interface{})

Critical sends a message to the logger with severity Critical. Arguments are handled in the manner of fmt.Print.

func (*Logger) Criticalf

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

Criticalf sends a message to the logger with severity Critical. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Debug

func (l *Logger) Debug(v ...interface{})

Debug sends a message to the logger with severity Debug. Arguments are handled in the manner of fmt.Print.

func (*Logger) Debugf

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

Debugf sends a message to the logger with severity Debug. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Emergency

func (l *Logger) Emergency(v ...interface{})

Emergency sends a message to the logger with severity Emergency. Arguments are handled in the manner of fmt.Print.

func (*Logger) Emergencyf

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

Emergencyf sends a message to the logger with severity Emergency. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Error

func (l *Logger) Error(v ...interface{})

Error sends a message to the logger with severity Error. Arguments are handled in the manner of fmt.Print.

func (*Logger) Errorf

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

Errorf sends a message to the logger with severity Error. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Info

func (l *Logger) Info(v ...interface{})

Info sends a message to the logger with severity Info. Arguments are handled in the manner of fmt.Print.

func (*Logger) Infof

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

Infof sends a message to the logger with severity Info. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Notice

func (l *Logger) Notice(v ...interface{})

Notice sends a message to the logger with severity Notice. Arguments are handled in the manner of fmt.Print.

func (*Logger) Noticef

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

Noticef sends a message to the logger with severity Notice. Arguments are handled in the manner of fmt.Printf.

func (*Logger) SetIncludeSources added in v0.2.0

func (l *Logger) SetIncludeSources(include bool)

SetIncludeSources for the logger. Will include file, line and func.

func (*Logger) SetOutput

func (l *Logger) SetOutput(w io.Writer)

SetOutput destination for the logger.

func (*Logger) SetProject

func (l *Logger) SetProject(project string)

SetProject for the logger. Used for things such as traces that require project to be included.

func (*Logger) StartOperation

func (l *Logger) StartOperation(id, producer string) *Entry

StartOperation with a given ID and producer. Will log the start of the operation at Notice level.

func (*Logger) Warn

func (l *Logger) Warn(v ...interface{})

Warn sends a message to the logger with severity Warn. Arguments are handled in the manner of fmt.Print.

func (*Logger) Warnf

func (l *Logger) Warnf(format string, v ...interface{})

Warnf sends a message to the logger with severity Warn. Arguments are handled in the manner of fmt.Printf.

func (*Logger) WithDetail added in v0.6.0

func (l *Logger) WithDetail(k string, v interface{}) *Entry

WithDetail for a given Entry. Will create a child entry.

func (*Logger) WithDetails added in v0.3.0

func (l *Logger) WithDetails(details Fields) *Entry

WithDetails for a given Entry. Will create a child entry.

func (*Logger) WithError added in v0.6.0

func (l *Logger) WithError(err error) *Entry

WithError for a given Entry. Will create a child entry.

func (*Logger) WithLabels added in v0.2.0

func (l *Logger) WithLabels(labels Fields) *Entry

WithLabels for a given Entry. Will create a child entry.

func (*Logger) WithOperation

func (l *Logger) WithOperation(id, producer string) *Entry

WithOperation details included in all logs written for a given Entry.

func (*Logger) WithSpan

func (l *Logger) WithSpan(sc trace.SpanContext) *Entry

WithSpan details included for a given Trace. Will create a child entry.

func (*Logger) WithStack added in v0.12.1

func (l *Logger) WithStack() *Entry

WithStack included. Will create a child entry.

type Operation

type Operation struct {
	ID       string `json:"id,omitempty"`
	Producer string `json:"producer,omitempty"`
	First    bool   `json:"first,omitempty"`
	Last     bool   `json:"last,omitempty"`
}

The Operation a given log entry is part of.

type SourceLocation

type SourceLocation struct {
	File     string `json:"file,omitempty"`
	Line     string `json:"line,omitempty"`
	Function string `json:"function,omitempty"`
}

SourceLocation that originated the log call.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL