log

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2025 License: Apache-2.0 Imports: 16 Imported by: 0

README

ContainerSSH logger

This package provides a Logger interface that can log all kinds of messages and errors, including the Message interface from the message package. It provides the following methods for logging:

logger.Debug(message ...interface{})
logger.Info(message ...interface{})
logger.Notice(message ...interface{})
logger.Warning(message ...interface{})
logger.Error(message ...interface{})
logger.Critical(message ...interface{})
logger.Alert(message ...interface{})
logger.Emergency(message ...interface{})

You are encouraged to pass a message.Message implementation to it. We also provide the following compatibility methods which log at the info level.

logger.Log(v ...interface{})
logger.Logf(format string, v ...interface{})

We provide a method to create a child logger that has a different minimum log level. Messages below this level will be discarded:

newLogger := logger.WithLevel(log.LevelInfo)

We can also create a new logger copy with default labels added:

newLogger := logger.WithLabel("label name", "label value")

Finally, the logger also supports calling the Rotate() and Close() methods. Rotate() instructs the output to close all handles and reopen them to facilitate rotating logs. Close() permanently closes the writer.

Creating a logger

The Logger interface is intended for generic implementations. The default implementation can be created as follows:

logger, err := log.NewLogger(config)

Alternatively, you can also use the log.MustNewLogger method to skip having to deal with the error. (It will panic if an error happens.)

If you need a factory you can use the log.LoggerFactory interface and the log.NewLoggerFactory to create a factory you can pass around. The Make(config) method will make a new logger when needed.

Documentation

Overview

Package log contains utilities to log messages in ContainerSSH.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewGoLogWriter

func NewGoLogWriter(backendLogger Logger) io.Writer

NewGoLogWriter creates an adapter for the go logger that writes using the Log method of the logger.

func RunTests

func RunTests(m *testing.M)

RunTests is a helper function that transforms the test output depending on the CI system running. This is most prominently used with GitHub Actions to produce a nice-looking output.

Types

type Logger

type Logger interface {
	// WithLevel returns a copy of the logger for a specified log level. Panics if the log level provided is invalid.
	WithLevel(level config.LogLevel) Logger
	// WithLabel returns a logger with an added label (e.g. username, IP, etc.) Panics if the label name is empty.
	WithLabel(labelName message.LabelName, labelValue message.LabelValue) Logger

	// Debug logs a message at the debug level.
	Debug(message ...interface{})

	// Info logs a message at the info level.
	Info(message ...interface{})

	// Notice logs a message at the notice level.
	Notice(message ...interface{})

	// Warning logs a message at the warning level.
	Warning(message ...interface{})

	// Error logs a message at the error level.
	Error(message ...interface{})

	// Critical logs a message at the critical level.
	Critical(message ...interface{})

	// Alert logs a message at the alert level.
	Alert(message ...interface{})

	// Emergency logs a message at the emergency level.
	Emergency(message ...interface{})

	// Log logs a number of objects or strings to the log.
	Log(v ...interface{})
	// Logf formats a message and logs it.
	Logf(format string, v ...interface{})

	// Rotate triggers the logging backend to close all connections and reopen them to allow for rotating log files.
	Rotate() error
	// Close closes the logging backend.
	Close() error
}

Logger The logger interface provides logging facilities on various levels.

func MustNewLogger

func MustNewLogger(
	config config.LogConfig,
) Logger

MustNewLogger is identical to NewLogger, except that it panics instead of returning an error

func NewLogger

func NewLogger(config config.LogConfig) (Logger, error)

NewLogger creates a standard logger pipeline.

- config is the configuration structure for the logger

goland:noinspection GoUnusedExportedFunction

func NewTestLogger

func NewTestLogger(t *testing.T) Logger

NewTestLogger creates a logger for testing purposes.

type LoggerFactory

type LoggerFactory interface {
	// Make creates a new logger with the specified configuration and module.
	//
	// - config is the configuration structure.
	//
	// Return:
	//
	// - Logger is the logger created.
	// - error is returned if the configuration was invalid.
	Make(config config.LogConfig) (Logger, error)

	// MustMake is identical to Make but panics if an error happens
	MustMake(config config.LogConfig) Logger
}

LoggerFactory is a factory to create a logger on demand

func NewLoggerFactory

func NewLoggerFactory() LoggerFactory

NewLoggerFactory create a pipeline logger factory

type Writer

type Writer interface {
	// Write writes a log message to the output.
	Write(level config.LogLevel, message message.Message) error
	// Rotate attempts to rotate the logs. Has no effect on non-file based loggers.
	Rotate() error

	io.Closer
}

Writer is a specialized writer to write a line of log messages.

Jump to

Keyboard shortcuts

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