/
multi_writer.go
63 lines (58 loc) · 1.49 KB
/
multi_writer.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
package multi_writer
import (
"github.com/gookit/color"
"github.com/kyaxcorp/go-core/core/helpers/conv"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
"io"
)
// This is a writer which has a method Write and returns the interface of io.Writer
type multiWriter struct {
//writers []io.Writer
writers []CustomWriter
}
type CustomWriter struct {
Writer io.Writer
// Should colors be filtered for this writer
FilterColors bool
// Here we define what type of writer is this, like stdout, file etc...
Type string
}
// MultiWriter -> create the instance which will handle the writing
func MultiWriter(multiWriters []CustomWriter) io.Writer {
return &multiWriter{
writers: multiWriters,
}
}
// Write -> This is the main function which will handle the writing
func (t *multiWriter) Write(p []byte) (n int, err error) {
for _, w := range t.writers {
// Remove stdout colors
if w.FilterColors {
// Convert from bytes to string
msg := conv.BytesToStr(p)
// Read the json value
_msg := gjson.Get(msg, "message").String()
// Remove the color codes
_msg = color.ClearCode(_msg)
//log.Println("found value", _msg)
// Set back to json the message value
newMsg, _err := sjson.Set(msg, "message", _msg)
if _err != nil {
// Take the original...
newMsg = msg
}
// convert back to bytes
p = conv.StrToBytes(newMsg)
}
n, err = w.Writer.Write(p)
if err != nil {
return
}
if n != len(p) {
err = io.ErrShortWrite
return
}
}
return len(p), nil
}