Use a single logger instance

Close #6
This commit is contained in:
Max Claus Nunes 2018-10-21 10:16:10 -03:00
parent c9da986360
commit daee1bff02
4 changed files with 29 additions and 22 deletions

View File

@ -15,16 +15,18 @@ var (
version = "1.0.1-dev"
)
var logger = gaper.NewLogger("gaper")
func main() {
logger := gaper.Logger()
loggerVerbose := false
parseArgs := func(c *cli.Context) *gaper.Config {
loggerVerbose = c.Bool("verbose")
return &gaper.Config{
BinName: c.String("bin-name"),
BuildPath: c.String("build-path"),
BuildArgsMerged: c.String("build-args"),
ProgramArgsMerged: c.String("program-args"),
Verbose: c.Bool("verbose"),
DisableDefaultIgnore: c.Bool("disable-default-ignore"),
WatchItems: c.StringSlice("watch"),
IgnoreItems: c.StringSlice("ignore"),
@ -42,7 +44,7 @@ func main() {
app.Action = func(c *cli.Context) {
args := parseArgs(c)
chOSSiginal := make(chan os.Signal, 2)
logger.Verbose(args.Verbose)
logger.Verbose(loggerVerbose)
if err := gaper.Run(args, chOSSiginal); err != nil {
logger.Error(err)

View File

@ -29,8 +29,6 @@ var (
NoRestartOnExit = "exit"
)
var logger = NewLogger("gaper")
// exit statuses
var exitStatusSuccess = 0
var exitStatusError = 1
@ -48,7 +46,6 @@ type Config struct {
PollInterval int
Extensions []string
NoRestartOn string
Verbose bool
DisableDefaultIgnore bool
WorkingDirectory string
}

View File

@ -7,18 +7,26 @@ import (
"github.com/fatih/color"
)
// Logger used by gaper
type Logger struct {
// 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 {
verbose bool
logDebug *log.Logger
logInfo *log.Logger
logError *log.Logger
}
// NewLogger creates a new logger
func NewLogger(prefix string) *Logger {
// newLogger creates a new logger
func newLogger(prefix string) *LoggerEntity {
prefix = "[" + prefix + "] "
return &Logger{
return &LoggerEntity{
verbose: false,
logDebug: log.New(os.Stdout, prefix, 0),
logInfo: log.New(os.Stdout, color.CyanString(prefix), 0),
@ -27,35 +35,35 @@ func NewLogger(prefix string) *Logger {
}
// Verbose toggle this logger verbosity
func (l *Logger) Verbose(verbose bool) {
func (l *LoggerEntity) Verbose(verbose bool) {
l.verbose = verbose
}
// Debug logs a debug message
func (l *Logger) Debug(v ...interface{}) {
func (l *LoggerEntity) Debug(v ...interface{}) {
if l.verbose {
l.logDebug.Println(v...)
}
}
// Debugf logs a debug message with format
func (l *Logger) Debugf(format string, v ...interface{}) {
func (l *LoggerEntity) Debugf(format string, v ...interface{}) {
if l.verbose {
l.logDebug.Printf(format, v...)
}
}
// Info logs a info message
func (l *Logger) Info(v ...interface{}) {
func (l *LoggerEntity) Info(v ...interface{}) {
l.logInfo.Println(v...)
}
// Error logs an error message
func (l *Logger) Error(v ...interface{}) {
func (l *LoggerEntity) Error(v ...interface{}) {
l.logError.Println(v...)
}
// Errorf logs and error message with format
func (l *Logger) Errorf(format string, v ...interface{}) {
func (l *LoggerEntity) Errorf(format string, v ...interface{}) {
l.logError.Printf(format, v...)
}

View File

@ -7,19 +7,19 @@ import (
)
func TestLoggerDefault(t *testing.T) {
l := NewLogger("gaper-test")
l := newLogger("gaper-test")
assert.Equal(t, l.verbose, false)
}
func TestLoggerEnableVerbose(t *testing.T) {
l := NewLogger("gaper-test")
l := newLogger("gaper-test")
l.Verbose(true)
assert.Equal(t, l.verbose, true)
}
func TestLoggerRunAllLogsWithoutVerbose(t *testing.T) {
// no asserts, just checking it doesn't crash
l := NewLogger("gaper-test")
l := newLogger("gaper-test")
l.Debug("debug")
l.Debugf("%s", "debug")
l.Info("info")
@ -29,7 +29,7 @@ func TestLoggerRunAllLogsWithoutVerbose(t *testing.T) {
func TestLoggerRunAllLogsWithVerbose(t *testing.T) {
// no asserts, just checking it doesn't crash
l := NewLogger("gaper-test")
l := newLogger("gaper-test")
l.Verbose(true)
l.Debug("debug")
l.Debugf("%s", "debug")