Documentation ¶
Overview ¶
Package errlog provides a simple object to enhance Go source code debugging
Example result:
$ go run myfailingapp.go Program starting error in main.main: something failed here line 13 of /Users/snwfdhmp/go/src/github.com/snwfdhmp/sandbox/testerr.go 9: func main() { 10: fmt.Println("Program starting") 11: err := errors.New("something failed here") 12: 13: errlog.Debug(err) 14: 15: fmt.Println("End of the program") 16: } exit status 1
You can configure your own logger with these options :
type Config struct { LinesBefore int LinesAfter int PrintStack bool PrintSource bool PrintError bool ExitOnDebugSuccess bool }
Example :
debug := errlog.NewLogger(&errlog.Config{ LinesBefore: 2, LinesAfter: 1, PrintError: true, PrintSource: true, PrintStack: false, ExitOnDebugSuccess: true, }) // ... if err != nil { debug.Debug(err) return }
Outputs :
Error in main.someBigFunction(): I'm failing for no reason line 41 of /Users/snwfdhmp/go/src/github.com/snwfdhmp/sandbox/testerr.go:41 33: func someBigFunction() { ... 40: if err := someNastyFunction(); err != nil { 41: debug.Debug(err) 42: return 43: } exit status 1
Index ¶
Constants ¶
View Source
const ( // ModeDisabled represents the disabled mode (NO-OP) ModeDisabled = iota + 1 // ModeEnabled represents the enabled mode (Print) ModeEnabled )
Variables ¶
View Source
var ( //DefaultLoggerPrintFunc is log.Printf without return values DefaultLoggerPrintFunc = func(format string, data ...interface{}) { log.Printf(format+"\n", data...) } //DefaultLoggerPrintFunc is log.Printf without return values DefaultLoggerPrintlnFunc = func(data ...interface{}) { log.Println(data...) } //DefaultLogger logger implements default configuration for a logger DefaultLogger = &logger{ config: &Config{ PrintFunc: DefaultLoggerPrintFunc, PrintlnFunc: DefaultLoggerPrintlnFunc, LinesBefore: 4, LinesAfter: 2, PrintStack: false, PrintSource: true, PrintError: true, ExitOnDebugSuccess: false, }, } )
Functions ¶
func PrintStackMinus ¶
func PrintStackMinus(depthToRemove int)
PrintStackMinus prints the current stack trace minus the amount of depth in parameter
func SetDebugMode ¶
func SetDebugMode(toggle bool)
SetDebugMode sets debug mode to On if toggle==true or Off if toggle==false. It changes log level an so displays more logs about whats happening. Useful for debugging.
Types ¶
type Config ¶
type Config struct { PrintFunc func(format string, data ...interface{}) //Printer func (eg: fmt.Printf) PrintlnFunc func(data ...interface{}) //Printer func (eg: fmt.Printf) LinesBefore int //How many lines to print *before* the error line when printing source code LinesAfter int //How many lines to print *after* the error line when printing source code PrintStack bool //Shall we print stack trace ? yes/no PrintSource bool //Shall we print source code along ? yes/no PrintError bool //Shall we print the error of Debug(err) ? yes/no ExitOnDebugSuccess bool //Shall we os.Exit(1) after Debug has finished logging everything ? (doesn't happen when err is nil) DisableStackIndentation bool //Shall we print stack vertically instead of indented Mode int }
Config holds the configuration for a logger
type Logger ¶
type Logger interface { // Debug wraps up Logger debugging funcs related to an error // If the given error is nil, it returns immediately // It relies on Logger.Config to determine what will be printed or executed // It returns whether err != nil Debug(err error) bool //PrintSource prints lines based on given opts (see PrintSourceOptions type definition) PrintSource(lines []string, opts PrintSourceOptions) //DebugSource debugs a source file DebugSource(filename string, lineNumber int) //SetConfig replaces current config with the given one SetConfig(cfg *Config) //Config returns current config Config() *Config //Disable is used to disable Logger (every call to this Logger will perform NO-OP (no operation)) and return instantly //Use Disable(true) to disable and Disable(false) to enable again Disable(bool) }
Logger interface allows to log an error, or to print source code lines. Check out NewLogger function to learn more about Logger objects and Config.
Click to show internal directories.
Click to hide internal directories.