logutil

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2022 License: CC0-1.0 Imports: 9 Imported by: 0

Documentation

Overview

Package logutil contains a simple leveled logging infrastructure supporting different log levels, package scopes, formatters and handlers.

The main object is the Logger object which requires a scope. Use GetLogger(scope string) to get an instance. Log messages are published by various log methods (e.g. Info).

The logger object is also used to add sinks which consume log messages. Each sinks requires a formatter which formats / decorades incoming log messages. Log messages are handled by the most specific scoped sinks which allow the message level.

Example:

logger = GetLogger("foo.bar")

logger.AddLogSink(Info, SimpleFormatter(), myLogFile)

logger.Info("A log message")

Index

Constants

View Source
const (
	Debug   Level = "Debug"
	Info          = "Info"
	Warning       = "Warning"
	Error         = "Error"
)

Log levels

Variables

This section is empty.

Functions

func ClearLogSinks

func ClearLogSinks()

ClearLogSinks removes all configured log sinks.

Types

type Formatter

type Formatter interface {

	/*
	   Format formats a given log message into a string.
	*/
	Format(level Level, scope string, msg ...interface{}) string
}

Formatter is used to format log messages.

func ConsoleFormatter

func ConsoleFormatter() Formatter

ConsoleFormatter returns a simple formatter which does a simple fmt.Sprintln on all log messages. It only adds the log level.

func SimpleFormatter

func SimpleFormatter() Formatter

SimpleFormatter returns a simple formatter which does a simple fmt.Sprintln on all log messages. It also adds a current timestamp, the message scope and log level.

func TemplateFormatter

func TemplateFormatter(template string) Formatter

TemplateFormatter returns a formatter which produces log messages according to a given template string. The template string may contain one or more of the following directives:

%s The scope of the log message %l The level of the log message %t Current timestamp (milliseconds elapsed since January 1, 1970 UTC) %f Function in which the log message was issued e.g. foo.bar.MyFunc() %c Code location of the log statement which issuing the log message e.g. package/somefile.go:12 %m The log message and its arguments formatted with fmt.Sprintf()

type Level

type Level string

Level represents a logging level

func StringToLoglevel

func StringToLoglevel(loglevelString string) Level

StringToLoglevel tries to turn a given string into a log level.

type Logger

type Logger interface {

	/*
	   AddLogSink adds a log sink to a logger. A log sink can be a file or console
	   which satisfies the io.Writer interface.
	*/
	AddLogSink(loglevel Level, formatter Formatter, appender io.Writer)

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

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

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

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

	/*
		Error logs a message at error level and a stacktrace.
	*/
	LogStackTrace(loglevel Level, msg ...interface{})
}

Logger is the main logging object which is used to add sinks and publish log messages. A log messages is only handled by the most appropriate sink in terms of level and scope. Multiple sinks can be registered for the same level and scope.

func GetLogger

func GetLogger(scope string) Logger

GetLogger returns a logger of a certain scope. Use the empty string ” for the root scope.

Jump to

Keyboard shortcuts

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