Skip to content

Commit 56d1893

Browse files
joeybloggsjoeybloggs
authored andcommitted
Add RedirectSTDLogOutput(...) to console logger to allow capturing of all std log messages.
1 parent 860e31a commit 56d1893

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## log
22
<img align="right" src="https://raw.githubusercontent.com/go-playground/log/master/logo.png">
3-
![Project status](https://img.shields.io/badge/version-2.0-green.svg)
3+
![Project status](https://img.shields.io/badge/version-2.1-green.svg)
44
[![Build Status](https://semaphoreci.com/api/v1/joeybloggs/log/branches/master/badge.svg)](https://semaphoreci.com/joeybloggs/log)
55
[![Coverage Status](https://coveralls.io/repos/github/go-playground/log/badge.svg?branch=master)](https://coveralls.io/github/go-playground/log?branch=master)
66
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/log)](https://goreportcard.com/report/github.com/go-playground/log)

handlers/console/console.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package console
22

33
import (
4+
"bufio"
45
"fmt"
56
"io"
67
stdlog "log"
@@ -39,6 +40,7 @@ type Console struct {
3940
gopath string
4041
fileDisplay log.FilenameDisplay
4142
displayColor bool
43+
redirStdOutput bool
4244
}
4345

4446
// Colors mapping.
@@ -76,6 +78,12 @@ func (c *Console) SetFilenameDisplay(fd log.FilenameDisplay) {
7678
c.fileDisplay = fd
7779
}
7880

81+
// RedirectSTDLogOutput tells Console to redirect the std Logger output
82+
// to the log package itself.
83+
func (c *Console) RedirectSTDLogOutput(b bool) {
84+
c.redirStdOutput = b
85+
}
86+
7987
// DisplayColor tells Console to output in color or not
8088
// Default is : true
8189
func (c *Console) DisplayColor(color bool) {
@@ -136,9 +144,38 @@ func (c *Console) Run() chan<- *log.Entry {
136144
go c.handleLog(ch)
137145
}
138146

147+
// let's check to see if we should hanlde the std log messages as well
148+
if c.redirStdOutput {
149+
150+
done := make(chan struct{})
151+
152+
go handleStdLogger(done)
153+
154+
<-done // have to wait, it was running too quickly and some messages can be lost
155+
}
156+
139157
return ch
140158
}
141159

160+
// this will redirect the output of
161+
func handleStdLogger(done chan<- struct{}) {
162+
r, w := io.Pipe()
163+
defer r.Close()
164+
defer w.Close()
165+
166+
stdlog.SetOutput(w)
167+
168+
scanner := bufio.NewScanner(r)
169+
170+
go func() {
171+
done <- struct{}{}
172+
}()
173+
174+
for scanner.Scan() {
175+
log.WithFields(log.F("stdlog", true)).Info(scanner.Text())
176+
}
177+
}
178+
142179
// handleLog consumes and logs any Entry's passed to the channel
143180
func (c *Console) handleLog(entries <-chan *log.Entry) {
144181

handlers/console/console_test.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package console
22

33
import (
44
"bytes"
5+
stdlog "log"
56
"strings"
67
"testing"
78

@@ -19,7 +20,6 @@ import (
1920
// go test -coverprofile cover.out && go tool cover -html=cover.out -o cover.html
2021

2122
func TestConsoleLogger(t *testing.T) {
22-
2323
tests := getConsoleLoggerTests()
2424

2525
buff := new(bytes.Buffer)
@@ -274,6 +274,29 @@ func TestSetFilenameColor(t *testing.T) {
274274
buff.Reset()
275275
}
276276

277+
func TestConsoleSTDLogCapturing(t *testing.T) {
278+
279+
buff := new(bytes.Buffer)
280+
281+
cLog := New()
282+
cLog.SetWriter(buff)
283+
cLog.DisplayColor(false)
284+
cLog.SetBuffersAndWorkers(3, 3)
285+
cLog.SetTimestampFormat("MST")
286+
cLog.RedirectSTDLogOutput(true)
287+
log.RegisterHandler(cLog, log.AllLevels...)
288+
289+
stdlog.Println("STD LOG message")
290+
291+
s := buff.String()
292+
293+
expected := "STD LOG message stdlog=true"
294+
295+
if !strings.Contains(s, expected) {
296+
t.Errorf("Expected '%s' Got '%s'", expected, s)
297+
}
298+
}
299+
277300
type test struct {
278301
lvl log.Level
279302
msg string

0 commit comments

Comments
 (0)