github.com/safing/portbase@v0.19.5/log/formatting.go (about)

     1  package log
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  )
     7  
     8  var counter uint16
     9  
    10  const (
    11  	maxCount   uint16 = 999
    12  	timeFormat string = "060102 15:04:05.000"
    13  )
    14  
    15  func (s Severity) String() string {
    16  	switch s {
    17  	case TraceLevel:
    18  		return "TRAC"
    19  	case DebugLevel:
    20  		return "DEBU"
    21  	case InfoLevel:
    22  		return "INFO"
    23  	case WarningLevel:
    24  		return "WARN"
    25  	case ErrorLevel:
    26  		return "ERRO"
    27  	case CriticalLevel:
    28  		return "CRIT"
    29  	default:
    30  		return "NONE"
    31  	}
    32  }
    33  
    34  func formatLine(line *logLine, duplicates uint64, useColor bool) string {
    35  	colorStart := ""
    36  	colorEnd := ""
    37  	if useColor {
    38  		colorStart = line.level.color()
    39  		colorEnd = endColor()
    40  	}
    41  
    42  	counter++
    43  
    44  	var fLine string
    45  	if line.line == 0 {
    46  		fLine = fmt.Sprintf("%s%s ? %s %s %03d%s%s %s", colorStart, line.timestamp.Format(timeFormat), rightArrow, line.level.String(), counter, formatDuplicates(duplicates), colorEnd, line.msg)
    47  	} else {
    48  		fLen := len(line.file)
    49  		fPartStart := fLen - 10
    50  		if fPartStart < 0 {
    51  			fPartStart = 0
    52  		}
    53  		fLine = fmt.Sprintf("%s%s %s:%03d %s %s %03d%s%s %s", colorStart, line.timestamp.Format(timeFormat), line.file[fPartStart:], line.line, rightArrow, line.level.String(), counter, formatDuplicates(duplicates), colorEnd, line.msg)
    54  	}
    55  
    56  	if line.tracer != nil {
    57  		// append full trace time
    58  		if len(line.tracer.logs) > 0 {
    59  			fLine += fmt.Sprintf(" Σ=%s", line.timestamp.Sub(line.tracer.logs[0].timestamp))
    60  		}
    61  
    62  		// append all trace actions
    63  		var d time.Duration
    64  		for i, action := range line.tracer.logs {
    65  			// set color
    66  			if useColor {
    67  				colorStart = action.level.color()
    68  			}
    69  			// set filename length
    70  			fLen := len(action.file)
    71  			fPartStart := fLen - 10
    72  			if fPartStart < 0 {
    73  				fPartStart = 0
    74  			}
    75  			// format
    76  			if i == len(line.tracer.logs)-1 { // last
    77  				d = line.timestamp.Sub(action.timestamp)
    78  			} else {
    79  				d = line.tracer.logs[i+1].timestamp.Sub(action.timestamp)
    80  			}
    81  			fLine += fmt.Sprintf("\n%s%19s %s:%03d %s %s%s     %s", colorStart, d, action.file[fPartStart:], action.line, rightArrow, action.level.String(), colorEnd, action.msg)
    82  		}
    83  	}
    84  
    85  	if counter >= maxCount {
    86  		counter = 0
    87  	}
    88  
    89  	return fLine
    90  }
    91  
    92  func formatDuplicates(duplicates uint64) string {
    93  	if duplicates == 0 {
    94  		return ""
    95  	}
    96  	return fmt.Sprintf(" [%dx]", duplicates+1)
    97  }