gaper/loggger.go

62 lines
1.2 KiB
Go
Raw Normal View History

package gaper
2018-06-16 19:22:21 -05:00
import (
"log"
"os"
"github.com/fatih/color"
)
2018-06-20 21:04:16 -05:00
// Logger used by gaper
2018-06-16 19:22:21 -05:00
type Logger struct {
verbose bool
logDebug *log.Logger
logInfo *log.Logger
logError *log.Logger
}
2018-06-20 21:04:16 -05:00
// NewLogger creates a new logger
2018-06-16 19:22:21 -05:00
func NewLogger(prefix string) *Logger {
prefix = "[" + prefix + "] "
return &Logger{
verbose: false,
logDebug: log.New(os.Stdout, prefix, 0),
logInfo: log.New(os.Stdout, color.CyanString(prefix), 0),
logError: log.New(os.Stdout, color.RedString(prefix), 0),
}
}
2018-06-20 21:04:16 -05:00
// Verbose toggle this logger verbosity
2018-06-16 19:22:21 -05:00
func (l *Logger) Verbose(verbose bool) {
l.verbose = verbose
}
2018-06-20 21:04:16 -05:00
// Debug logs a debug message
2018-06-16 19:22:21 -05:00
func (l *Logger) Debug(v ...interface{}) {
if l.verbose {
l.logDebug.Println(v...)
}
}
2018-06-20 21:04:16 -05:00
// Debugf logs a debug message with format
func (l *Logger) Debugf(format string, v ...interface{}) {
if l.verbose {
l.logDebug.Printf(format, v...)
}
}
2018-06-20 21:04:16 -05:00
// Info logs a info message
2018-06-16 19:22:21 -05:00
func (l *Logger) Info(v ...interface{}) {
l.logInfo.Println(v...)
}
2018-06-20 21:04:16 -05:00
// Error logs an error message
2018-06-16 19:22:21 -05:00
func (l *Logger) Error(v ...interface{}) {
l.logError.Println(v...)
}
2018-06-20 21:04:16 -05:00
// Errorf logs and error message with format
2018-06-16 19:22:21 -05:00
func (l *Logger) Errorf(format string, v ...interface{}) {
l.logError.Printf(format, v...)
}