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 ¶
- Constants
- Variables
- func AbsoluteFilePath() bool
- func AddUserUninterestingGoroutine(newGr string)
- func AfterTest(t *testing.T)
- func DebugVisible() int
- func ErrFatal(err error, args ...interface{})
- func ErrFatalf(err error, f string, args ...interface{})
- func Error(args ...interface{})
- func Errorf(f string, args ...interface{})
- func Fatal(args ...interface{})
- func Fatalf(f string, args ...interface{})
- func GetStdErr() string
- func GetStdOut() string
- func Info(args ...interface{})
- func Infof(f string, args ...interface{})
- func LLvl1(args ...interface{})
- func LLvl2(args ...interface{})
- func LLvl3(args ...interface{})
- func LLvl4(args ...interface{})
- func LLvl5(args ...interface{})
- func LLvlf1(f string, args ...interface{})
- func LLvlf2(f string, args ...interface{})
- func LLvlf3(f string, args ...interface{})
- func LLvlf4(f string, args ...interface{})
- func LLvlf5(f string, args ...interface{})
- func Lvl1(args ...interface{})
- func Lvl2(args ...interface{})
- func Lvl3(args ...interface{})
- func Lvl4(args ...interface{})
- func Lvl5(args ...interface{})
- func Lvlf1(f string, args ...interface{})
- func Lvlf2(f string, args ...interface{})
- func Lvlf3(f string, args ...interface{})
- func Lvlf4(f string, args ...interface{})
- func Lvlf5(f string, args ...interface{})
- func MainTest(m *testing.M, ls ...int)
- func OutputToBuf()
- func OutputToOs()
- func Padding() bool
- func Panic(args ...interface{})
- func Panicf(f string, args ...interface{})
- func ParseEnv()
- func Print(args ...interface{})
- func Printf(f string, args ...interface{})
- func RegisterFlags()
- func RegisterLogger(l Logger) int
- func SetAbsoluteFilePath(absolute bool)
- func SetDebugVisible(lvl int)
- func SetPadding(padding bool)
- func SetShowTime(show bool)
- func SetUseColors(useColors bool)
- func ShowTime() bool
- func Stack() string
- func TestOutput(show bool, level int)
- func TraceID(id []byte)
- func UnregisterLogger(key int)
- func UseColors() bool
- func Warn(args ...interface{})
- func Warnf(f string, args ...interface{})
- type Logger
- type LoggerInfo
- type Tracer
Examples ¶
Constants ¶
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 )
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 ¶
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.
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.
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.
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 ¶
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 ErrFatal ¶
func ErrFatal(err error, args ...interface{})
ErrFatal calls log.Fatal in the case err != 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 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 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 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 MainTest ¶
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 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 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 ¶
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 TestOutput ¶
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.
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 ¶
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.