diff --git a/gslog/levels.go b/gslog/levels.go index 1116112..74f51a8 100644 --- a/gslog/levels.go +++ b/gslog/levels.go @@ -31,21 +31,33 @@ func Debug(msg string, params ...interface{}) { log(DEBUG, &msg, params) } +// Alias of Debug +var Debugf = Debug + // Info writes a message to the log with info severity func Info(msg string, params ...interface{}) { log(INFO, &msg, params) } +// Alias of Info +var Infof = Info + // Warn writes a message to the log with warn severity func Warn(msg string, params ...interface{}) { log(WARN, &msg, params) } +// Alias of Warn +var Warnf = Warn + // Error writes a message to the log with error serverity func Error(msg string, params ...interface{}) { log(ERROR, &msg, params) } +// Alias of Error +var Errorf = Error + // Fatal writes a message to the log with fatal severity, flushes any // messages waiting to be written, and exits with a non-zero status func Fatal(msg string, params ...interface{}) { @@ -53,3 +65,6 @@ func Fatal(msg string, params ...interface{}) { Flush() os.Exit(1) } + +// Alias of Fatal +var Fatalf = Fatal diff --git a/gslog/logger.go b/gslog/logger.go index 879962f..89467e7 100644 --- a/gslog/logger.go +++ b/gslog/logger.go @@ -15,7 +15,7 @@ type logger struct { sync.RWMutex fileHandle *os.File messages chan *message - flushCh chan chan bool + flushCh chan struct{} minLevel LogLevel } @@ -26,7 +26,7 @@ func init() { l = &logger{} l.fileHandle = os.Stderr l.messages = make(chan *message, 1024) - l.flushCh = make(chan chan bool) + l.flushCh = make(chan struct{}) go handleMessages() } @@ -56,7 +56,9 @@ func SetMinimumLevel(level string) error { return nil } -// SetLogFile sets the file to which messages will be logged to. Default is STDOUT. +// SetLogFile sets the file to which messages will be logged to. Can take +// "stdout" or "stderr" to log to os.Stdout and os.Stderr, respectively. If +// unset defaults to os.Stderr func SetLogFile(path string) error { l.RWMutex.Lock() defer l.RWMutex.Unlock() @@ -69,6 +71,11 @@ func SetLogFile(path string) error { return nil } fh = os.Stderr + } else if path == "stdout" { + if l.fileHandle == os.Stdout { + return nil + } + fh = os.Stdout } else { flags := os.O_APPEND | os.O_CREATE | os.O_WRONLY fh, err = os.OpenFile(path, flags, 0644) @@ -87,9 +94,7 @@ func SetLogFile(path string) error { // Flushes the log of at least 100 milliseconds worth of entries func Flush() { - retCh := make(chan bool) - l.flushCh <- retCh - <-retCh + l.flushCh <- struct{}{} } // logString generates the string which will be written to the logfile @@ -146,8 +151,7 @@ func handleMessages() { case <-time.After(100 * time.Millisecond): select { - case flushret := <-l.flushCh: - flushret <- true + case <-l.flushCh: default: // Oh well }