slogr

package module
v0.0.0-...-1a55f62 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2024 License: MIT Imports: 19 Imported by: 4

README

slogr

slog.Handler that prints log records for Google Logging

Documentation

Index

Constants

View Source
const (
	NameKey      = "name"
	ErrorKey     = "error"
	LabelKey     = "labels"
	RequestKey   = "request"
	ResponseKey  = "response"
	OperationKey = "operation"
)

Variables

View Source
var LoggerKey = &ContextKey{
	name: reflect.TypeOf(ContextKey{}).PkgPath(),
}

LoggerKey represents the context key of the logger.

Functions

func Error

func Error(err error) slog.Attr

Error returns an error attribute

func FromContext

func FromContext(ctx context.Context) *slog.Logger

FromContext returns the logger from a given context.

func Label

func Label(attr ...any) slog.Attr

Label returns an Attr for a Group Label. The caller must not subsequently mutate the argument slice.

Use Label to collect several Attrs under a labels key on a log line.

func Name

func Name(value string) slog.Attr

Name returns an Attr for a log name. The caller must not subsequently mutate the argument slice.

Use Label to collect several Attrs under a name key on a log line.

func NewHandler

func NewHandler(w io.Writer, opts *HandlerOptions) slog.Handler

NewHandler creates a slog.Handler that writes tinted logs to w, using the default options.

func NewLogger

func NewLogger(w io.Writer, options *HandlerOptions) *slog.Logger

NewLogger crates a new logger instance.

func OperationContinue

func OperationContinue(id, producer string) slog.Attr

OperationContinue is a function for logging `Operation`. It should be called for any non-start/end operation log.

func OperationEnd

func OperationEnd(id, producer string) slog.Attr

OperationEnd is a function for logging `Operation`. It should be called for the last operation log.

func OperationStart

func OperationStart(id, producer string) slog.Attr

OperationStart is a function for logging `Operation`. It should be called for the first operation log.

func Request

func Request(r *http.Request, opts ...RequestOption) slog.Attr

Request returns an Attr for a http.Request. The caller must not subsequently mutate the argument slice.

Use Request to collect several Attrs under a HttpRequest key on a log line.

func Response

func Response(r *http.Response, opts ...RequestOption) slog.Attr

Respone returns an Attr for a http.Respone. The caller must not subsequently mutate the argument slice.

Use Response to collect several Attrs under a HttpRequest key on a log line.

func ResponseWriter

func ResponseWriter(r http.ResponseWriter, opts ...RequestOption) slog.Attr

ResponseWriter returns an Attr for a http.ResponseWriter. The caller must not subsequently mutate the argument slice.

Use Response to collect several Attrs under a HttpRequest key on a log line.

func WithContext

func WithContext(ctx context.Context, logger *slog.Logger) context.Context

WithContext provides the logger in a given context.

Types

type ContextKey

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

ContextKey represents a context key.

func (*ContextKey) String

func (k *ContextKey) String() string

String returns the context key as a string.

type Entry

type Entry loggingpb.LogEntry

An individual entry in a log.

func (*Entry) GetJsonPayload

func (x *Entry) GetJsonPayload() *structpb.Struct

GetJsonPayload returns the log entry payload, represented as a structure that is expressed as a JSON object.

func (*Entry) GetPayload

func (x *Entry) GetPayload() interface{}

GetPayload returns the underlying payload

func (*Entry) MarshalJSON

func (x *Entry) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

type Handler

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

Handler implements a slog.Handler.

func (*Handler) Enabled

func (h *Handler) Enabled(_ context.Context, level slog.Level) bool

Enabled implements slog.Handler

func (*Handler) Handle

func (h *Handler) Handle(ctx context.Context, r slog.Record) error

Handle implements slog.Handler

func (*Handler) WithAttrs

func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs implements slog.Handler

func (*Handler) WithGroup

func (h *Handler) WithGroup(name string) slog.Handler

WithGroup implements slog.Handler

type HandlerOptions

type HandlerOptions struct {
	// ProjectID is Google Cloud Project ID
	// If you want to use trace_id, you should set this or set GOOGLE_CLOUD_PROJECT environment.
	// Cloud Shell and App Engine set this environment variable to the project ID, so use it if present.
	ProjectID string

	// When AddIndent is true, the handler adds an ident to the JSON output.
	AddIndent bool

	// When AddSource is true, the handler adds a ("source", "file:line")
	// attribute to the output indicating the source code position of the log
	// statement. AddSource is false by default to skip the cost of computing
	// this information.
	AddSource bool

	// Level reports the minimum record level that will be logged.
	// The handler discards records with lower levels.
	// If Level is nil, the handler assumes LevelInfo.
	// The handler calls Level.Level for each record processed;
	// to adjust the minimum level dynamically, use a LevelVar.
	Level slog.Leveler
}

HandlerOptions for a slog.Handler that writes tinted logs. A zero HandlerOptions consists entirely of default values.

type LevelVar

type LevelVar string

StringLevel represents a slog.Leveler for string

func (LevelVar) Level

func (v LevelVar) Level() slog.Level

Level implements slog.Leveler.

func (*LevelVar) MarshalText

func (v *LevelVar) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler by calling [Level.MarshalText].

func (*LevelVar) Set

func (v *LevelVar) Set(value string)

Set set the value.

func (LevelVar) String

func (v LevelVar) String() string

String returns the level as string.

func (*LevelVar) UnmarshalText

func (v *LevelVar) UnmarshalText(data []byte) error

UnmarshalText implements encoding.TextUnmarshaler by calling [Level.UnmarshalText].

type RequestOption

type RequestOption interface {
	Apply(*ltype.HttpRequest)
}

RequestOption represents a request option

func WithLatency

func WithLatency(v time.Duration) RequestOption

WithLatency sets the latency to a given request.

type RequestOptionFunc

type RequestOptionFunc func(*ltype.HttpRequest)

RequestOptionFunc represents a request option function.

func (RequestOptionFunc) Apply

func (fn RequestOptionFunc) Apply(r *ltype.HttpRequest)

Apply the option.

Jump to

Keyboard shortcuts

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