github.com/facebookincubator/go-belt@v0.0.0-20230703220935-39cd348f1a38/tool/logger/implementation/logrus/formatter/compact_text.go (about)

     1  // Copyright 2023 Meta Platforms, Inc. and affiliates.
     2  //
     3  // Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
     4  //
     5  // 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
     6  //
     7  // 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
     8  //
     9  // 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
    10  //
    11  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    12  
    13  package formatter
    14  
    15  import (
    16  	"fmt"
    17  	"path/filepath"
    18  	"sort"
    19  	"strings"
    20  	"time"
    21  
    22  	"github.com/sirupsen/logrus"
    23  )
    24  
    25  var logLevelSymbol []byte
    26  
    27  func init() {
    28  	logLevelSymbol = make([]byte, len(logrus.AllLevels)+1)
    29  	for _, level := range logrus.AllLevels {
    30  		logLevelSymbol[level] = strings.ToUpper(level.String()[:1])[0]
    31  	}
    32  }
    33  
    34  // CompactText is a logrus formatter which prints laconic lines, like
    35  // [2001-02-03T04:05:06Z W main.go:56] my message
    36  type CompactText struct {
    37  	TimestampFormat string
    38  	FieldAllowList  []string
    39  }
    40  
    41  // Format implements logrus.Formatter.
    42  func (f *CompactText) Format(entry *logrus.Entry) ([]byte, error) {
    43  	var str, header strings.Builder
    44  	timestamp := time.RFC3339
    45  	if f.TimestampFormat != "" {
    46  		timestamp = f.TimestampFormat
    47  	}
    48  	header.WriteString(fmt.Sprintf("%s %c",
    49  		entry.Time.Format(timestamp),
    50  		logLevelSymbol[entry.Level],
    51  	))
    52  	if entry.Caller != nil {
    53  		header.WriteString(fmt.Sprintf(" %s:%d", filepath.Base(entry.Caller.File), entry.Caller.Line))
    54  	}
    55  	str.WriteString(fmt.Sprintf("[%s] %s",
    56  		header.String(),
    57  		entry.Message,
    58  	))
    59  
    60  	keys := make([]string, 0, len(entry.Data))
    61  	for key := range entry.Data {
    62  		if f.FieldAllowList != nil {
    63  			found := false
    64  			for _, allowed := range f.FieldAllowList {
    65  				if key == allowed {
    66  					found = true
    67  					break
    68  				}
    69  			}
    70  			if !found {
    71  				continue
    72  			}
    73  		}
    74  		keys = append(keys, key)
    75  	}
    76  	sort.Strings(keys)
    77  
    78  	for _, key := range keys {
    79  		str.WriteString(fmt.Sprintf("\t%s=%v", key, entry.Data[key]))
    80  	}
    81  
    82  	str.WriteByte('\n')
    83  	return []byte(str.String()), nil
    84  }