-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer_sink.go
58 lines (52 loc) · 1.5 KB
/
writer_sink.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
package lager
import (
"bytes"
"github.com/bar-counter/slog/lager/color"
"io"
"sync"
)
//const logBufferSize = 1024
// A Sink represents a write destination for a Logger. It provides
// a thread-safe interface for writing logs
type Sink interface {
//Log to the sink. Best effort -- no need to worry about errors.
Log(level LogLevel, payload []byte)
}
type writerSink struct {
writer io.Writer
minLogLevel LogLevel
name string
writeL *sync.Mutex
}
// NewWriterSink is function which returns new struct object
func NewWriterSink(name string, writer io.Writer, minLogLevel LogLevel) Sink {
return &writerSink{
writer: writer,
minLogLevel: minLogLevel,
writeL: new(sync.Mutex),
name: name,
}
}
func (sink *writerSink) Log(level LogLevel, log []byte) {
if level < sink.minLogLevel {
return
}
if sink.name == "stdout" {
if bytes.Contains(log, []byte("WARN")) {
log = bytes.Replace(log, []byte("WARN"), color.WarnByte, -1)
} else if bytes.Contains(log, []byte("ERROR")) {
log = bytes.Replace(log, []byte("ERROR"), color.ErrorByte, -1)
} else if bytes.Contains(log, []byte("FATAL")) {
log = bytes.Replace(log, []byte("FATAL"), color.FatalByte, -1)
} else if bytes.Contains(log, []byte("DEBUG")) {
log = bytes.Replace(log, []byte("DEBUG"), color.DebugByte, -1)
}
}
log = append(log, '\n')
sink.writeL.Lock()
_, err := sink.writer.Write(log)
if err != nil {
println("writer_sink Log Failed to write log: " + err.Error())
}
sink.writeL.Unlock()
}