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

View File

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

View File

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

View File

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