github.com/neohugo/neohugo@v0.123.8/common/loggers/handlerdefault.go (about)

     1  // Copyright 2024 The Hugo Authors. All rights reserved.
     2  // Some functions in this file (see comments) is based on the Go source code,
     3  // copyright The Go Authors and  governed by a BSD-style license.
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  // http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  // package loggers contains some basic logging setup.
    17  package loggers
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"strings"
    23  	"sync"
    24  
    25  	"github.com/bep/logg"
    26  
    27  	"github.com/fatih/color"
    28  )
    29  
    30  // levelColor mapping.
    31  var levelColor = [...]*color.Color{
    32  	logg.LevelTrace: color.New(color.FgWhite),
    33  	logg.LevelDebug: color.New(color.FgWhite),
    34  	logg.LevelInfo:  color.New(color.FgBlue),
    35  	logg.LevelWarn:  color.New(color.FgYellow),
    36  	logg.LevelError: color.New(color.FgRed),
    37  }
    38  
    39  // levelString mapping.
    40  var levelString = [...]string{
    41  	logg.LevelTrace: "TRACE",
    42  	logg.LevelDebug: "DEBUG",
    43  	logg.LevelInfo:  "INFO ",
    44  	logg.LevelWarn:  "WARN ",
    45  	logg.LevelError: "ERROR",
    46  }
    47  
    48  // newDefaultHandler handler.
    49  func newDefaultHandler(outWriter, errWriter io.Writer) logg.Handler {
    50  	return &defaultHandler{
    51  		outWriter: outWriter,
    52  		errWriter: errWriter,
    53  		Padding:   0,
    54  	}
    55  }
    56  
    57  // Default Handler implementation.
    58  // Based on https://github.com/apex/log/blob/master/handlers/cli/cli.go
    59  type defaultHandler struct {
    60  	mu        sync.Mutex
    61  	outWriter io.Writer // Defaults to os.Stdout.
    62  	errWriter io.Writer // Defaults to os.Stderr.
    63  
    64  	Padding int
    65  }
    66  
    67  // HandleLog implements logg.Handler.
    68  func (h *defaultHandler) HandleLog(e *logg.Entry) error {
    69  	color := levelColor[e.Level]
    70  	level := levelString[e.Level]
    71  
    72  	h.mu.Lock()
    73  	defer h.mu.Unlock()
    74  
    75  	var w io.Writer
    76  	if e.Level > logg.LevelInfo {
    77  		w = h.errWriter
    78  	} else {
    79  		w = h.outWriter
    80  	}
    81  
    82  	var prefix string
    83  	for _, field := range e.Fields {
    84  		if field.Name == FieldNameCmd {
    85  			prefix = fmt.Sprint(field.Value)
    86  			break
    87  		}
    88  	}
    89  
    90  	if prefix != "" {
    91  		prefix = prefix + ": "
    92  	}
    93  
    94  	color.Fprintf(w, "%s %s%s", fmt.Sprintf("%*s", h.Padding+1, level), color.Sprint(prefix), e.Message)
    95  
    96  	for _, field := range e.Fields {
    97  		if strings.HasPrefix(field.Name, reservedFieldNamePrefix) {
    98  			continue
    99  		}
   100  		fmt.Fprintf(w, " %s %v", color.Sprint(field.Name), field.Value)
   101  	}
   102  
   103  	fmt.Fprintln(w)
   104  
   105  	return nil
   106  }