logger

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2023 License: 0BSD Imports: 4 Imported by: 1

README

Logger

Sane logging in go.

Configuration

Configuration belongs primarily in the environment

Output

It is the job of the consuming application's initiation to redirect stdout and stderr to the appropriate place(s)

e.g. For local work you desire all messages print to the stdout and stderr of your shell. For a production workflow, log messages need to be written and appended to a file, in addition to being printed to the terminal.

In the former scenario nothing needs to be done. But for a production workflow, whatever starts the application process may need to run

your_consuming_application 2>&1 | tee -a some_log_file.log

Log Level/Severity

Log level for the default logger is set to INFO, WARN, and ERROR.

Log level for consuming client application code can be done by passing the severity for a non-default logger along with where stdout and stderr should write to, e.g.:

	s := logger.New("", logger.Error | logger.Info, os.Stdout, os.Stderr)
Overriding Behavior

The first precedent to set severity is the mask passed during the creation of a new logger (see Log Level/Severity)

The second precedent are ENV variables prefixed with LOG_LEVEL_. These variables, if defined, will override behavior set through New logger instantiation. If defined and set to something other than 0 that log level will be enabled, vice versa if set to 0. This allows more granular control at runtime of which logging levels are displayed, as the variables can be set completely independently, i.e.:

  • LOG_LEVEL_ERROR=1 LOG_LEVEL_WARN=0 ...
  • LOG_LEVEL_ERROR=0 LOG_LEVEL_DEBUG=0 LOG_LEVEL_INFO=1 ...
  • LOG_LEVEL_ERROR=0 LOG_LEVEL_DEBUG=0 LOG_LEVEL_INFO=0 LOG_LEVEL_WARN=1 ...

Multiple Loggers

To configure specific loggers, prefix the ENV variable with the same prefix used for that logger. Configuration of log levels set for specific loggers will take priority over non-prefixed LOG_LEVEL_ configuration.

e.g., if you need a renderLogger and a computationalLogger, you might create them like this

	renderLogger := logger.New("renderer", logger.DefaultSeverity)
	computeLogger := logger.New("computer", logger.DefaultSeverity)

The default severity may be fine for most situations but for testing you may want additional DEBUG log messages from the computeLogger.

In that case, in the ENV of whatever is running tests, you can define

compute_LOG_LEVEL_DEBUG=1

and the computeLogger will additionally show DEBUG messages while the renderLogger will only show messages under the default severity (WARN and ERROR).

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultLogger = New("", DefaultSeverity, os.Stdout, os.Stderr)

DefaultLogger is an unprefixed logger using the default severity

Functions

This section is empty.

Types

type ChannelLabel

type ChannelLabel string

ChannelLabel is an string enumeration of the names of the channels

const (
	// ChannelLabelError is the prefix used for error messages
	ChannelLabelError ChannelLabel = "ERROR"
	// ChannelLabelWarn is the prefix used for warning messages
	ChannelLabelWarn ChannelLabel = "WARN"
	// ChannelLabelInfo is the prefix used for informational messages
	ChannelLabelInfo ChannelLabel = "INFO"
	// ChannelLabelDebug is the prefix used for debug messages
	ChannelLabelDebug ChannelLabel = "DEBUG"
)

type Log

type Log interface {
	Info(format string, v ...any)
	Debug(format string, v ...any)
	Warn(format string, v ...any)
	Error(format string, v ...any)
}

Log defines a general logger

type Logger

type Logger struct {
	Severity Severity
	// contains filtered or unexported fields
}

Logger is used to log to appropriate levels

func New

func New(prefix string, severity Severity, stdoutWriter io.Writer, stderrWriter io.Writer) (logger *Logger)

New returns a valid logger ready for use

func (*Logger) ChannelEnabled

func (logger *Logger) ChannelEnabled(channel Severity) bool

ChannelEnabled returns whether the severity is enabled (prints to the log)

func (*Logger) Debug

func (logger *Logger) Debug(format string, v ...any)

Debug a message

func (*Logger) Error

func (logger *Logger) Error(format string, v ...any)

Error a message

func (*Logger) Info

func (logger *Logger) Info(format string, v ...any)

Info a message

func (*Logger) Warn

func (logger *Logger) Warn(format string, v ...any)

Warn a message

type Severity

type Severity = int64

Severity is an alias for an int64

const (
	Error Severity = 1 << iota // Error shows error log messages
	Warn                       // Warn shows warning log messages
	Info                       // Info shows info log messages
	Debug                      // Debug shows debug log messages

	// DefaultSeverity shows error, warn, and info log messages
	DefaultSeverity Severity = Error | Warn | Info
)

Jump to

Keyboard shortcuts

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