-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtermlog.go
64 lines (54 loc) · 1.73 KB
/
termlog.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright (C) 2010, Kyle Lemons <[email protected]>. All rights reserved.
package log4go
import (
"fmt"
"io"
"os"
)
var stdout io.Writer = os.Stdout
/* ConsoleLogWriter was previously a channel which would let you do things
like pass nil values instead. We chagned it to a struct so we could
dangle some extra attributes on it. So that we don't have to go rework
large swaths of the code base to uses pointers for ConsoleLogWriter we're
just going to add this interface so everything else "Just Works". */
type ConsoleLogWriter interface {
run(out io.Writer)
LogWrite(rec *LogRecord)
Close()
}
// This is the standard writer that prints to standard output.
type ConsoleLogWriterImp struct {
records chan *LogRecord
completed chan int
}
// This creates a new ConsoleLogWriter
func NewConsoleLogWriter() ConsoleLogWriter {
writer := ConsoleLogWriterImp{
records: make(chan *LogRecord, LogBufferLength),
completed: make(chan int),
}
go writer.run(stdout)
return writer
}
func (w ConsoleLogWriterImp) run(out io.Writer) {
var timestr string
var timestrAt int64
for rec := range w.records {
if at := rec.Created.UnixNano() / 1e9; at != timestrAt {
timestr, timestrAt = rec.Created.Format("01/02/06 15:04:05"), at
}
fmt.Fprint(out, "[", timestr, "] [", levelStrings[rec.Level], "] ", rec.Message, "\n")
}
close(w.completed)
}
// This is the ConsoleLogWriter's output method. This will block if the output
// buffer is full.
func (w ConsoleLogWriterImp) LogWrite(rec *LogRecord) {
w.records <- rec
}
// Close stops the logger from sending messages to standard output. Attempts to
// send log messages to this logger after a Close have undefined behavior.
func (w ConsoleLogWriterImp) Close() {
close(w.records)
<-w.completed
}