log

package
v3.2.10 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2021 License: LGPL-3.0 Imports: 19 Imported by: 153

Documentation

Overview

Package log is an output-library that can print nicely formatted messages to the screen.

There are log-level messages that will be printed according to the current debug-level set. Furthermore a set of common messages exist that are printed according to a chosen format.

The log-level messages are:

log.Lvl1("Important information")
log.Lvl2("Less important information")
log.Lvl3("Eventually flooding information")
log.Lvl4("Definitively flooding information")
log.Lvl5("I hope you never need this")

in your program, then according to the debug-level one or more levels of output will be shown. To set the debug-level, use

log.SetDebugVisible(3)

which will show all `Lvl1`, `Lvl2`, and `Lvl3`. If you want to turn on just one output, you can use

log.LLvl2("Less important information")

By adding a single 'L' to the method, it *always* gets printed.

You can also add a 'f' to the name and use it like fmt.Printf:

log.Lvlf1("Level: %d/%d", now, max)

The common messages are:

log.Print("Simple output")
log.Info("For your information")
log.Warn("Only a warning")
log.Error("This is an error, but continues")
log.Panic("Something really went bad - calls panic")
log.Fatal("No way to continue - calls os.Exit")

These messages are printed according to the value of 'Format': - Format == FormatLvl - same as log.Lvl - Format == FormatPython - with some nice python-style formatting - Format == FormatNone - just as plain text

The log-package also takes into account the following environment-variables:

	DEBUG_LVL // will act like SetDebugVisible
	DEBUG_TIME // if 'true' it will print the date and time
 DEBUG_FILEPATH // if 'true' it will print the absolute filepath
	DEBUG_COLOR // if 'false' it will not use colors
	DEBUG_PADDING // if 'false' it will not use padding

But for this the function ParseEnv() or AddFlags() has to be called.

Index

Examples

Constants

View Source
const (
	// DefaultStdDebugLvl is the default debug level for the standard logger
	DefaultStdDebugLvl = 1
	// DefaultStdShowTime is the default value for 'showTime' for the standard logger
	DefaultStdShowTime = false
	// DefaultStdAbsoluteFilePath is the default to show absolute path for the standard logger
	DefaultStdAbsoluteFilePath = false
	// DefaultStdUseColors is the default value for 'useColors' for the standard logger
	DefaultStdUseColors = false
	// DefaultStdPadding is the default value for 'padding' for the standard logger
	DefaultStdPadding = true
)
View Source
const (
	// FormatPython uses [x] and others to indicate what is shown
	FormatPython = -1
	// FormatNone is just pure print
	FormatNone = 0
)

These formats can be used in place of the debugVisible

Variables

View Source
var LinePadding = 3

LinePadding of line-numbers for a nice debug-output - used in the same way as NamePadding. Deprecated: the caller is merged thus no line padding is necessary anymore.

View Source
var MainTestWait = 0 * time.Minute

MainTestWait is the maximum time the MainTest-method waits before aborting and printing a stack-trace of all functions. This is deprecated and should not be used anymore.

View Source
var NamePadding = 40

NamePadding - the padding of functions to make a nice debug-output - this is automatically updated whenever there are longer functions and kept at that new maximum. If you prefer to have a fixed output and don't remember oversized names, put a negative value in here.

View Source
var StaticMsg = ""

StaticMsg - if this variable is set, it will be outputted between the position and the message.

Functions

func AbsoluteFilePath

func AbsoluteFilePath() bool

AbsoluteFilePath returns the current setting for showing the absolute path inside a log.

func AddUserUninterestingGoroutine

func AddUserUninterestingGoroutine(newGr string)

AddUserUninterestingGoroutine can be called when the environment of some specific tests leaks goroutines unknown to interestingGoroutines(). This function is not safe for concurrent execution. The caller should add all of the desired exceptions before launching any goroutines.

func AfterTest

func AfterTest(t *testing.T)

AfterTest can be called to wait for leaking goroutines to finish. If they do not finish after a reasonable time (600ms) the test will fail.

Inspired by https://golang.org/src/net/http/main_test.go and https://github.com/coreos/etcd/blob/master/pkg/testutil/leak.go

func DebugVisible

func DebugVisible() int

DebugVisible returns the actual visible debug-level

func ErrFatal

func ErrFatal(err error, args ...interface{})

ErrFatal calls log.Fatal in the case err != nil

func ErrFatalf

func ErrFatalf(err error, f string, args ...interface{})

ErrFatalf will call Fatalf when the error is non-nil

func Error

func Error(args ...interface{})

Error prints out the error message. If the argument is an error, it will print it using "%+v".

func Errorf

func Errorf(f string, args ...interface{})

Errorf is like Error but with a format-string

func Fatal

func Fatal(args ...interface{})

Fatal prints out the fatal message and quits with os.Exit(1)

func Fatalf

func Fatalf(f string, args ...interface{})

Fatalf is like Fatal but with a format-string

func GetStdErr

func GetStdErr() string

GetStdErr returns all log.*-outputs to StdErr since the last call.

func GetStdOut

func GetStdOut() string

GetStdOut returns all log.*-outputs to StdOut since the last call.

func Info

func Info(args ...interface{})

Info prints the arguments given with a 'info'-format

func Infof

func Infof(f string, args ...interface{})

Infof takes a format-string and calls Info

func LLvl1

func LLvl1(args ...interface{})

LLvl1 *always* prints

Example
OutputToOs()
Lvl1("Lvl output")
LLvl1("LLvl output")
Lvlf1("Lvlf output")
LLvlf1("LLvlf output")
OutputToBuf()
Output:

1 : fake_name.go:0 (log.ExampleLLvl1)        - Lvl output
1!: fake_name.go:0 (log.ExampleLLvl1)        - LLvl output
1 : fake_name.go:0 (log.ExampleLLvl1)        - Lvlf output
1!: fake_name.go:0 (log.ExampleLLvl1)        - LLvlf output

func LLvl2

func LLvl2(args ...interface{})

LLvl2 *always* prints

func LLvl3

func LLvl3(args ...interface{})

LLvl3 *always* prints

func LLvl4

func LLvl4(args ...interface{})

LLvl4 *always* prints

func LLvl5

func LLvl5(args ...interface{})

LLvl5 *always* prints

func LLvlf1

func LLvlf1(f string, args ...interface{})

LLvlf1 *always* prints

func LLvlf2

func LLvlf2(f string, args ...interface{})

LLvlf2 *always* prints

func LLvlf3

func LLvlf3(f string, args ...interface{})

LLvlf3 *always* prints

func LLvlf4

func LLvlf4(f string, args ...interface{})

LLvlf4 *always* prints

func LLvlf5

func LLvlf5(f string, args ...interface{})

LLvlf5 *always* prints

func Lvl1

func Lvl1(args ...interface{})

Lvl1 debug output is informational and always displayed

Example
OutputToOs()
Lvl1("Multiple", "parameters")
OutputToBuf()
Output:

1 : fake_name.go:0 (log.ExampleLvl1)         - Multiple parameters

func Lvl2

func Lvl2(args ...interface{})

Lvl2 is more verbose but doesn't spam the stdout in case there is a big simulation

Example
SetDebugVisible(2)
OutputToOs()
Lvl1("Level1")
Lvl2("Level2")
Lvl3("Level3")
Lvl4("Level4")
Lvl5("Level5")
OutputToBuf()
SetDebugVisible(1)
Output:

1 : fake_name.go:0 (log.ExampleLvl2)         - Level1
2 : fake_name.go:0 (log.ExampleLvl2)         - Level2

func Lvl3

func Lvl3(args ...interface{})

Lvl3 gives debug-output that can make it difficult to read for big simulations with more than 100 hosts

Example
NamePadding = -1
OutputToOs()
Lvl1("Before")
thisIsAVeryLongFunctionNameThatWillOverflow()
Lvl1("After")
OutputToBuf()
Output:

1 : fake_name.go:0 (log.ExampleLvl3) - Before
1 : fake_name.go:0 (log.thisIsAVeryLongFunctionNameThatWillOverflow) - Overflow
1 : fake_name.go:0 (log.ExampleLvl3) - After

func Lvl4

func Lvl4(args ...interface{})

Lvl4 is only good for test-runs with very limited output

func Lvl5

func Lvl5(args ...interface{})

Lvl5 is for big data

func Lvlf1

func Lvlf1(f string, args ...interface{})

Lvlf1 is like Lvl1 but with a format-string

Example
OutputToOs()
Lvl1("Before")
thisIsAVeryLongFunctionNameThatWillOverflow()
Lvl1("After")
OutputToBuf()
Output:

1 : fake_name.go:0 (log.ExampleLvlf1)        - Before
1 : fake_name.go:0 (log.thisIsAVeryLongFunctionNameThatWillOverflow) - Overflow
1 : fake_name.go:0 (log.ExampleLvlf1)        - After

func Lvlf2

func Lvlf2(f string, args ...interface{})

Lvlf2 is like Lvl2 but with a format-string

func Lvlf3

func Lvlf3(f string, args ...interface{})

Lvlf3 is like Lvl3 but with a format-string

func Lvlf4

func Lvlf4(f string, args ...interface{})

Lvlf4 is like Lvl4 but with a format-string

func Lvlf5

func Lvlf5(f string, args ...interface{})

Lvlf5 is like Lvl5 but with a format-string

func MainTest

func MainTest(m *testing.M, ls ...int)

MainTest can be called from TestMain. It will parse the flags and set the DebugVisible to defaultMainTest, then run the tests and check for remaining go-routines. For the debug-level, the following heuristic is used to set the debug-level:

  • the first ls argument given, else
  • from the environment variable, else
  • defaultMainTest (2)

Also be aware that go only outputs the stdout of the tests if the verbose flag is given.

func OutputToBuf

func OutputToBuf()

OutputToBuf is called for sending all the log.*-outputs to internal buffers that can be used for checking what the logger would've written. This is mostly used for tests. The buffers are zeroed after this call.

func OutputToOs

func OutputToOs()

OutputToOs redirects the output of the log.*-outputs again to the os.

func Padding

func Padding() bool

Padding returns the actual setting of the padding in log

func Panic

func Panic(args ...interface{})

Panic prints out the panic message and panics

func Panicf

func Panicf(f string, args ...interface{})

Panicf is like Panic but with a format-string

func ParseEnv

func ParseEnv()

ParseEnv looks at the following environment-variables:

DEBUG_LVL - for the actual debug-lvl - default is 1
DEBUG_TIME - whether to show the timestamp - default is false
DEBUG_COLOR - whether to color the output - default is false
DEBUG_PADDING - whether to pad the output nicely - default is true

func Print

func Print(args ...interface{})

Print directly sends the arguments to the stdout

func Printf

func Printf(f string, args ...interface{})

Printf is like Print but takes a formatting-argument first

func RegisterFlags

func RegisterFlags()

RegisterFlags adds the flags and the variables for the debug-control (standard logger) using the standard flag-package.

func RegisterLogger

func RegisterLogger(l Logger) int

RegisterLogger will register a callback that will receive a copy of every message, fully formatted. It returns the key assigned to the logger (used to unregister the logger).

func SetAbsoluteFilePath

func SetAbsoluteFilePath(absolute bool)

SetAbsoluteFilePath allows to print the absolute file path for logging calls.

func SetDebugVisible

func SetDebugVisible(lvl int)

SetDebugVisible set the global debug output level in a go-rountine-safe way

func SetPadding

func SetPadding(padding bool)

SetPadding can turn off or turn on the use of padding in log

func SetShowTime

func SetShowTime(show bool)

SetShowTime allows for turning on the flag that adds the current time to the debug-output

func SetUseColors

func SetUseColors(useColors bool)

SetUseColors can turn off or turn on the use of colors in the debug-output

func ShowTime

func ShowTime() bool

ShowTime returns the current setting for showing the time in the debug output

func Stack

func Stack() string

Stack converts []byte to string

func TestOutput

func TestOutput(show bool, level int)

TestOutput sets the DebugVisible to 0 if 'show' is false, else it will set DebugVisible to 'level'

Usage: TestOutput( test.Verbose(), 2 ) DEPRECATED: write your own if/else

func TraceID

func TraceID(id []byte)

TraceID helps the tracing-simulation to know which trace it should attach to. The TraceID will be stored with the corresponding go-routine, and any new go-routines spawned from the method will be attached to the same TraceID.

func UnregisterLogger

func UnregisterLogger(key int)

UnregisterLogger takes the key it was assigned and returned by 'RegisterLogger', closes the corresponding Logger and removes it from the loggers.

func UseColors

func UseColors() bool

UseColors returns the actual setting of the color-usage in log

func Warn

func Warn(args ...interface{})

Warn prints out the warning message and quits

func Warnf

func Warnf(f string, args ...interface{})

Warnf is like Warn but with a format-string

Types

type Logger

type Logger interface {
	Log(level int, msg string)
	Close()
	GetLoggerInfo() *LoggerInfo
}

Logger is the interface that specifies how loggers will receive and display messages.

func NewFileLogger

func NewFileLogger(lInfo *LoggerInfo, path string) (Logger, error)

NewFileLogger creates a logger that writes into the file with the given path and is using the given LoggerInfo. It returns the logger.

func NewSyslogLogger

func NewSyslogLogger(lInfo *LoggerInfo, priority syslog.Priority, tag string) (Logger, error)

NewSyslogLogger creates a logger that writes into syslog with the given priority and tag, and is using the given LoggerInfo (without the Logger). It returns the logger.

type LoggerInfo

type LoggerInfo struct {
	// These are information-debugging levels that can be turned on or off.
	// Every logging greater than 'debugLvl' will be discarded . So you can
	// Log at different levels and easily turn on or off the amount of logging
	// generated by adjusting the 'debugLvl' variable.
	DebugLvl int
	// If 'showTime' is true, it will print the time for each line displayed
	// by the logger.
	ShowTime bool
	// If 'AbsoluteFilePath' is true, it will print the absolute file path
	// instead of the base.
	AbsoluteFilePath bool
	// If 'useColors' is true, logs will be colored (defaults to monochrome
	// output). It also controls padding, since colorful output is higly
	// correlated with humans who like their log lines padded.
	UseColors bool
	// If 'padding' is true, it will nicely pad the line that is written.
	Padding bool
	// If true, it will only send the raw message to the logger
	RawMessage bool
}

LoggerInfo is a structure that should be used when creating a logger. It contains parameters about how to log (with time, colors, ...) and embeds the Logger interface, which should define how the logger should log.

type Tracer

type Tracer interface {
	TraceID(id []byte)
}

Tracer is an additional interface that specifies a tracer extension to onet/log.

Jump to

Keyboard shortcuts

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