Documentation ¶
Overview ¶
Package logger delivers a tiered loggin mechanics. It is a thin wrapper above the standard library's log package. Its main purpose is to give the developer separate log streams and logging severity levels. There are 3 types of streams: debug, log and error and 4 levels of logging: LevelDebug, LevelLog, LevelError and LevelNoLog.
Users of the package will interact with its Logger type. It supports the Debug(f|ln)?, Log(f|ln)?, Error(f|ln)? and Fatal(f|ln)? functions which handle their arguments in the way fmt.Print(f|ln)? do.
Every Logger has its own log level. See the constants section for information how to use this level.
For fine tuning loggers users will need to import the log package and use the Logger.Debugger, Logger.Logger and Logger.Errorer which are just instances of log.Logger.
The package supplies a default logger and a shorthand functions for using it. The default logger is created autmatically with its debug, log and error streams using stdout, stound and stderr respectively. Its logging level is set to LevelLog.
Index ¶
- Constants
- func Debug(v ...interface{})
- func Debugf(format string, args ...interface{})
- func Debugln(v ...interface{})
- func Error(v ...interface{})
- func Errorf(format string, args ...interface{})
- func Errorln(v ...interface{})
- func Fatal(v ...interface{})
- func Fatalf(format string, args ...interface{})
- func Fatalln(v ...interface{})
- func Log(v ...interface{})
- func Logf(format string, args ...interface{})
- func Logln(v ...interface{})
- func SetDebugOutput(w io.Writer)
- func SetErrorOutput(w io.Writer)
- func SetLevel(level int)
- func SetLogOutput(w io.Writer)
- type Logger
- func (l *Logger) Debug(v ...interface{})
- func (l *Logger) Debugf(format string, args ...interface{})
- func (l *Logger) Debugln(v ...interface{})
- func (l *Logger) Error(v ...interface{})
- func (l *Logger) Errorf(format string, args ...interface{})
- func (l *Logger) Errorln(v ...interface{})
- func (l *Logger) Fatal(v ...interface{})
- func (l *Logger) Fatalf(format string, v ...interface{})
- func (l *Logger) Fatalln(v ...interface{})
- func (l *Logger) Log(v ...interface{})
- func (l *Logger) Logf(format string, args ...interface{})
- func (l *Logger) Logln(v ...interface{})
- func (l *Logger) SetDebugOutput(w io.Writer)
- func (l *Logger) SetErrorOutput(w io.Writer)
- func (l *Logger) SetLogOutput(w io.Writer)
Examples ¶
Constants ¶
const ( LevelDebug = iota // Will show all messages LevelLog // Will show error and log messages LevelError // Will show error messages only LevelNoLog // No logs will be emitted whatsoever )
These constants are used to configer a Logger's output level. They are ordered in a strict ascending order. If the level of a logger is set at particular constant it will only emit messages in the streams for this constant's level and above.
For example if a logger has its Level set to LevelLog it will emit messages in its log and error streams but will not emit anything in the debug stream.
A special level LevelNoLog may be used in order to silence the logger completely.
Variables ¶
This section is empty.
Functions ¶
func Debug ¶
func Debug(v ...interface{})
Debug calls the default logger's Debug function. Arguments are handled in the manner of fmt.Print.
func Debugf ¶
func Debugf(format string, args ...interface{})
Debugf calls the default logger's Debugf function. Arguments are handled in the manner of fmt.Printf.
func Debugln ¶
func Debugln(v ...interface{})
Debugln calls the default logger's Debugln function. Arguments are handled in the manner of fmt.Println.
func Error ¶
func Error(v ...interface{})
Error calls the default logger's Error function. Arguments are handled in the manner of fmt.Print.
func Errorf ¶
func Errorf(format string, args ...interface{})
Errorf calls the default logger's Errorf function. Arguments are handled in the manner of fmt.Printf.
func Errorln ¶
func Errorln(v ...interface{})
Errorln calls the default logger's Errorln function. Arguments are handled in the manner of fmt.Println.
func Fatal ¶
func Fatal(v ...interface{})
Fatal calls the default logger's Fatal function. Arguments are handled in the manner of fmt.Print.
func Fatalf ¶
func Fatalf(format string, args ...interface{})
Fatalf calls the default logger's Fatalf function. Arguments are handled in the manner of fmt.Printf.
func Fatalln ¶
func Fatalln(v ...interface{})
Fatalln calls the default logger's Fatalln function. Arguments are handled in the manner of fmt.Println.
func Log ¶
func Log(v ...interface{})
Log calls the default logger's Log function. Arguments are handled in the manner of fmt.Print.
func Logf ¶
func Logf(format string, args ...interface{})
Logf calls the default logger's Logf function. Arguments are handled in the manner of fmt.Printf.
func Logln ¶
func Logln(v ...interface{})
Logln calls the default logger's Logln function. Arguments are handled in the manner of fmt.Println.
func SetDebugOutput ¶
SetDebugOutput replaces the debug stream output of the default logger.
func SetErrorOutput ¶
SetErrorOutput replaces the error stream output of the default logger.
func SetLogOutput ¶
SetLogOutput replaces the log stream output of the default logger.
Types ¶
type Logger ¶
type Logger struct { /* The debug stream. It is a pointer to the underlying log.Logger strcture which is used when Debug, Debugf and Debugln are called. */ Debugger *log.Logger /* The log stream. Used by Log, Logf and Logln. */ Logger *log.Logger /* The error stream. Used by Error, Errorf and Errorln. */ Errorer *log.Logger /* Which Log Level this Logger is using. See the descrption of the log level constants to understend what this means. */ Level int }
Logger is a type which actually consistes of 3 log.Logger object. One for every stream - debug, log and error.
Example ¶
package main import ( "github.com/ironsmile/logger" ) func main() { // Create a new logger lg := logger.New() // Using the underlying standard's library log.Logger instances // to reconfiger the debug stream. lg.Debugger.SetPrefix("") lg.Debugger.SetFlags(0) // Now all types of messages will be outputed. lg.Level = logger.LevelDebug // Emit something to the debug stream lg.Debugf("debug yourself and you will be %d time stronger", 42) }
Output: debug yourself and you will be 42 time stronger
Example (Configuration) ¶
package main import ( "os" "github.com/ironsmile/logger" ) func main() { lg := logger.New() // Increase this logger's verbosity lg.Level = logger.LevelDebug // Lets be sure all error messages have the '[ERROR] ' string in front of them lg.Errorer.SetPrefix("[ERROR] ") // We do not want anything in the message except our prefix. lg.Errorer.SetFlags(0) // We want errors to be seen in the standard output. lg.SetErrorOutput(os.Stdout) // Emit something to the error stream lg.Error("error to stdout") }
Output: [ERROR] error to stdout
func Default ¶
func Default() *Logger
Default returns a pointer to the default Logger. Using it users can configure the behaviour of it.
Example ¶
package main import ( "os" "github.com/ironsmile/logger" ) func main() { // Reconfiguring the default logger logger.Default().Debugger.SetFlags(0) logger.Default().Debugger.SetPrefix("level.debug: ") // Setting its level logger.SetLevel(logger.LevelDebug) // Setting its output destination logger.SetDebugOutput(os.Stdout) // Actually printing something in the output destination logger.Debugln("the default may be more than you need") }
Output: level.debug: the default may be more than you need
func New ¶
func New() *Logger
New creates and returns a new Logger. Its debug and log streams are stdout and its error stream is stderr. The returned logger will have log level set to LevelLog.
func (*Logger) Debug ¶
func (l *Logger) Debug(v ...interface{})
Debug emits this message to the debug stream.
func (*Logger) Debugf ¶
Debugf emits this message to the debug stream. Supports fmt.Printf formatting.
func (*Logger) Debugln ¶
func (l *Logger) Debugln(v ...interface{})
Debugln emits this message to the debug streamand adds a new line at the end of the message. Similar to fmt.Println.
func (*Logger) Error ¶
func (l *Logger) Error(v ...interface{})
Error emits this message to the error stream.
func (*Logger) Errorf ¶
Errorf emits this message to the error stream. Supports fmt.Printf formatting.
func (*Logger) Errorln ¶
func (l *Logger) Errorln(v ...interface{})
Error emits this message to the error stream and adds a new line at the end of the message. Similar to fmt.Println.
func (*Logger) Fatal ¶
func (l *Logger) Fatal(v ...interface{})
Fatal will print the message to the error stream and halt the program.
func (*Logger) Fatalf ¶
Fatalf is similar to Fatal but supports formatting. See fmt.Printf for format instructions.
func (*Logger) Fatalln ¶
func (l *Logger) Fatalln(v ...interface{})
Fatalln is similar to Fatal but adds a new line at the end of the output.
func (*Logger) Log ¶
func (l *Logger) Log(v ...interface{})
Log emits this message to the log stream.
func (*Logger) Logf ¶
Logf emits this message to the log stream. It supports formatting. See fmt.Printf for details on the formatting options.
func (*Logger) Logln ¶
func (l *Logger) Logln(v ...interface{})
Logln emits this message to the log stream and adds a new line at the end of the message. Similar to fmt.Println.
func (*Logger) SetDebugOutput ¶
SetDebugOutput changes the debug output stream. It preserves the flags and prefix of the old stream.
func (*Logger) SetErrorOutput ¶
SetErrorOutput changes the error output stream. It preserves the flags and prefix of the old stream.
func (*Logger) SetLogOutput ¶
SetLogOutput changes the log output stream. It preserves the flags and prefix of the old stream.