-
Notifications
You must be signed in to change notification settings - Fork 1
/
logwriter.go
52 lines (43 loc) · 1.09 KB
/
logwriter.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
package log
import (
"github.com/Futuremine-chain/future/tools/log/log15/term"
"github.com/jrick/logrotate/rotator"
"github.com/mattn/go-colorable"
"io"
"os"
)
// logWriter implements an io.Writer that outputs to both standard output and
// the write-end pipe of an initialized log rotator.
type logWriter struct {
// logRotator is one of the logging outputs. It should be closed on
// application shutdown.
logRotator *rotator.Rotator
// Use for color terminal
colorableWrite io.Writer
}
func (lw *logWriter) Init() {
// init a colorful logger if possible
usecolor := term.IsTty(os.Stdout.Fd()) && os.Getenv("TERM") != "dumb"
if usecolor {
lw.colorableWrite = colorable.NewColorableStderr()
}
}
func (lw *logWriter) Close() {
if lw.logRotator != nil {
lw.logRotator.Close()
}
}
func (lw *logWriter) IsUseColor() bool {
return lw.colorableWrite != nil
}
func (lw *logWriter) Write(p []byte) (n int, err error) {
if lw.logRotator != nil {
lw.logRotator.Write(p)
}
if lw.colorableWrite != nil {
lw.colorableWrite.Write(p)
} else {
os.Stderr.Write(p)
}
return len(p), nil
}