Skip to content

Commit 8eeefe2

Browse files
committed
Merge pull request #2 from mediocregopher/gslog-changes
Gslog changes
2 parents 4ed8568 + fcd9110 commit 8eeefe2

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

gslog/levels.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,40 @@ func Debug(msg string, params ...interface{}) {
3131
log(DEBUG, &msg, params)
3232
}
3333

34+
// Alias of Debug
35+
var Debugf = Debug
36+
3437
// Info writes a message to the log with info severity
3538
func Info(msg string, params ...interface{}) {
3639
log(INFO, &msg, params)
3740
}
3841

42+
// Alias of Info
43+
var Infof = Info
44+
3945
// Warn writes a message to the log with warn severity
4046
func Warn(msg string, params ...interface{}) {
4147
log(WARN, &msg, params)
4248
}
4349

50+
// Alias of Warn
51+
var Warnf = Warn
52+
4453
// Error writes a message to the log with error serverity
4554
func Error(msg string, params ...interface{}) {
4655
log(ERROR, &msg, params)
4756
}
4857

58+
// Alias of Error
59+
var Errorf = Error
60+
4961
// Fatal writes a message to the log with fatal severity, flushes any
5062
// messages waiting to be written, and exits with a non-zero status
5163
func Fatal(msg string, params ...interface{}) {
5264
log(FATAL, &msg, params)
5365
Flush()
5466
os.Exit(1)
5567
}
68+
69+
// Alias of Fatal
70+
var Fatalf = Fatal

gslog/logger.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type logger struct {
1515
sync.RWMutex
1616
fileHandle *os.File
1717
messages chan *message
18-
flushCh chan chan bool
18+
flushCh chan struct{}
1919
minLevel LogLevel
2020
}
2121

@@ -26,7 +26,7 @@ func init() {
2626
l = &logger{}
2727
l.fileHandle = os.Stderr
2828
l.messages = make(chan *message, 1024)
29-
l.flushCh = make(chan chan bool)
29+
l.flushCh = make(chan struct{})
3030

3131
go handleMessages()
3232
}
@@ -56,7 +56,9 @@ func SetMinimumLevel(level string) error {
5656
return nil
5757
}
5858

59-
// SetLogFile sets the file to which messages will be logged to. Default is STDOUT.
59+
// SetLogFile sets the file to which messages will be logged to. Can take
60+
// "stdout" or "stderr" to log to os.Stdout and os.Stderr, respectively. If
61+
// unset defaults to os.Stderr
6062
func SetLogFile(path string) error {
6163
l.RWMutex.Lock()
6264
defer l.RWMutex.Unlock()
@@ -69,6 +71,11 @@ func SetLogFile(path string) error {
6971
return nil
7072
}
7173
fh = os.Stderr
74+
} else if path == "stdout" {
75+
if l.fileHandle == os.Stdout {
76+
return nil
77+
}
78+
fh = os.Stdout
7279
} else {
7380
flags := os.O_APPEND | os.O_CREATE | os.O_WRONLY
7481
fh, err = os.OpenFile(path, flags, 0644)
@@ -87,9 +94,7 @@ func SetLogFile(path string) error {
8794

8895
// Flushes the log of at least 100 milliseconds worth of entries
8996
func Flush() {
90-
retCh := make(chan bool)
91-
l.flushCh <- retCh
92-
<-retCh
97+
l.flushCh <- struct{}{}
9398
}
9499

95100
// logString generates the string which will be written to the logfile
@@ -146,8 +151,7 @@ func handleMessages() {
146151

147152
case <-time.After(100 * time.Millisecond):
148153
select {
149-
case flushret := <-l.flushCh:
150-
flushret <- true
154+
case <-l.flushCh:
151155
default: // Oh well
152156
}
153157

0 commit comments

Comments
 (0)