2018-06-22 07:01:36 -05:00
|
|
|
package gaper
|
2018-06-16 19:22:21 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/fatih/color"
|
|
|
|
)
|
|
|
|
|
2018-10-21 08:16:10 -05:00
|
|
|
// logger use by the whole package
|
|
|
|
var logger = newLogger("gaper")
|
|
|
|
|
|
|
|
// Logger give access to external packages to use gaper logger
|
|
|
|
func Logger() *LoggerEntity {
|
|
|
|
return logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoggerEntity used by gaper
|
|
|
|
type LoggerEntity struct {
|
2018-06-16 19:22:21 -05:00
|
|
|
verbose bool
|
|
|
|
logDebug *log.Logger
|
|
|
|
logInfo *log.Logger
|
|
|
|
logError *log.Logger
|
|
|
|
}
|
|
|
|
|
2018-10-21 08:16:10 -05:00
|
|
|
// newLogger creates a new logger
|
|
|
|
func newLogger(prefix string) *LoggerEntity {
|
2018-06-16 19:22:21 -05:00
|
|
|
prefix = "[" + prefix + "] "
|
2018-10-21 08:16:10 -05:00
|
|
|
return &LoggerEntity{
|
2018-06-16 19:22:21 -05:00
|
|
|
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-10-21 08:16:10 -05:00
|
|
|
func (l *LoggerEntity) Verbose(verbose bool) {
|
2018-06-16 19:22:21 -05:00
|
|
|
l.verbose = verbose
|
|
|
|
}
|
|
|
|
|
2018-06-20 21:04:16 -05:00
|
|
|
// Debug logs a debug message
|
2018-10-21 08:16:10 -05:00
|
|
|
func (l *LoggerEntity) Debug(v ...interface{}) {
|
2018-06-16 19:22:21 -05:00
|
|
|
if l.verbose {
|
|
|
|
l.logDebug.Println(v...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-20 21:04:16 -05:00
|
|
|
// Debugf logs a debug message with format
|
2018-10-21 08:16:10 -05:00
|
|
|
func (l *LoggerEntity) Debugf(format string, v ...interface{}) {
|
2018-06-18 21:47:20 -05:00
|
|
|
if l.verbose {
|
|
|
|
l.logDebug.Printf(format, v...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-20 21:04:16 -05:00
|
|
|
// Info logs a info message
|
2018-10-21 08:16:10 -05:00
|
|
|
func (l *LoggerEntity) Info(v ...interface{}) {
|
2018-06-16 19:22:21 -05:00
|
|
|
l.logInfo.Println(v...)
|
|
|
|
}
|
|
|
|
|
2018-06-20 21:04:16 -05:00
|
|
|
// Error logs an error message
|
2018-10-21 08:16:10 -05:00
|
|
|
func (l *LoggerEntity) Error(v ...interface{}) {
|
2018-06-16 19:22:21 -05:00
|
|
|
l.logError.Println(v...)
|
|
|
|
}
|
|
|
|
|
2018-06-20 21:04:16 -05:00
|
|
|
// Errorf logs and error message with format
|
2018-10-21 08:16:10 -05:00
|
|
|
func (l *LoggerEntity) Errorf(format string, v ...interface{}) {
|
2018-06-16 19:22:21 -05:00
|
|
|
l.logError.Printf(format, v...)
|
|
|
|
}
|