Documentation ¶
Overview ¶
Package elog extends Go's built-in log package to enable levelled logging with improved formatting.
Methods ¶
Inherited from the log package (https://golang.org/pkg/log):
func (l *Logger) Print(v ...interface{}) func (l *Logger) Printf(format string, v ...interface{}) func (l *Logger) Println(v ...interface{}) func (l *Logger) Output(calldepth int, s string) error func (l *Logger) Fatal(v ...interface{}) func (l *Logger) Fatalf(format string, v ...interface{}) func (l *Logger) Fatalln(v ...interface{}) func (l *Logger) Panic(v ...interface{}) func (l *Logger) Panicf(format string, v ...interface{}) func (l *Logger) Panicln(v ...interface{})
Extensions:
func (l *Logger) Debug(a ...interface{}) func (l *Logger) Debugf(format string, a ...interface{}) func (l *Logger) Debugln(a ...interface{}) func (l *Logger) DebugOutput(calldepth int, s string) func (l *Logger) Errorf(format string, a ...interface{}) error
Index ¶
Constants ¶
const ( NoTrace = 1 // no file name or line lumber LongFile = log.Llongfile // absolute file path and line number: /home/user/go/main.go:15 ShortFile = log.Lshortfile // file name and line number: main.go:15 )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct { // Writer is the destination to which log data is written // Default: os.Stdout Writer io.Writer // TimeFormat is the format of time in the prefix (compatible with https://golang.org/pkg/time/#pkg-constants) // Default: "2006/01/02 15:04:05" TimeFormat string // Trace is to control the tracing information // Default: NoTrace Trace int // DebugEnabled can be used to pass a parsed command-line boolean flag to enable the debugging // Default: nil DebugEnabled *bool // DebugEnvVar is the environment variable used to enable debugging when set to 1 // Default: "DEBUG" DebugEnvVar string // DebugPrefix is the prefix used when logging with Debug methods and Errorf // Default: "[debug] " DebugPrefix string // DebugTrace is to control the tracing information when in debugging mode // Default: ShortFile DebugTrace int }
type Logger ¶
func New ¶
New creates a new Logger. The prefix variable is the prefix used when logging with Print, Fatal, and Panic methods. The config variable is a struct with optional fields.
When config is set to nil, the default configuration will be used.
func (*Logger) Debug ¶
func (l *Logger) Debug(a ...interface{})
Debug has similar arguments to those of fmt.Print. It prints to the logger only when debugging is enabled.
func (*Logger) DebugOutput ¶
DebugOutput is similar to Debug but allows control over the scope of tracing. (log.Output for debugging)
E.g. :
[main.go] 10 apiError("error message") 11 12 // A function to log formatted API errors 13 func apiError(s string){ 14 logger.DebugOutput(2, "API Error:", s) 15 } Prints: 2016/07/19 17:34:10 [debug] main.go:10: API Error: error message
func (*Logger) Debugf ¶
Debugf has similar arguments to those of fmt.Printf. It prints to the logger only when debugging is enabled.
func (*Logger) Debugln ¶
func (l *Logger) Debugln(a ...interface{})
Debugln has similar arguments to those of fmt.Println. It prints to the logger only when debugging is enabled.
func (*Logger) Errorf ¶
Errorf has similar arguments and signature to those of fmt.Errorf. In addition to formatting and returning error, Errorf prints the error message to the logger when debugging is enabled.
E.g. :
[example.go] 19 func DoSomething() error { 20 err, v := divide(10, 0) 21 if err != nil { 22 return logger.Errorf("Division error: %s", err) 23 } 24 25 return nil 24 } Prints: 2016/07/19 17:44:22 [debug] example.go:22: Division error: cannot divide by zero