Intercept stdlib logger (try 2). (#4796)

This builds on #4665 and #4781. The problem we had previously was that
we were relying on a goroutine to consume bytes from a pipe in a
non-blocking manner, which meant that log.Fatal would cause us to exit
before writing out the data.

This version implements an io.Writer so we can make sure the log line
gets written in a blocking manner.
This commit is contained in:
Jacob Hoffman-Andrews 2020-04-27 11:21:43 -07:00 committed by GitHub
parent b1347fb3b3
commit 324aaa0571
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 0 deletions

View File

@ -8,6 +8,7 @@ import (
"expvar"
"fmt"
"io/ioutil"
"log"
"log/syslog"
"net/http"
"net/http/pprof"
@ -131,6 +132,15 @@ func (log promLogger) Println(args ...interface{}) {
log.AuditErr(fmt.Sprint(args...))
}
type logWriter struct {
blog.Logger
}
func (lw logWriter) Write(p []byte) (n int, err error) {
lw.Logger.Info(string(p))
return
}
// StatsAndLogging constructs a prometheus registerer and an AuditLogger based
// on its config parameters, and return them both. It also spawns off an HTTP
// server on the provided port to report the stats and provide pprof profiling
@ -169,6 +179,7 @@ func NewLogger(logConf SyslogConfig) blog.Logger {
cfsslLog.SetLogger(cfsslLogger{logger})
_ = mysql.SetLogger(mysqlLogger{logger})
grpclog.SetLoggerV2(grpcLogger{logger})
log.SetOutput(logWriter{logger})
return logger
}

View File

@ -3,7 +3,9 @@ package cmd
import (
"encoding/json"
"fmt"
"log"
"runtime"
"strings"
"testing"
"github.com/letsencrypt/boulder/core"
@ -121,6 +123,21 @@ func TestCfsslLogger(t *testing.T) {
}
}
func TestCaptureStdlibLog(t *testing.T) {
logger := blog.UseMock()
oldDest := log.Writer()
defer func() {
log.SetOutput(oldDest)
}()
log.SetOutput(logWriter{logger})
log.Print("thisisatest")
results := logger.GetAllMatching("thisisatest")
if len(results) != 1 {
t.Fatalf("Expected logger to receive 'thisisatest', got: %s",
strings.Join(logger.GetAllMatching(".*"), "\n"))
}
}
func TestVersionString(t *testing.T) {
core.BuildID = "TestBuildID"
core.BuildTime = "RightNow!"