A simple leveled logging library with coloured output.
Log levels:
INFO(blue)WARNING(pink)ERROR(red)FATAL(red)
Formatters:
DefaultFormatterColouredFormatter
The logger can be configured using optional configuration functions:
WithLogLevel(level Level)- Sets the minimum log level to output (default:INFO)WithFormatter(formatter Formatter)- Sets the log formatter (default:DefaultFormatter)
Create a new package log in your app:
package log
import (
"github.com/RichardKnop/logging"
)
var (
// Create logger with default configuration (INFO level, DefaultFormatter)
logger = logging.New(nil, nil)
// INFO ...
INFO = logger[logging.INFO]
// WARNING ...
WARNING = logger[logging.WARNING]
// ERROR ...
ERROR = logger[logging.ERROR]
// FATAL ...
FATAL = logger[logging.FATAL]
)You can customize the logger using configuration options:
package log
import (
"github.com/RichardKnop/logging"
)
var (
// Create logger with coloured formatter and DEBUG level
logger = logging.New(
nil,
nil,
logging.WithLogLevel(logging.DEBUG),
logging.WithFormatter(new(logging.ColouredFormatter)),
)
// DEBUG ...
DEBUG = logger[logging.DEBUG]
// INFO ...
INFO = logger[logging.INFO]
// WARNING ...
WARNING = logger[logging.WARNING]
// ERROR ...
ERROR = logger[logging.ERROR]
// FATAL ...
FATAL = logger[logging.FATAL]
)Then from your app you could do:
package main
import (
"github.com/yourusername/yourapp/log"
)
func main() {
log.INFO.Print("log message")
log.WARNING.Printf("formatted %s", "message")
log.ERROR.Println("error message")
}