log

package
v0.6.16 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2023 License: Apache-2.0 Imports: 9 Imported by: 12

README

log

This package provides logging for the Registry project. See https://pkg.go.dev/github.com/apigee/registry/pkg/log for package documentation and examples.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(ctx context.Context, msg string)

func Debugf

func Debugf(ctx context.Context, msg string, v ...interface{})

func Error

func Error(ctx context.Context, msg string)

func Errorf

func Errorf(ctx context.Context, msg string, v ...interface{})

func Fatal

func Fatal(ctx context.Context, msg string)

func Fatalf

func Fatalf(ctx context.Context, msg string, v ...interface{})

func Info

func Info(ctx context.Context, msg string)

func Infof

func Infof(ctx context.Context, msg string, v ...interface{})

func NewContext

func NewContext(ctx context.Context, logger Logger) context.Context

NewContext returns a new context that can be used to retrieve the provided logger. This can be used to share a logger instance as the context passes through a single process.

func NewOutboundContext

func NewOutboundContext(ctx context.Context, md Metadata) context.Context

NewOutboundContext returns a new context with the provided metadata attached. This can be used to preserve the provided fields across gRPC calls.

Example
package main

import (
	"bytes"
	"context"
	"encoding/json"
	"fmt"

	"github.com/apigee/registry/pkg/log"
	"google.golang.org/grpc/metadata"
)

func main() {
	ctx := log.NewOutboundContext(context.Background(), log.Metadata{
		UID: "test_uid",
	})

	var (
		// Buffer to hold logs.
		buf bytes.Buffer
		// Logger that writes JSON entries to a buffer.
		logger = log.NewLogger(log.JSONFormat(&buf))
		// Struct to hold the parsed log entry.
		entry struct {
			Fields map[string]interface{} `json:"fields"`
		}
	)

	// "Call" a server that prints a log message.
	grpcFakeCall(ctx, func(ctx context.Context) {
		log.WithInboundFields(ctx, logger).Info("Print a server log with the unique ID")
		if err := json.Unmarshal(buf.Bytes(), &entry); err != nil {
			panic(err)
		}

		fmt.Printf("Unique ID = %v", entry.Fields["uid"])
	})
}

// Converts outgoing gRPC metadata (if present) to incoming metadata before calling the handler.
func grpcFakeCall(ctx context.Context, handler func(context.Context)) {
	if md, ok := metadata.FromOutgoingContext(ctx); ok {
		ctx = metadata.NewIncomingContext(ctx, md) // Set incoming context with caller's outgoing metadata.
	}

	ctx = metadata.NewOutgoingContext(ctx, metadata.MD{}) // Clear outgoing context.
	handler(ctx)
}
Output:

Unique ID = test_uid

func Warn

func Warn(ctx context.Context, msg string)

func Warnf

func Warnf(ctx context.Context, msg string, v ...interface{})

Types

type Logger

type Logger interface {
	Fatal(string)
	Fatalf(string, ...interface{})

	Error(string)
	Errorf(string, ...interface{})

	Warn(string)
	Warnf(string, ...interface{})

	Info(string)
	Infof(string, ...interface{})

	Debug(string)
	Debugf(string, ...interface{})

	WithError(error) Logger
	WithField(string, interface{}) Logger
	WithFields(map[string]interface{}) Logger
}

Logger describes the interface of a structured logger.

func FromContext

func FromContext(ctx context.Context, opts ...Option) Logger

FromContext returns a logger from the provided context. Options will be applied to a default configuration if the context doesn't have an associated logger.

func NewLogger

func NewLogger(opts ...Option) Logger

NewLogger returns a logger configured with the provided options.

func NewWithRecorder

func NewWithRecorder(opts ...Option) (Logger, *recorder)

Creates a Logger with a Recorder for accessing created log entries

func WithInboundFields

func WithInboundFields(ctx context.Context, logger Logger) Logger

WithInboundFields returns a new logger including fields from the incoming context's metadata.

type Metadata

type Metadata struct {
	// UID is a unique identifier for logs. It can be used to group sets of related logs.
	UID string
}

Metadata provides fields for multi-process log organization.

type Option

type Option func(*apexAdapter)

Options configure the logger.

var (
	DebugLevel Option = func(l *apexAdapter) {
		l.entry.Logger.Level = apex.DebugLevel
	}

	InfoLevel Option = func(l *apexAdapter) {
		l.entry.Logger.Level = apex.InfoLevel
	}

	WarnLevel Option = func(l *apexAdapter) {
		l.entry.Logger.Level = apex.WarnLevel
	}

	ErrorLevel Option = func(l *apexAdapter) {
		l.entry.Logger.Level = apex.ErrorLevel
	}

	FatalLevel Option = func(l *apexAdapter) {
		l.entry.Logger.Level = apex.FatalLevel
	}
)

Configures the logger to print logs with a minimum severity level.

func JSONFormat

func JSONFormat(w io.Writer) Option

JSONFormat configures the logger to print logs as JSON.

func TextFormat

func TextFormat(w io.Writer) Option

TextFormat configures the logger to print logs as human-friendly text.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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