Skip to content

Commit

Permalink
Improve stderr logginf for PostCSS and simlilar
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Aug 20, 2020
1 parent ae63c2b commit 75bd286
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
17 changes: 17 additions & 0 deletions common/loggers/loggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ func init() {
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.
type Logger struct {
*jww.Notepad
Expand Down
17 changes: 17 additions & 0 deletions common/loggers/loggers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
package loggers

import (
"bytes"
"fmt"
"log"
"testing"

qt "github.com/frankban/quicktest"
Expand All @@ -30,3 +33,17 @@ func TestLogger(t *testing.T) {
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")
}
2 changes: 0 additions & 2 deletions modules/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,8 +531,6 @@ func (c *Client) runGo(
return nil
}

//defer c.logger.PrintTimer(time.Now(), fmt.Sprint(args))

stderr := new(bytes.Buffer)
cmd := exec.CommandContext(ctx, "go", args...)

Expand Down
5 changes: 3 additions & 2 deletions resources/resource_transformers/babel/babel.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ package babel

import (
"io"
"os"
"os/exec"
"path/filepath"
"strconv"

"github.com/gohugoio/hugo/common/loggers"

"github.com/gohugoio/hugo/common/hugo"
"github.com/gohugoio/hugo/resources/internal"

Expand Down Expand Up @@ -157,7 +158,7 @@ func (t *babelTransformation) Transform(ctx *resources.ResourceTransformationCtx
cmd := exec.Command(binary, cmdArgs...)

cmd.Stdout = ctx.To
cmd.Stderr = os.Stderr
cmd.Stderr = loggers.LoggerToWriterWithPrefix(logger.INFO, "babel")
cmd.Env = hugo.GetExecEnviron(t.rs.Cfg)

stdin, err := cmd.StdinPipe()
Expand Down
4 changes: 2 additions & 2 deletions resources/resource_transformers/postcss/postcss.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
"github.com/gohugoio/hugo/hugofs"
"github.com/pkg/errors"

"os"
"os/exec"

"github.com/mitchellh/mapstructure"
Expand Down Expand Up @@ -199,9 +198,10 @@ func (t *postcssTransformation) Transform(ctx *resources.ResourceTransformationC
cmd := exec.Command(binary, cmdArgs...)

var errBuf bytes.Buffer
infoW := loggers.LoggerToWriterWithPrefix(logger.INFO, "postcss")

cmd.Stdout = ctx.To
cmd.Stderr = io.MultiWriter(os.Stderr, &errBuf)
cmd.Stderr = io.MultiWriter(infoW, &errBuf)
cmd.Env = hugo.GetExecEnviron(t.rs.Cfg)

stdin, err := cmd.StdinPipe()
Expand Down

0 comments on commit 75bd286

Please sign in to comment.