Documentation ¶
Index ¶
- func Debug(ctx context.Context, msg string)
- func Debugf(ctx context.Context, msg string, v ...interface{})
- func Error(ctx context.Context, msg string)
- func Errorf(ctx context.Context, msg string, v ...interface{})
- func Fatal(ctx context.Context, msg string)
- func Fatalf(ctx context.Context, msg string, v ...interface{})
- func Info(ctx context.Context, msg string)
- func Infof(ctx context.Context, msg string, v ...interface{})
- func NewContext(ctx context.Context, logger Logger) context.Context
- func NewOutboundContext(ctx context.Context, md Metadata) context.Context
- func Warn(ctx context.Context, msg string)
- func Warnf(ctx context.Context, msg string, v ...interface{})
- type Logger
- type Metadata
- type Option
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewContext ¶
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 ¶
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
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 ¶
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 NewWithRecorder ¶
Creates a Logger with a Recorder for accessing created log entries
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 ¶
JSONFormat configures the logger to print logs as JSON.
func TextFormat ¶
TextFormat configures the logger to print logs as human-friendly text.