ldlog

package
v3.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2023 License: Apache-2.0 Imports: 3 Imported by: 30

Documentation

Overview

Package ldlog contains a logging abstraction used by LaunchDarkly SDK components.

These types and methods allow you to customize the logging behavior of the SDK, such as filtering log output by level or redirecting it to another destination.

For a test implementation that captures output, see github.com/launchdarkly/go-sdk-common/v3/ldlogtest.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseLogger

type BaseLogger interface {
	// Logs a message on a single line. This is equivalent to log.Logger.Println.
	Println(values ...interface{})
	// Logs a message on a single line, applying a format string. This is equivalent to log.Logger.Printf.
	Printf(format string, values ...interface{})
}

BaseLogger is a generic logger interface with no level mechanism. Since its methods are a subset of Go's log.Logger, you may use log.New to create a BaseLogger.

This is identical to the Logger interface in the main SDK package. It is redefined here so the ldlog package does not have to refer back to the main package.

type LogLevel

type LogLevel int

LogLevel describes one of the possible thresholds of log message, from LogDebug to LogError.

const (

	// Debug is the least significant logging level, containing verbose output you will normally
	// not need to see. This level is disabled by default.
	Debug LogLevel = iota
	// Info is the logging level for informational messages about normal operations. This level
	// is enabled by default.
	Info LogLevel = iota
	// Warn is the logging level for more significant messages about an uncommon condition that
	// is not necessarily an error. This level is enabled by default.
	Warn LogLevel = iota
	// Error is the logging level for error conditions that should not happen during normal
	// operation of the SDK. This level is enabled by default.
	Error LogLevel = iota
	// None means no messages at all should be logged.
	None LogLevel = iota
)

func (LogLevel) Name

func (level LogLevel) Name() string

Name returns a descriptive name for this log level.

func (LogLevel) String

func (level LogLevel) String() string

String is the default string representation of LogLevel, which is the same as LogLevel.Name.

type Loggers

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

Loggers is a configurable logging component with a level filter.

By default, Loggers sends output to standard error and enables all levels except Debug. You may call any of its Set methods to change this configuration.

func NewDefaultLoggers

func NewDefaultLoggers() Loggers

NewDefaultLoggers returns a new Loggers instance with default properties.

This is different from an empty Loggers{} instance in that the latter will not produce any output until you call either Loggers.SetBaseLogger, Loggers.SetMinLevel, or Loggers.Init on it. Calling NewDefaultLoggers() ensures that the default minimum level of Info is enabled.

func NewDisabledLoggers

func NewDisabledLoggers() Loggers

NewDisabledLoggers returns a Loggers instance that will never generate output.

func (Loggers) Debug

func (l Loggers) Debug(values ...interface{})

Debug logs a message at Debug level, if that level is enabled. It calls the BaseLogger's Println.

func (Loggers) Debugf

func (l Loggers) Debugf(format string, values ...interface{})

Debugf logs a message at Debug level with a format string, if that level is enabled. It calls the BaseLogger's Printf.

func (Loggers) Error

func (l Loggers) Error(values ...interface{})

Error logs a message at Error level, if that level is enabled. It calls the BaseLogger's Println.

func (Loggers) Errorf

func (l Loggers) Errorf(format string, values ...interface{})

Errorf logs a message at Error level with a format string, if that level is enabled. It calls the BaseLogger's Printf.

func (Loggers) ForLevel

func (l Loggers) ForLevel(level LogLevel) BaseLogger

ForLevel returns a BaseLogger that writes messages at the specified level. Use this if you have code that already uses the Printf/Println methods. All of the existing level configuration still applies, so, for instance, loggers.ForLevel(Debug).Println("x") is exactly the same as loggers.Debug("x").

If the level is not a valid log level, the return value is non-nil but will produce no output.

func (Loggers) GetMinLevel

func (l Loggers) GetMinLevel() LogLevel

GetMinLevel returns the minimum level that has been specified for log output. The default is Info.

func (Loggers) Info

func (l Loggers) Info(values ...interface{})

Info logs a message at Info level, if that level is enabled. It calls the BaseLogger's Println.

func (Loggers) Infof

func (l Loggers) Infof(format string, values ...interface{})

Infof logs a message at Info level with a format string, if that level is enabled. It calls the BaseLogger's Printf.

func (*Loggers) Init

func (l *Loggers) Init()

Init ensures that the Loggers instance is ready to use.

This is necessary only if you have a Loggers instance that was not produced by NewDefaultLoggers and that may not have had Loggers.SetBaseLogger or Loggers.SetMinLevel called on it. It ensures that the default properties have been set. If you have already set any properties, Init does nothing.

func (Loggers) IsDebugEnabled

func (l Loggers) IsDebugEnabled() bool

IsDebugEnabled returns true if the minimum log level is Debug, or false if it is higher.

This allows for greater efficiency in code that can produce verbose debug output. When the Debug level is disabled, calling Loggers.Debug or Loggers.Debugf does not produce any output but they can still cause unwanted overhead due to having to convert their parameters to interface{} values. To avoid that overhead, you can choose to not bother calling Debug or Debugf at all if IsDebugEnabled returns false.

func (*Loggers) SetBaseLogger

func (l *Loggers) SetBaseLogger(baseLogger BaseLogger)

SetBaseLogger specifies the default destination for output at all log levels. This does not apply to any levels whose BaseLogger has been overridden with Loggers.SetBaseLoggerForLevel. All messages written to this logger will be prefixed with "NAME: " where NAME is DEBUG, INFO, etc.

If baseLogger is nil, nothing is changed.

func (*Loggers) SetBaseLoggerForLevel

func (l *Loggers) SetBaseLoggerForLevel(level LogLevel, baseLogger BaseLogger)

SetBaseLoggerForLevel specifies the default destination for output at the given log level. All messages written to this logger will be prefixed with "NAME: " where NAME is DEBUG, INFO, etc.

If baseLogger is nil, this level will use the default from Loggers.SetBaseLogger.

func (*Loggers) SetMinLevel

func (l *Loggers) SetMinLevel(minLevel LogLevel)

SetMinLevel specifies the minimum level for log output, where Debug is the lowest and Error is the highest. Log messages at a level lower than this will be suppressed. The default is Info.

func (*Loggers) SetPrefix

func (l *Loggers) SetPrefix(prefix string)

SetPrefix specifies a string to be added before every log message, after the LEVEL: prefix. Do not include a trailing space.

func (Loggers) Warn

func (l Loggers) Warn(values ...interface{})

Warn logs a message at Warn level, if that level is enabled. It calls the BaseLogger's Println.

func (Loggers) Warnf

func (l Loggers) Warnf(format string, values ...interface{})

Warnf logs a message at Warn level with a format string, if that level is enabled. It calls the BaseLogger's Printf.

Jump to

Keyboard shortcuts

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