Documentation ¶
Overview ¶
Package log is a simple colored info and errors logger.
Errors will be displayed only if they are valid, so you can send all your errors without having to bother if they are filled or not. You just have to use it with all errors you want to be displayed and sort the others.
98% code coverage in examples.
Example ¶
Example of a very common case of chained tests in go:
package main import ( "errors" "github.com/sqp/godock/libs/cdtype" "github.com/sqp/godock/libs/log" ) func NewTestLogger(hist cdtype.LogOut) *log.Log { logger := log.NewLog(hist) logger.SetName("test") logger.SetTimeFormat("") logger.SetColorName(nocolor) logger.SetColorInfo(nocolor) logger.SetColorDebug(nocolor) logger.SetColorDEV(nocolor) logger.SetColorWarn(nocolor) logger.SetColorError(nocolor) return logger } func nocolor(str string) string { return str } func main() { logger := NewTestLogger(log.Logs) // The logger with its common history. testChain := func(isErrGet, isErrParse, isErrUse bool) error { data, e := GetSomeData(isErrGet) if logger.Err(e, "Get data") { // when we need to keep or forward the error. return e } parsed, e := ParseMyData(data, isErrParse) logger.Err(e, "Parse data", "(don't block)") // when we just want to output it. result, e := UseData(parsed, isErrParse, isErrUse) if logger.Err(e, "Use data") { // used as a simple test. return e } logger.Info("Data used", result) return nil } for _, test := range []struct { isErrGet, isErrParse, isErrUse bool }{ {true, false, false}, {false, true, false}, {false, false, true}, {false, false, false}, } { testChain(test.isErrGet, test.isErrParse, test.isErrUse) } } func GetSomeData(isErrGet bool) (string, error) { if isErrGet { return "", errors.New("get fail") } return "get success", nil } func ParseMyData(data string, isErrParse bool) (string, error) { if isErrParse { return "", errors.New("parse fail") } return "parse success", nil } func UseData(parsed string, isErrParse, isErrUse bool) (string, error) { if isErrUse { return "", errors.New("use fail") } if isErrParse { return "have half data", nil } return "everything is fine", nil }
Output: test [error] Get data : get fail test [error] Parse data (don't block) : parse fail test [Data used] have half data test [error] Use data : use fail test [Data used] everything is fine
Index ¶
- Variables
- type Fmt
- func (f *Fmt) Format(colfunc func(string) string, sender, msg string, more ...interface{}) string
- func (f *Fmt) FormatErr(e error, level cdtype.LogLevel, sender string, msg ...interface{}) string
- func (f *Fmt) FormatMsg(level cdtype.LogLevel, sender string, msg string, more ...interface{}) string
- func (f *Fmt) LevelColor(level cdtype.LogLevel) func(string) string
- func (f *Fmt) SetColorDEV(callFormat func(string) string)
- func (f *Fmt) SetColorDebug(callFormat func(string) string)
- func (f *Fmt) SetColorError(callFormat func(string) string)
- func (f *Fmt) SetColorInfo(callFormat func(string) string)
- func (f *Fmt) SetColorName(callFormat func(string) string)
- func (f *Fmt) SetColorWarn(callFormat func(string) string)
- func (f *Fmt) SetTimeFormat(format string)
- type History
- func (hist *History) Err(e error, level cdtype.LogLevel, sender, msg string)
- func (hist *History) List() []cdtype.LogMsg
- func (hist *History) Msg(level cdtype.LogLevel, sender, msg string, more ...interface{})
- func (hist *History) Raw(sender, msg string)
- func (hist *History) SetDelay(d time.Duration)
- func (hist *History) SetTerminal(f feeder)
- func (hist *History) Write(p []byte) (n int, err error)
- type Log
- func (l *Log) DEV(msg string, more ...interface{})
- func (l *Log) Debug(msg string, more ...interface{})
- func (l *Log) Debugf(title, format string, args ...interface{})
- func (l *Log) Err(e error, args ...interface{}) (fail bool)
- func (l *Log) Errorf(msg, format string, args ...interface{})
- func (l *Log) ExecAsync(command string, args ...string) error
- func (l *Log) ExecCmd(command string, args ...string) *exec.Cmd
- func (l *Log) ExecShlex(command string, args ...string) (*exec.Cmd, error)
- func (l *Log) ExecShow(command string, args ...string) error
- func (l *Log) ExecSync(command string, args ...string) (string, error)
- func (l *Log) GetDebug() bool
- func (l *Log) GoTry(call func())
- func (l *Log) Info(msg string, more ...interface{})
- func (l *Log) Infof(msg, format string, args ...interface{})
- func (l *Log) LogOut() cdtype.LogOut
- func (l *Log) NewErr(msg string, args ...interface{})
- func (l *Log) NewWarn(msg string, args ...interface{})
- func (l *Log) PlaySound(soundFile string) error
- func (l *Log) Recover()
- func (l *Log) SetDebug(debug bool) cdtype.Logger
- func (l *Log) SetLogOut(out cdtype.LogOut) cdtype.Logger
- func (l *Log) SetName(name string) cdtype.Logger
- func (l *Log) Warn(e error, args ...interface{}) (fail bool)
- func (l *Log) Warnf(msg, format string, args ...interface{})
- func (l *Log) Write(p []byte) (n int, err error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var CmdPlaySound = [][]string{
{"paplay", "--client-name=cairo-dock"},
{"aplay"},
{"play"},
}
CmdPlaySound defines simple sound players command and args.
var Logs = NewHistory()
Logs provides a default history logger.
Functions ¶
This section is empty.
Types ¶
type Fmt ¶
type Fmt struct {
// contains filtered or unexported fields
}
Fmt formats log messages.
func (*Fmt) FormatMsg ¶
func (f *Fmt) FormatMsg(level cdtype.LogLevel, sender string, msg string, more ...interface{}) string
FormatMsg returns a formatted standard message with endline.
func (*Fmt) LevelColor ¶
LevelColor returns the field color formater for the level.
func (*Fmt) SetColorDEV ¶
SetColorDEV sets the formater used to color the message as third log argument.
func (*Fmt) SetColorDebug ¶
SetColorDebug sets the formater used to color the message as third log argument.
func (*Fmt) SetColorError ¶
SetColorError sets the formater used to color the message as third log argument.
func (*Fmt) SetColorInfo ¶
SetColorInfo sets the formater used to color the message as third log argument.
func (*Fmt) SetColorName ¶
SetColorName sets the formater used to display the sender as second log argument.
func (*Fmt) SetColorWarn ¶
SetColorWarn sets the formater used to color the message as third log argument.
func (*Fmt) SetTimeFormat ¶
SetTimeFormat sets the time format displayed as first log argument.
type History ¶
type History struct { Fmt // extends the formater // contains filtered or unexported fields }
History provides an history for the Log system.
func NewHistory ¶
func NewHistory(optionalFeeder ...feeder) *History
NewHistory creates a logging history with an optional forwarder.
func (*History) SetTerminal ¶
func (hist *History) SetTerminal(f feeder)
SetTerminal sets the optional terminal forwarder.
type Log ¶
Log is a simple colored info and errors logger.
func (*Log) Debug ¶
Debug is to be used every time a useful step is reached in your module activity. It will display the flood to the user only when the debug flag is enabled.
func (*Log) ExecAsync ¶
ExecAsync run a command with output forwarded to console but don't wait for its completion. Errors will be logged.
func (*Log) GoTry ¶
func (l *Log) GoTry(call func())
GoTry launch a secured go routine. Panic will be recovered and logged.
func (*Log) Info ¶
Info displays normal informations on the standard output, with the first param in green.
func (*Log) Recover ¶
func (l *Log) Recover()
Recover from crash. Use with defer before a dangerous action.
func (*Log) SetDebug ¶
SetDebug change the debug state of the logger. Only enable or disable messages send with the Debug command.