log/tee.go

36 lines
612 B
Go
Raw Normal View History

package log
import (
"fmt"
"io"
"os"
"regexp"
)
// FileTee can write logs to a file on disk as well as standard output.
type FileTee struct {
fh io.Writer
}
var RegexpANSIColorCode = regexp.MustCompile(`\x1B\[[0-9;]+m`)
func NewFileTee(filename string) (*FileTee, error) {
fh, err := os.Create(filename)
if err != nil {
return nil, err
}
return &FileTee{
fh: fh,
}, nil
}
func (ft *FileTee) Write(p []byte) (n int, err error) {
var (
text = string(p)
noColor = RegexpANSIColorCode.ReplaceAllString(text, "")
)
fmt.Fprintf(os.Stdout, string(p))
return fmt.Fprintf(ft.fh, noColor)
}