README
¶
loglevel
A simple standard out logger and formatter that supports log levels, namespaces and coloured output.
Get started
Download the package
go get -t github.com/amimof/loglevel-go
Import it in your code
package main
import "github.com/amimof/loglevel-go"
func main() {
// Create an instance of loglevel
loglevel := loglevel.New()
// Set loglevel. 3=Debug, 2=Info, 1=Warn, 0=Error. Defaults to Info.
loglevel.SetLevel(3)
// Log away
loglevel.Debug("Debug message")
loglevel.Info("Info message")
loglevel.Warn("Warn message")
loglevel.Error("Error message")
}
Result
2017-01-21T13:56:28+01:00 DEBUG Debug message
2017-01-21T13:56:28+01:00 INFO Info message
2017-01-21T13:56:28+01:00 WARN Warn message
2017-01-21T13:56:28+01:00 ERROR Error message
Formatting
loglevel supports both non-format and format print methods. Formatting is as good as in the native fmt
package but with the additional log level functionality.
package main
import "github.com/amimof/loglevel-go"
func main() {
// Create an instance of loglevel
loglevel := loglevel.New()
// Set loglevel. 3=Debug, 2=Info, 1=Warn, 0=Error. Defaults to Info.
loglevel.SetLevel(3)
fname := "Luke"
lname := "Skywalker"
loglevel.Infof("The Jedi %s, %s", fname, lname)
}
Result:
2017-01-21T13:53:54+01:00 INFO The Jedi Luke, Skywalker
Namespaces
You can created multiple loglevel instances in your app. Assign different names to them, to make debugging app packages an modules easy.
package main
import "github.com/amimof/loglevel-go"
func main() {
// Create an instance of loglevel
loglevel := loglevel.New().SetLevel(3)
// Set the namespace of this loglevel instance
loglevel.Name = "StarWarsNames"
loglevel.PrintName = true
fname := "Luke"
lname := "Skywalker"
loglevel.Infof("The Jedi %s, %s", fname, lname)
}
Result:
2017-01-21T13:53:54+01:00 INFO StarWarsNames The Jedi Luke, Skywalker
Options
If you like vanilla, fmt.Println()
, formatting. Disable those fiels.
package main
import "github.com/amimof/loglevel-go"
func main() {
// Create an instance of loglevel
loglevel := loglevel.New()
loglevel.PrintTime = false
loglevel.PrintLevel = false
loglevel.PrintName = false
loglevel.Warn("Achtung, Achtung!")
}
Result
Achtung, Achtung!
Documentation
¶
Index ¶
- Constants
- type Logger
- func (l *Logger) Debug(message ...interface{})
- func (l *Logger) Debugf(format string, message ...interface{})
- func (l *Logger) Error(message ...interface{})
- func (l *Logger) Errorf(format string, message ...interface{})
- func (l *Logger) Info(message ...interface{})
- func (l *Logger) Infof(format string, message ...interface{})
- func (l *Logger) Out(str string) error
- func (l *Logger) Output(color, level, str string) (n int, err error)
- func (l *Logger) Panic(message ...interface{})
- func (l *Logger) Panicf(format string, message ...interface{})
- func (l *Logger) Panicln(message ...interface{})
- func (l *Logger) Print(message ...interface{})
- func (l *Logger) Printf(format string, message ...interface{})
- func (l *Logger) Println(message ...interface{})
- func (l *Logger) SetLevel(level int) *Logger
- func (l *Logger) Warn(message ...interface{})
- func (l *Logger) Warnf(format string, message ...interface{})
Constants ¶
const ( CLR_0 = "\x1b[30;1m" CLR_R = "\x1b[31;1m" CLR_G = "\x1b[32;1m" CLR_Y = "\x1b[33;1m" CLR_B = "\x1b[34;1m" CLR_M = "\x1b[35;1m" CLR_C = "\x1b[36;1m" CLR_W = "\x1b[37;1m" CLR_N = "\x1b[0m" )
const ( DEBUG = 3 INFO = 2 WARN = 1 ERROR = 0 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Logger ¶
type Logger struct { Name string Level int TimeFormat string PrintTime bool PrintName bool PrintLevel bool UseColors bool // contains filtered or unexported fields }
func (*Logger) Debug ¶
func (l *Logger) Debug(message ...interface{})
Prints a debug message on a new line
func (*Logger) Debugf ¶
Prints according to fmt.Sprintf format specifier and returns the resulting string
func (*Logger) Error ¶
func (l *Logger) Error(message ...interface{})
Prints an error message on a new line followed by Exit
func (*Logger) Errorf ¶
Prints according to fmt.Sprintf format specifier and returns the resulting string
func (*Logger) Info ¶
func (l *Logger) Info(message ...interface{})
Prints n info message on a new line
func (*Logger) Infof ¶
Prints according to fmt.Sprintf format specifier and returns the resulting string
func (*Logger) Output ¶ added in v1.1.0
Writes the specified string to std and colors it accordingly
func (*Logger) Panic ¶ added in v1.1.0
func (l *Logger) Panic(message ...interface{})
Same as fmt.Sprint followed by panic
func (*Logger) Panicln ¶ added in v1.1.0
func (l *Logger) Panicln(message ...interface{})
Same as fmt.Sprintln followed by panic
func (*Logger) Print ¶ added in v1.1.0
func (l *Logger) Print(message ...interface{})
Same as fmt.Sprint
func (*Logger) Println ¶ added in v1.1.0
func (l *Logger) Println(message ...interface{})
Same as fmt.Sprintln