Documentation ¶
Overview ¶
Package goose is a simple golang log package for debug purposes.
The idea is very simple. You create variables of type goose.Alert and set its debug level. We suggest you set these variables to level 0 when you don't want any log messages at all. Set to level 1 to log error messages. Set to level 2 or above for increasing levels of log verbosity.
In all the above methods the level parameter determines the log level of the message. The message will be actually emited only if, when the method is called, the log level is equal or higher than the message's log level. An empty string is returned by the Sprintf method if called with a message log level higher than the current log level.
Example ¶
In your initialization code, you may set the log levels of debug messages:
... reptilian.Goose = goose.Alert(1) // Only error messages will be logged reptext.Goose = goose.Alert(0) // No debug messages at all djparser.Goose = goose.Alert(4) // Error messages and less verbose messages (levels 2~4) will be logged ...
In the package to be debugged:
import ( ... "github.com/luisfurquim/goose" ... ) ... var Goose goose.Alert // Exported symbol needed only if you want to allow external control of the debug level ... // throughout your code you may log anything you want at varying levels of importance Goose.Logf(3, "Final Off=%d (%o)", d.Buf.Off, d.Buf.Off) ... Goose.Logf(7, "Index: %#v", d.Index) // Logs will be actually printed only if the first parameter (the message's log level) is lower or equal than the current log level indicated by the Goose variable. Remember to never use the zero value, like Goose.Logf(0,...), as we want to make the log level 0 to print no debug messages at all.
You may set multiple loggers if you need finer control on the verbosity:
type T1 struct { ... } type T2 struct { ... } var GooseT1 goose.Alert var GooseT2 goose.Alert ... GooseT1 = goose.Alert(1) // Only error messages will be logged GooseT2 = goose.Alert(3) // More verbosity... ... GooseT1.Logf(2, "Final Off=%d (%o)", d.Buf.Off, d.Buf.Off) // not printed ... GooseT2.Logf(2, "Index: %#v", d.Index) // printed
Tracing ¶
You may add/remove source code reference by enabling/disabling trace. With trace enabled Goose automatically adds source code reference in the format {package}[source filename]<function/method>(line number).
Check it with the following code:
Goose = goose.Alert(1) Goose.Logf(1,"no trace") goose.TraceOn() Goose.Logf(1,"trace") goose.TraceOff() Goose.Logf(1,"no trace") // Outputs: // 2015/07/28 09:42:30 no trace // 2015/07/28 09:42:30 {main}[teste-goose.go]<main>(14): trace // 2015/07/28 09:42:30 no trace
Redirecting output ¶
You may redirect the output to the syslogger just calling UseSyslogNet:
goose.UseSyslogNet("tcp", "myloghost.mydomain:514", syslog.LOG_ERR|syslog.LOG_LOCAL7)
Index ¶
- Variables
- func TraceOff()
- func TraceOn()
- func UseSyslogNet(proto string, srv string, priority syslog.Priority) error
- type Alert
- func (d Alert) DeepFatalf(stacklevel, level int, format string, parms ...interface{})
- func (d Alert) DeepLogf(stacklevel, level int, format string, parms ...interface{})
- func (d Alert) DeepPrintf(stacklevel, level int, format string, parms ...interface{})
- func (d Alert) DeepSprintf(stacklevel, level int, format string, parms ...interface{}) string
- func (d Alert) Fatalf(level int, format string, parms ...interface{})
- func (d Alert) Logf(level int, format string, parms ...interface{})
- func (d Alert) Printf(level int, format string, parms ...interface{})
- func (d *Alert) Set(level interface{})
- func (d Alert) Sprintf(level int, format string, parms ...interface{}) string
- type Geese
- type SyslogGoose
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func TraceOff ¶
func TraceOff()
TraceOff disables the inclusion of the package name, source file name, method/function caller and source line number in the log messages. This is the default state of logging.
func TraceOn ¶
func TraceOn()
TraceOn enables the inclusion of the package name, source file name, method/function caller and source line number in the log messages. As stated in https://golang.org/pkg/runtime/#Func.FileLine about the line numbering, "The result will not be accurate if pc is not a program counter within f".
Types ¶
type Alert ¶
type Alert uint8
Alert is the basic type that implements the goose log object
func (Alert) DeepFatalf ¶
Fatalf behaves as Logf but stops program execution. The execution ends EVEN WHEN the log level of the message is higher the the current log level.
func (Alert) DeepPrintf ¶
Logf emits the messages based on fmt.Printf
func (Alert) DeepSprintf ¶
Sprintf returns the messages as a string value
func (Alert) Fatalf ¶
Fatalf behaves as Logf but stops program execution. The execution ends EVEN WHEN the log level of the message is higher the the current log level.
type SyslogGoose ¶
SyslogGoose is a wrapper type around *syslog.Writer to ensure a syslogger that satisfies the io.Writer inrterface