logging

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2022 License: MIT Imports: 7 Imported by: 13

Documentation

Overview

Package logging provides a simple logging interface for 12-Factor applications.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(l Logger, f string, v ...interface{})

Debug writes a debug log message formatted according to a format specifier.

If IsDebug() returns false, no logging is performed.

It should be used for messages that are intended for the software developers that maintain the application.

f is the format specifier, as per fmt.Printf(), etc.

If l is nil, DefaultLogger is used.

func DebugString

func DebugString(l Logger, s string)

DebugString writes a pre-formatted debug log message.

If IsDebug() returns false, no logging is performed.

It should be used for messages that are intended for the software developers that maintain the application.

If l is nil, DefaultLogger is used.

func IsDebug

func IsDebug(l Logger) bool

IsDebug returns true if this logger will perform debug logging.

Generally the application should just call Debug() or DebugString() without calling IsDebug(), however it can be used to check if debug logging is necessary before executing expensive code that is only used to obtain debug information.

If l is nil, DefaultLogger is used.

func Log

func Log(l Logger, f string, v ...interface{})

Log writes an application log message formatted according to a format specifier.

It should be used for messages that are intended for people responsible for operating the application, such as the end-user or operations staff.

f is the format specifier, as per fmt.Printf(), etc.

If l is nil, DefaultLogger is used.

func LogString

func LogString(l Logger, s string)

LogString writes a pre-formatted application log message.

It should be used for messages that are intended for people responsible for operating the application, such as the end-user or operations staff.

If l is nil, DefaultLogger is used.

Types

type BufferedLogMessage

type BufferedLogMessage struct {
	Message string
	IsDebug bool
}

BufferedLogMessage is a log message stored by a BufferedLogger

func (BufferedLogMessage) String

func (m BufferedLogMessage) String() string

type BufferedLogger

type BufferedLogger struct {
	// CaptureDebug controls whether debug messages should be stored.
	CaptureDebug bool
	// contains filtered or unexported fields
}

BufferedLogger is an implementation of Logger that buffers log messages in memory.

func (*BufferedLogger) Debug

func (l *BufferedLogger) Debug(f string, v ...interface{})

Debug writes a debug log message formatted according to a format specifier.

If IsDebug() returns false, no logging is performed.

It should be used for messages that are intended for the software developers that maintain the application.

f is the format specifier, as per fmt.Printf(), etc.

func (*BufferedLogger) DebugString

func (l *BufferedLogger) DebugString(s string)

DebugString writes a pre-formatted debug log message.

If IsDebug() returns false, no logging is performed.

It should be used for messages that are intended for the software developers that maintain the application.

func (*BufferedLogger) FlushTo

func (l *BufferedLogger) FlushTo(dest Logger)

FlushTo logs the buffered messages to dest, and resets the logger.

func (*BufferedLogger) IsDebug

func (l *BufferedLogger) IsDebug() bool

IsDebug returns true if this logger will perform debug logging.

Generally the application should just call Debug() or DebugString() without calling IsDebug(), however it can be used to check if debug logging is necessary before executing expensive code that is only used to obtain debug information.

func (*BufferedLogger) Log

func (l *BufferedLogger) Log(f string, v ...interface{})

Log writes an application log message formatted according to a format specifier.

It should be used for messages that are intended for people responsible for operating the application, such as the end-user or operations staff.

f is the format specifier, as per fmt.Printf(), etc.

func (*BufferedLogger) LogString

func (l *BufferedLogger) LogString(s string)

LogString writes a pre-formatted application log message.

It should be used for messages that are intended for people responsible for operating the application, such as the end-user or operations staff.

func (*BufferedLogger) Messages

func (l *BufferedLogger) Messages() []BufferedLogMessage

Messages returns the messages that have been logged.

func (*BufferedLogger) Reset

func (l *BufferedLogger) Reset()

Reset removes all buffered log messages.

func (*BufferedLogger) TakeMessages

func (l *BufferedLogger) TakeMessages() []BufferedLogMessage

TakeMessages returns the messages that have been logged and resets the logger in a single operation.

type Callback added in v0.1.2

type Callback func(f string, v ...interface{})

Callback is the function signature for Printf-style callbacks used by CallbackLogger.

type CallbackLogger added in v0.1.2

type CallbackLogger struct {
	// LogTarget is the target for non-debug messages.
	LogTarget Callback

	// DebugTarget is the target for debug messages.
	// If it is nil no debug logging will occur and IsDebug() returns false.
	DebugTarget Callback
}

CallbackLogger is an implementation of Logger that forwards log messages to user-supplied callback functions.

func (*CallbackLogger) Debug added in v0.1.2

func (l *CallbackLogger) Debug(f string, v ...interface{})

Debug writes a debug log message formatted according to a format specifier.

If IsDebug() returns false, no logging is performed.

It should be used for messages that are intended for the software developers that maintain the application.

f is the format specifier, as per fmt.Printf(), etc.

func (*CallbackLogger) DebugString added in v0.1.2

func (l *CallbackLogger) DebugString(s string)

DebugString writes a pre-formatted debug log message.

It should be used for messages that are intended for the software developers that maintain the application.

func (*CallbackLogger) IsDebug added in v0.1.2

func (l *CallbackLogger) IsDebug() bool

IsDebug returns true if this logger will perform debug logging.

Generally the application should just call Debug() or DebugString() without calling IsDebug(), however it can be used to check if debug logging is necessary before executing expensive code that is only used to obtain debug information.

func (*CallbackLogger) Log added in v0.1.2

func (l *CallbackLogger) Log(f string, v ...interface{})

Log writes an application log message formatted according to a format specifier.

It should be used for messages that are intended for people responsible for operating the application, such as the end-user or operations staff.

f is the format specifier, as per fmt.Printf(), etc.

func (*CallbackLogger) LogString added in v0.1.2

func (l *CallbackLogger) LogString(s string)

LogString writes a pre-formatted application log message.

It should be used for messages that are intended for people responsible for operating the application, such as the end-user or operations staff.

type DiscardLogger

type DiscardLogger struct{}

DiscardLogger is a logger that produces no output.

func (DiscardLogger) Debug

func (DiscardLogger) Debug(f string, v ...interface{})

Debug is a no-op.

func (DiscardLogger) DebugString

func (DiscardLogger) DebugString(s string)

DebugString is a no-op.

func (DiscardLogger) IsDebug

func (DiscardLogger) IsDebug() bool

IsDebug always returns false.

func (DiscardLogger) Log

func (DiscardLogger) Log(f string, v ...interface{})

Log is a no-op.

func (DiscardLogger) LogString

func (DiscardLogger) LogString(s string)

LogString is a no-op.

type LineWriter added in v1.0.0

type LineWriter struct {
	// Target is the logger that receives the log messages.
	Target Logger
}

LineWriter is an adaptor that presents a Logger as an io.Writer.

For each call to Write() the data is forwarded to the logger as a separate message.

func (*LineWriter) Write added in v1.0.0

func (w *LineWriter) Write(data []byte) (int, error)

type Logger

type Logger interface {
	// Log writes an application log message formatted according to a format
	// specifier.
	//
	// It should be used for messages that are intended for people responsible
	// for operating the application, such as the end-user or operations staff.
	//
	// f is the format specifier, as per fmt.Printf(), etc.
	Log(f string, v ...interface{})

	// LogString writes a pre-formatted application log message.
	//
	// It should be used for messages that are intended for people responsible
	// for operating the application, such as the end-user or operations staff.
	LogString(s string)

	// Debug writes a debug log message formatted according to a format
	// specifier.
	//
	// If IsDebug() returns false, no logging is performed.
	//
	// It should be used for messages that are intended for the software
	// developers that maintain the application.
	//
	// f is the format specifier, as per fmt.Printf(), etc.
	Debug(f string, v ...interface{})

	// DebugString writes a pre-formatted debug log message.
	//
	// If IsDebug() returns false, no logging is performed.
	//
	// It should be used for messages that are intended for the software
	// developers that maintain the application.
	DebugString(s string)

	// IsDebug returns true if this logger will perform debug logging.
	//
	// Generally the application should just call Debug() or DebugString()
	// without calling IsDebug(), however it can be used to check if debug
	// logging is necessary before executing expensive code that is only used to
	// obtain debug information.
	IsDebug() bool
}

Logger is an interface for writing log messages.

var (
	// DefaultLogger is a logger that only logs non-debug messages.
	DefaultLogger Logger = &StandardLogger{CaptureDebug: false}

	// DebugLogger is a logger that logs both debug and non-debug messages.
	DebugLogger Logger = &StandardLogger{CaptureDebug: true}

	// SilentLogger is a logger that does not log any messages.
	SilentLogger Logger = DiscardLogger{}
)

func Demote added in v0.2.2

func Demote(target Logger) Logger

Demote returns a logger that forwards all messages to the target logger as debug messages. Thus, it "demotes" non-debug messages to the debug level.

If the target is nil, DefaultLogger is used.

func Prefix added in v0.2.2

func Prefix(target Logger, f string, v ...interface{}) Logger

Prefix returns a logger that prefixes all messages with a fixed string.

f is the format specifier for the prefix, as per fmt.Printf(), etc.

If the target is nil, DefaultLogger is used.

func Promote added in v0.2.2

func Promote(target Logger) Logger

Promote returns a Logger that forwards all messages to a target logger as non-debug messages. Thus, it "promotes" debug messages to the non-debug level.

If the target is nil, DefaultLogger is used.

func Tee added in v1.3.0

func Tee(targets ...Logger) Logger

Tee returns a logger that forwards all messages to multiple target loggers.

It panics if no targets are provided, ensuring that log messages are not lost due to misconfiguration.

The target list is de-duplicated, ensuring that log messages are not repeatedly sent to the same logger. Two loggers are considered equal if they compare the same using a regular shallow interface comparison via ==.

Although the returned logger "wraps" the target loggers, it does not implement Wrapper, as that interface does not support multiple loggers.

func Unwrap added in v0.2.2

func Unwrap(l Logger) Logger

Unwrap returns the "inner-most" logger wrapped by l.

If l is nil, or any of the wrapped loggers a nil, DefaultLogger is returned.

func Zap added in v1.4.0

func Zap(target *zap.Logger) Logger

Zap returns a Logger that writes to a Zap logger.

type StandardLogger

type StandardLogger struct {
	// Target is the standard Go logger used to write messages. If it is nil,
	// logs are sent to STDOUT as per the 12-factor logging recomendations. Note
	// that this differs to Go's default logger, which writes to STDERR.
	Target *log.Logger

	// CaptureDebug controls whether debug messages should be written to the
	// target logger.
	CaptureDebug bool
}

StandardLogger is an implementation of Logger that uses Go's standard logger package.

func (*StandardLogger) Debug

func (l *StandardLogger) Debug(f string, v ...interface{})

Debug writes a debug log message formatted according to a format specifier.

If IsDebug() returns false, no logging is performed.

It should be used for messages that are intended for the software developers that maintain the application.

f is the format specifier, as per fmt.Printf(), etc.

func (*StandardLogger) DebugString

func (l *StandardLogger) DebugString(s string)

DebugString writes a pre-formatted debug log message.

If IsDebug() returns false, no logging is performed.

It should be used for messages that are intended for the software developers that maintain the application.

func (*StandardLogger) IsDebug

func (l *StandardLogger) IsDebug() bool

IsDebug returns true if this logger will perform debug logging.

Generally the application should just call Debug() or DebugString() without calling IsDebug(), however it can be used to check if debug logging is necessary before executing expensive code that is only used to obtain debug information.

func (*StandardLogger) Log

func (l *StandardLogger) Log(f string, v ...interface{})

Log writes an application log message formatted according to a format specifier.

It should be used for messages that are intended for people responsible for operating the application, such as the end-user or operations staff.

f is the format specifier, as per fmt.Printf(), etc.

func (*StandardLogger) LogString

func (l *StandardLogger) LogString(s string)

LogString writes a pre-formatted application log message.

It should be used for messages that are intended for people responsible for operating the application, such as the end-user or operations staff.

type StreamWriter added in v1.0.0

type StreamWriter struct {
	// Target is the logger that receives the log messages.
	Target Logger
	// contains filtered or unexported fields
}

StreamWriter is an adaptor that presents a Logger as an io.WriteCloser.

Each line of text written via Write() is logged as a separate message. Any call to write with text that does not end in line separator is buffered until a line separator is written or Close() is called. Blank lines are ignored.

Any instance of a LF, CR or CRLF is treated as a line separator. This allows usage with Unix-style or Windows-style text output, as well as console output that uses CR to overwrite the current line.

func (*StreamWriter) Close added in v1.0.0

func (w *StreamWriter) Close() error

Close closes the writer, producing a log message from any remaining buffered text.

func (*StreamWriter) Write added in v1.0.0

func (w *StreamWriter) Write(data []byte) (int, error)

type Wrapper added in v0.2.2

type Wrapper interface {
	Logger

	// UnwrapLogger returns the logger wrapped by this logger.
	//
	// If ok is true it means that this logger is wrapping another logger, even
	// if that logger is nil, indicating that DefaultLogger should be used.
	//
	// If ok is false it means that this logger is not wrapping another logger.
	UnwrapLogger() (l Logger, ok bool)
}

Wrapper is an interface for loggers that "wrap" some other logger.

Jump to

Keyboard shortcuts

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