Improve stderr logging for PostCSS and simlilar

Fixes #7584
This commit is contained in:
Bjørn Erik Pedersen 2020-08-20 18:43:09 +02:00
parent ae63c2b5c9
commit ec37420468
7 changed files with 77 additions and 25 deletions

View file

@ -39,6 +39,23 @@ func init() {
jww.SetLogListeners(jww.LogCounter(GlobalErrorCounter, jww.LevelError)) jww.SetLogListeners(jww.LogCounter(GlobalErrorCounter, jww.LevelError))
} }
func LoggerToWriterWithPrefix(logger *log.Logger, prefix string) io.Writer {
return prefixWriter{
logger: logger,
prefix: prefix,
}
}
type prefixWriter struct {
logger *log.Logger
prefix string
}
func (w prefixWriter) Write(p []byte) (n int, err error) {
w.logger.Printf("%s: %s", w.prefix, p)
return len(p), nil
}
// Logger wraps a *loggers.Logger and some other related logging state. // Logger wraps a *loggers.Logger and some other related logging state.
type Logger struct { type Logger struct {
*jww.Notepad *jww.Notepad
@ -93,17 +110,32 @@ func NewLogger(stdoutThreshold, logThreshold jww.Threshold, outHandle, logHandle
// NewDebugLogger is a convenience function to create a debug logger. // NewDebugLogger is a convenience function to create a debug logger.
func NewDebugLogger() *Logger { func NewDebugLogger() *Logger {
return newBasicLogger(jww.LevelDebug) return NewBasicLogger(jww.LevelDebug)
} }
// NewWarningLogger is a convenience function to create a warning logger. // NewWarningLogger is a convenience function to create a warning logger.
func NewWarningLogger() *Logger { func NewWarningLogger() *Logger {
return newBasicLogger(jww.LevelWarn) return NewBasicLogger(jww.LevelWarn)
}
// NewInfoLogger is a convenience function to create a info logger.
func NewInfoLogger() *Logger {
return NewBasicLogger(jww.LevelInfo)
} }
// NewErrorLogger is a convenience function to create an error logger. // NewErrorLogger is a convenience function to create an error logger.
func NewErrorLogger() *Logger { func NewErrorLogger() *Logger {
return newBasicLogger(jww.LevelError) return NewBasicLogger(jww.LevelError)
}
// NewBasicLogger creates a new basic logger writing to Stdout.
func NewBasicLogger(t jww.Threshold) *Logger {
return newLogger(t, jww.LevelError, os.Stdout, ioutil.Discard, false)
}
// NewBasicLoggerForWriter creates a new basic logger writing to w.
func NewBasicLoggerForWriter(t jww.Threshold, w io.Writer) *Logger {
return newLogger(t, jww.LevelError, w, ioutil.Discard, false)
} }
var ( var (
@ -215,7 +247,3 @@ func newLogger(stdoutThreshold, logThreshold jww.Threshold, outHandle, logHandle
errors: errorBuff, errors: errorBuff,
} }
} }
func newBasicLogger(t jww.Threshold) *Logger {
return newLogger(t, jww.LevelError, os.Stdout, ioutil.Discard, false)
}

View file

@ -14,6 +14,9 @@
package loggers package loggers
import ( import (
"bytes"
"fmt"
"log"
"testing" "testing"
qt "github.com/frankban/quicktest" qt "github.com/frankban/quicktest"
@ -30,3 +33,17 @@ func TestLogger(t *testing.T) {
c.Assert(l.ErrorCounter.Count(), qt.Equals, uint64(2)) c.Assert(l.ErrorCounter.Count(), qt.Equals, uint64(2))
} }
func TestLoggerToWriterWithPrefix(t *testing.T) {
c := qt.New(t)
var b bytes.Buffer
logger := log.New(&b, "", 0)
w := LoggerToWriterWithPrefix(logger, "myprefix")
fmt.Fprint(w, "Hello Hugo!")
c.Assert(b.String(), qt.Equals, "myprefix: Hello Hugo!\n")
}

View file

@ -14,12 +14,15 @@
package hugolib package hugolib
import ( import (
"bytes"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"runtime" "runtime"
"testing" "testing"
jww "github.com/spf13/jwalterweatherman"
"github.com/gohugoio/hugo/htesting" "github.com/gohugoio/hugo/htesting"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -80,10 +83,13 @@ class Car {
c.Assert(err, qt.IsNil) c.Assert(err, qt.IsNil)
defer clean() defer clean()
var logBuf bytes.Buffer
logger := loggers.NewBasicLoggerForWriter(jww.LevelInfo, &logBuf)
v := viper.New() v := viper.New()
v.Set("workingDir", workDir) v.Set("workingDir", workDir)
v.Set("disableKinds", []string{"taxonomy", "term", "page"}) v.Set("disableKinds", []string{"taxonomy", "term", "page"})
b := newTestSitesBuilder(t).WithLogger(loggers.NewWarningLogger()) b := newTestSitesBuilder(t).WithLogger(logger)
// Need to use OS fs for this. // Need to use OS fs for this.
b.Fs = hugofs.NewDefault(v) b.Fs = hugofs.NewDefault(v)
@ -108,12 +114,10 @@ Transpiled: {{ $transpiled.Content | safeJS }}
_, err = exec.Command("npm", "install").CombinedOutput() _, err = exec.Command("npm", "install").CombinedOutput()
b.Assert(err, qt.IsNil) b.Assert(err, qt.IsNil)
out, err := captureStderr(func() error { b.Build(BuildCfg{})
return b.BuildE(BuildCfg{})
})
// Make sure Node sees this. // Make sure Node sees this.
b.Assert(out, qt.Contains, "Hugo Environment: production") b.Assert(logBuf.String(), qt.Contains, "babel: Hugo Environment: production")
b.Assert(err, qt.IsNil) b.Assert(err, qt.IsNil)
b.AssertFileContent("public/index.html", ` b.AssertFileContent("public/index.html", `

View file

@ -14,10 +14,15 @@
package hugolib package hugolib
import ( import (
"bytes"
jww "github.com/spf13/jwalterweatherman"
"fmt" "fmt"
"io" "io"
"math/rand" "math/rand"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"runtime" "runtime"
@ -893,10 +898,13 @@ h1 {
c.Assert(err, qt.IsNil) c.Assert(err, qt.IsNil)
defer clean() defer clean()
var logBuf bytes.Buffer
newTestBuilder := func(v *viper.Viper) *sitesBuilder { newTestBuilder := func(v *viper.Viper) *sitesBuilder {
v.Set("workingDir", workDir) v.Set("workingDir", workDir)
v.Set("disableKinds", []string{"taxonomy", "term", "page"}) v.Set("disableKinds", []string{"taxonomy", "term", "page"})
b := newTestSitesBuilder(t).WithLogger(loggers.NewWarningLogger()) logger := loggers.NewBasicLoggerForWriter(jww.LevelInfo, &logBuf)
b := newTestSitesBuilder(t).WithLogger(logger)
// Need to use OS fs for this. // Need to use OS fs for this.
b.Fs = hugofs.NewDefault(v) b.Fs = hugofs.NewDefault(v)
b.WithWorkingDir(workDir) b.WithWorkingDir(workDir)
@ -942,14 +950,10 @@ class-in-b {
b.Assert(os.Chdir(workDir), qt.IsNil) b.Assert(os.Chdir(workDir), qt.IsNil)
_, err = exec.Command("npm", "install").CombinedOutput() _, err = exec.Command("npm", "install").CombinedOutput()
b.Assert(err, qt.IsNil) b.Assert(err, qt.IsNil)
b.Build(BuildCfg{})
out, _ := captureStderr(func() error {
b.Build(BuildCfg{})
return nil
})
// Make sure Node sees this. // Make sure Node sees this.
b.Assert(out, qt.Contains, "Hugo Environment: production") b.Assert(logBuf.String(), qt.Contains, "Hugo Environment: production")
b.AssertFileContent("public/index.html", ` b.AssertFileContent("public/index.html", `
Styles RelPermalink: /css/styles.css Styles RelPermalink: /css/styles.css

View file

@ -531,8 +531,6 @@ func (c *Client) runGo(
return nil return nil
} }
//defer c.logger.PrintTimer(time.Now(), fmt.Sprint(args))
stderr := new(bytes.Buffer) stderr := new(bytes.Buffer)
cmd := exec.CommandContext(ctx, "go", args...) cmd := exec.CommandContext(ctx, "go", args...)

View file

@ -15,11 +15,12 @@ package babel
import ( import (
"io" "io"
"os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strconv" "strconv"
"github.com/gohugoio/hugo/common/loggers"
"github.com/gohugoio/hugo/common/hugo" "github.com/gohugoio/hugo/common/hugo"
"github.com/gohugoio/hugo/resources/internal" "github.com/gohugoio/hugo/resources/internal"
@ -157,7 +158,7 @@ func (t *babelTransformation) Transform(ctx *resources.ResourceTransformationCtx
cmd := exec.Command(binary, cmdArgs...) cmd := exec.Command(binary, cmdArgs...)
cmd.Stdout = ctx.To cmd.Stdout = ctx.To
cmd.Stderr = os.Stderr cmd.Stderr = loggers.LoggerToWriterWithPrefix(logger.INFO, "babel")
cmd.Env = hugo.GetExecEnviron(t.rs.Cfg) cmd.Env = hugo.GetExecEnviron(t.rs.Cfg)
stdin, err := cmd.StdinPipe() stdin, err := cmd.StdinPipe()

View file

@ -36,7 +36,6 @@ import (
"github.com/gohugoio/hugo/hugofs" "github.com/gohugoio/hugo/hugofs"
"github.com/pkg/errors" "github.com/pkg/errors"
"os"
"os/exec" "os/exec"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
@ -199,9 +198,10 @@ func (t *postcssTransformation) Transform(ctx *resources.ResourceTransformationC
cmd := exec.Command(binary, cmdArgs...) cmd := exec.Command(binary, cmdArgs...)
var errBuf bytes.Buffer var errBuf bytes.Buffer
infoW := loggers.LoggerToWriterWithPrefix(logger.INFO, "postcss")
cmd.Stdout = ctx.To cmd.Stdout = ctx.To
cmd.Stderr = io.MultiWriter(os.Stderr, &errBuf) cmd.Stderr = io.MultiWriter(infoW, &errBuf)
cmd.Env = hugo.GetExecEnviron(t.rs.Cfg) cmd.Env = hugo.GetExecEnviron(t.rs.Cfg)
stdin, err := cmd.StdinPipe() stdin, err := cmd.StdinPipe()