mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-29 00:22:09 -05:00
parent
1ad117cbe2
commit
1c7b7b4ef2
3 changed files with 78 additions and 3 deletions
|
@ -80,7 +80,10 @@ func NewErrorLogger() *Logger {
|
||||||
return newBasicLogger(jww.LevelError)
|
return newBasicLogger(jww.LevelError)
|
||||||
}
|
}
|
||||||
|
|
||||||
var ansiColorRe = regexp.MustCompile("(?s)\\033\\[\\d*(;\\d*)*m")
|
var (
|
||||||
|
ansiColorRe = regexp.MustCompile("(?s)\\033\\[\\d*(;\\d*)*m")
|
||||||
|
errorRe = regexp.MustCompile("(ERROR|FATAL|WARN)")
|
||||||
|
)
|
||||||
|
|
||||||
type ansiCleaner struct {
|
type ansiCleaner struct {
|
||||||
w io.Writer
|
w io.Writer
|
||||||
|
@ -90,12 +93,40 @@ func (a ansiCleaner) Write(p []byte) (n int, err error) {
|
||||||
return a.w.Write(ansiColorRe.ReplaceAll(p, []byte("")))
|
return a.w.Write(ansiColorRe.ReplaceAll(p, []byte("")))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type labelColorizer struct {
|
||||||
|
w io.Writer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a labelColorizer) Write(p []byte) (n int, err error) {
|
||||||
|
replaced := errorRe.ReplaceAllStringFunc(string(p), func(m string) string {
|
||||||
|
switch m {
|
||||||
|
case "ERROR", "FATAL":
|
||||||
|
return terminal.Error(m)
|
||||||
|
case "WARN":
|
||||||
|
return terminal.Warning(m)
|
||||||
|
default:
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
})
|
||||||
|
// io.MultiWriter will abort if we return a bigger write count than input
|
||||||
|
// bytes, so we lie a little.
|
||||||
|
_, err = a.w.Write([]byte(replaced))
|
||||||
|
return len(p), err
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func newLogger(stdoutThreshold, logThreshold jww.Threshold, outHandle, logHandle io.Writer, saveErrors bool) *Logger {
|
func newLogger(stdoutThreshold, logThreshold jww.Threshold, outHandle, logHandle io.Writer, saveErrors bool) *Logger {
|
||||||
|
isTerm := terminal.IsTerminal(os.Stdout)
|
||||||
errorCounter := &jww.Counter{}
|
errorCounter := &jww.Counter{}
|
||||||
if logHandle != ioutil.Discard && terminal.IsTerminal(os.Stdout) {
|
if logHandle != ioutil.Discard && isTerm {
|
||||||
// Remove any Ansi coloring from log output
|
// Remove any Ansi coloring from log output
|
||||||
logHandle = ansiCleaner{w: logHandle}
|
logHandle = ansiCleaner{w: logHandle}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if isTerm {
|
||||||
|
outHandle = labelColorizer{w: outHandle}
|
||||||
|
}
|
||||||
|
|
||||||
listeners := []jww.LogListener{jww.LogCounter(errorCounter, jww.LevelError)}
|
listeners := []jww.LogListener{jww.LogCounter(errorCounter, jww.LevelError)}
|
||||||
var errorBuff *bytes.Buffer
|
var errorBuff *bytes.Buffer
|
||||||
if saveErrors {
|
if saveErrors {
|
||||||
|
|
32
common/loggers/loggers_test.go
Normal file
32
common/loggers/loggers_test.go
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
// Copyright 2018 The Hugo Authors. All rights reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package loggers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLogger(t *testing.T) {
|
||||||
|
assert := require.New(t)
|
||||||
|
l := NewWarningLogger()
|
||||||
|
|
||||||
|
l.ERROR.Println("One error")
|
||||||
|
l.ERROR.Println("Two error")
|
||||||
|
l.WARN.Println("A warning")
|
||||||
|
|
||||||
|
assert.Equal(uint64(2), l.ErrorCounter.Count())
|
||||||
|
|
||||||
|
}
|
|
@ -23,6 +23,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
errorColor = "\033[1;31m%s\033[0m"
|
||||||
|
warningColor = "\033[0;33m%s\033[0m"
|
||||||
noticeColor = "\033[1;36m%s\033[0m"
|
noticeColor = "\033[1;36m%s\033[0m"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -38,6 +40,16 @@ func Notice(s string) string {
|
||||||
return colorize(s, noticeColor)
|
return colorize(s, noticeColor)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Error colorizes the string in a colour that grabs attention.
|
||||||
|
func Error(s string) string {
|
||||||
|
return colorize(s, errorColor)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Warning colorizes the string in a colour that warns.
|
||||||
|
func Warning(s string) string {
|
||||||
|
return colorize(s, warningColor)
|
||||||
|
}
|
||||||
|
|
||||||
// colorize s in color.
|
// colorize s in color.
|
||||||
func colorize(s, color string) string {
|
func colorize(s, color string) string {
|
||||||
s = fmt.Sprintf(color, doublePercent(s))
|
s = fmt.Sprintf(color, doublePercent(s))
|
||||||
|
|
Loading…
Reference in a new issue