libyear/pkg/logger/logger.go

70 lines
1.2 KiB
Go

package logger
import (
"fmt"
"io"
"os"
)
// Log provices a simple leveled logger
type Log struct {
quiet bool
debug bool
errOut io.Writer
stdOut io.Writer
}
// NewLogger returns a new logger (duh) with the given levels and output writers
func NewLogger(quiet, debug bool, stdOut, errOut io.Writer) *Log {
if stdOut == nil {
stdOut = os.Stdout
}
if errOut == nil {
errOut = os.Stderr
}
return &Log{
quiet: quiet,
debug: debug,
stdOut: stdOut,
errOut: errOut,
}
}
// Error logs at the error level
func (l *Log) Error(s ...interface{}) {
fmt.Fprint(l.errOut, s...)
}
// Errorf uses fmt.Fprint formatting
func (l *Log) Errorf(f string, s ...interface{}) {
fmt.Fprintf(l.errOut, f, s...)
}
// Log only logs if quiet is false
func (l *Log) Log(s ...interface{}) {
if !l.quiet {
fmt.Fprint(l.stdOut, s...)
}
}
// Logf uses fmt.Fprint fprmatting
func (l *Log) Logf(f string, s ...interface{}) {
if !l.quiet {
fmt.Fprintf(l.stdOut, f, s...)
}
}
// Debug logs debug-level logs if debug is true
func (l *Log) Debug(s ...interface{}) {
if l.debug {
fmt.Fprint(l.errOut, s...)
}
}
// Debugf uses fmt.Fprint fprmatting
func (l *Log) Debugf(f string, s ...interface{}) {
if l.debug {
fmt.Fprintf(l.errOut, f, s...)
}
}