log

package
v7.6.0 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2024 License: Apache-2.0 Imports: 14 Imported by: 5

Documentation

Overview

* Copyright 2020-2024 Open Networking Foundation (ONF) and the ONF Contributors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.

Index

Constants

View Source
const (
	// DebugLevel logs a message at debug level
	DebugLevel = LogLevel(iota)
	// InfoLevel logs a message at info level
	InfoLevel
	// WarnLevel logs a message at warning level
	WarnLevel
	// ErrorLevel logs a message at error level
	ErrorLevel
	// FatalLevel logs a message, then calls os.Exit(1).
	FatalLevel
)
View Source
const CONSOLE = "console"

CONSOLE formats the log for the console, mostly used during development

View Source
const JSON = "json"

JSON formats the log using json format, mostly used by an automated logging system consumption

View Source
const (
	RootSpanNameKey = "op-name"
)

Variables

This section is empty.

Functions

func CleanUp

func CleanUp() error

CleanUp flushed any buffered log entries. Applications should take care to call CleanUp before exiting.

func ConstructZapConfig

func ConstructZapConfig(outputType string, level LogLevel, fields Fields) zp.Config

func CreateAsyncSpan

func CreateAsyncSpan(ctx context.Context, taskName string, keyAndValues ...Fields) (opentracing.Span, context.Context)

Creates a Async Child Span with Follows-From relationship from Parent Span embedded in passed context. Should be used only in scenarios when a) There is dis-continuation in execution and thus result of Child span does not affect the Parent flow at all b) The execution of Child Span is guaranteed to start after the completion of Parent Span In case of any confusion, use CreateChildSpan method Some situations where this method would be suitable includes Kafka Async RPC call, Propagation of Event across a channel etc.

func CreateChildSpan

func CreateChildSpan(ctx context.Context, taskName string, keyAndValues ...Fields) (opentracing.Span, context.Context)

Creates a Child Span from Parent Span embedded in passed context. Should be used before starting a new major operation in Synchronous or Asynchronous mode (go routine), such as following: 1. Start of all implemented External API methods unless using a interceptor for auto-injection of Span (Server side impl) 2. Just before calling an Third-Party lib which is invoking a External API (etcd, kafka) 3. In start of a Go Routine responsible for performing a major task involving significant duration 4. Any method which is suspected to be time consuming...

func EnrichSpan

func EnrichSpan(ctx context.Context, keyAndValues ...Fields)

Method to inject additional log fields into Span e.g. device-id

func GetPackageNames

func GetPackageNames() []string

Return a list of all packages that have individually-configured loggers

func LogLevelToString

func LogLevelToString(l LogLevel) (string, error)

func MarkSpanError

func MarkSpanError(ctx context.Context, err error)

Method to inject Error into the Span in event of any operation failure

func SetAllLogLevel

func SetAllLogLevel(level LogLevel)

SetAllLogLevel sets the log level of all registered packages to level

func SetDefaultLogLevel

func SetDefaultLogLevel(level LogLevel)

SetDefaultLogLevel sets the log level used for packages that don't have specific loggers

func SetLogLevel

func SetLogLevel(level LogLevel) error

SetLogLevel sets the log level for the logger corresponding to the caller's package

func SetPackageLogLevel

func SetPackageLogLevel(packageName string, level LogLevel)

SetPackageLogLevel dynamically sets the log level of a given package to level. This is typically invoked at an application level during debugging

func TerminateTracing

func TerminateTracing(c io.Closer)

func UpdateAllCallerSkipLevel

func UpdateAllCallerSkipLevel(skipLevel int) error

UpdateAllCallerSkipLevel create new loggers for all registered pacakges with the default updated caller skipltFields. This will enable to skip wrapper file caller in caller info and stacktrace

func UpdateAllLoggers

func UpdateAllLoggers(defaultFields Fields) error

UpdateAllLoggers create new loggers for all registered pacakges with the defaultFields.

func UpdateLogger

func UpdateLogger(defaultFields Fields) error

UpdateLogger updates the logger associated with a caller's package with supplied defaultFields

func WithSpanFromContext

func WithSpanFromContext(targetCtx, sourceCtx context.Context) context.Context

Extracts the span from Source context and injects into the supplied Target context. This should be used in situations wherein we are calling a time-sensitive operation (etcd update) and hence had a context.Background() used earlier to avoid any cancellation/timeout of operation by passed context. This will allow propagation of span with a different base context (and not the original context)

Types

type ActiveTracerProxy

type ActiveTracerProxy struct {
}

A Wrapper to utilize currently Active Tracer instance. The middleware library being used for generating Spans for GRPC API calls does not support dynamically setting the Active Tracer similar to the SetGlobalTracer method provided by OpenTracing API

func (ActiveTracerProxy) Extract

func (atw ActiveTracerProxy) Extract(format interface{}, carrier interface{}) (opentracing.SpanContext, error)

func (ActiveTracerProxy) Inject

func (atw ActiveTracerProxy) Inject(sm opentracing.SpanContext, format interface{}, carrier interface{}) error

func (ActiveTracerProxy) StartSpan

func (atw ActiveTracerProxy) StartSpan(operationName string, opts ...opentracing.StartSpanOption) opentracing.Span

type CLogger

type CLogger interface {
	Debug(context.Context, ...interface{})
	Debugln(context.Context, ...interface{})
	Debugf(context.Context, string, ...interface{})
	Debugw(context.Context, string, Fields)

	Info(context.Context, ...interface{})
	Infoln(context.Context, ...interface{})
	Infof(context.Context, string, ...interface{})
	Infow(context.Context, string, Fields)

	Warn(context.Context, ...interface{})
	Warnln(context.Context, ...interface{})
	Warnf(context.Context, string, ...interface{})
	Warnw(context.Context, string, Fields)

	Error(context.Context, ...interface{})
	Errorln(context.Context, ...interface{})
	Errorf(context.Context, string, ...interface{})
	Errorw(context.Context, string, Fields)

	Fatal(context.Context, ...interface{})
	Fatalln(context.Context, ...interface{})
	Fatalf(context.Context, string, ...interface{})
	Fatalw(context.Context, string, Fields)

	With(Fields) CLogger

	// The following are added to be able to use this logger as a gRPC LoggerV2 if needed
	//
	Warning(context.Context, ...interface{})
	Warningln(context.Context, ...interface{})
	Warningf(context.Context, string, ...interface{})

	// V reports whether verbosity level l is at least the requested verbose level.
	V(l LogLevel) bool

	//Returns the log level of this specific logger
	GetLogLevel() LogLevel
}

Context Aware Logger represents an abstract logging interface. Any logging implementation used will need to abide by this interface

func RegisterPackage

func RegisterPackage(outputType string, level LogLevel, defaultFields Fields, pkgNames ...string) (CLogger, error)

AddPackage registers a package to the log map. Each package gets its own logger which allows its config (loglevel) to be changed dynamically without interacting with the other packages. outputType is JSON, level is the lowest level log to output with this logger and defaultFields is a map of key-value pairs to always add to the output. Note: AddPackage also returns a reference to the actual logger. If a calling package uses this reference directly instead of using the publicly available functions in this log package then a number of functionalities will not be available to it, notably log tracing with filename.functionname.linenumber annotation.

pkgNames parameter should be used for testing only as this function detects the caller's package.

func SetDefaultLogger

func SetDefaultLogger(outputType string, level LogLevel, defaultFields Fields) (CLogger, error)

SetLogger needs to be invoked before the logger API can be invoked. This function initialize the default logger (zap's sugaredlogger)

func UpdateCallerSkipLevel

func UpdateCallerSkipLevel(skipLevel int) (CLogger, error)

UpdateCallerSkipLevel create new loggers for specified registered pacakges with the default updated caller skipltFields. This will enable to skip wrapper file caller in caller info and stacktrace

type Fields

type Fields map[string]interface{}

Fields is used as key-value pairs for structured logging

type LogFeaturesManager

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

Global Settings governing the Log Correlation and Tracing features. Should only be updated through the exposed public methods

func GetGlobalLFM

func GetGlobalLFM() *LogFeaturesManager

func (*LogFeaturesManager) ExtractContextAttributes

func (lfm *LogFeaturesManager) ExtractContextAttributes(ctx context.Context) []interface{}

Extracts details of Execution Context as log fields from the Tracing Span injected into the context instance. Following log fields are extracted: 1. Operation Name : key as 'op-name' and value as Span operation name 2. Operation Id : key as 'op-id' and value as 64 bit Span Id in hex digits string

Additionally, any tags present in Span are also extracted to use as log fields e.g. device-id.

If no Span is found associated with context, blank slice is returned without any log fields

func (*LogFeaturesManager) GetLogCorrelationStatus

func (lfm *LogFeaturesManager) GetLogCorrelationStatus() bool

func (*LogFeaturesManager) GetTracePublishingStatus

func (lfm *LogFeaturesManager) GetTracePublishingStatus() bool

func (*LogFeaturesManager) InitTracingAndLogCorrelation

func (lfm *LogFeaturesManager) InitTracingAndLogCorrelation(tracePublishEnabled bool, traceAgentAddress string, logCorrelationEnabled bool) (io.Closer, error)

Method to Initialize Jaeger based Tracing client based on initial status of Tracing Publish and Log Correlation

func (*LogFeaturesManager) SetLogCorrelationStatus

func (lfm *LogFeaturesManager) SetLogCorrelationStatus(isEnabled bool)

func (*LogFeaturesManager) SetTracePublishingStatus

func (lfm *LogFeaturesManager) SetTracePublishingStatus(isEnabled bool)

type LogLevel

type LogLevel int8

func GetDefaultLogLevel

func GetDefaultLogLevel() LogLevel

GetDefaultLogLevel gets the log level used for packages that don't have specific loggers

func GetPackageLogLevel

func GetPackageLogLevel(packageName ...string) (LogLevel, error)

GetPackageLogLevel returns the current log level of a package.

func StringToLogLevel

func StringToLogLevel(l string) (LogLevel, error)

Jump to

Keyboard shortcuts

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