-
Notifications
You must be signed in to change notification settings - Fork 1
/
writer.go
55 lines (48 loc) · 1.35 KB
/
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
/* stdlogtoapex is a package that provides an implementation of
/* io.Writer that can be used to redirect log output from the standard
/* libraries log package via github.com/apex/log.
*/
package stdlogtoapex
import (
"log"
alog "github.com/apex/log"
)
// Writer is a struct that implements the io.Writer interface and can,
// therefore, be passed into log.SetOutput. Writer must always be
// constructed by called NewWriter.
type Writer struct {
prefixLen int
}
func (w *Writer) stripDatePrefix(p []byte) string {
if w.prefixLen > len(p) {
// Safer to return the old value than trim all.
return string(p)
}
return string(p[w.prefixLen:])
}
// Write implements the io.Writer interface for Writer. Log messages
// output via this method will have their date and time information
// stripped (apex log will have its own) .
func (w *Writer) Write(p []byte) (n int, err error) {
msg := w.stripDatePrefix(p)
alog.Info(msg)
return len(p), nil
}
// NewWriter creates a new Writer that can be passed to the SetOutput
// function in the log package from the standard library.
func NewWriter() *Writer {
var prefixLen int
flags := log.Flags()
if flags&log.Ldate != 0 {
prefixLen += 11
}
if flags&log.Lmicroseconds != 0 {
prefixLen += 16
} else {
if flags&log.Ltime != 0 {
prefixLen += 9
}
}
writer := &Writer{prefixLen: prefixLen}
return writer
}