github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/countlog/output/compact/compact.go (about)

     1  package compact
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/v2pro/plz/countlog/output"
     6  	"github.com/v2pro/plz/countlog/spi"
     7  	"github.com/v2pro/plz/msgfmt"
     8  	"strings"
     9  )
    10  
    11  type Format struct {
    12  }
    13  
    14  func (format *Format) FormatterOf(site *spi.LogSite) output.Formatter {
    15  	eventName := site.Event
    16  	sample := site.Sample
    17  	var formatters output.Formatters
    18  
    19  	formatters = append(formatters, &timestampFormatter{}, fixedFormatter(fmt.Sprintf(
    20  		"[%s] ", site.Location())))
    21  
    22  	if strings.HasPrefix(eventName, "event!") {
    23  		formatters = append(formatters, fixedFormatter(eventName[len("event!"):]))
    24  	} else if strings.HasPrefix(eventName, "callee!") {
    25  		tag := "call " + eventName[len("callee!"):]
    26  		formatters = append(formatters, fixedFormatter(tag))
    27  	} else {
    28  		formatters = append(formatters,
    29  			&defaultFormatter{msgfmt.FormatterOf(eventName, site.Sample)})
    30  	}
    31  
    32  	formatters = append(formatters, &errorFormatter{})
    33  	for i := 0; i < len(sample); i += 2 {
    34  		key := sample[i].(string)
    35  		pattern := "||" + key + "={" + key + "}"
    36  		formatters = append(formatters, &defaultFormatter{
    37  			msgfmt.FormatterOf(pattern, sample),
    38  		})
    39  	}
    40  
    41  	formatters = append(formatters, fixedFormatter("\n"))
    42  	return formatters
    43  }