Skip to content
Snippets Groups Projects
Select Git revision
  • 6b2a0a5fe39c0d21c0083560664b08a134ab7a12
  • main default protected
  • trading_hours
  • refactor_trading_hours
  • audit_cleaning_cater_for_non_struct_fields
  • remove-info-logs
  • sl-refactor
  • 18-use-scan-for-param-values
  • 17-order-search-results
  • 4-simplify-framework-2
  • 1-http-error
  • v1.297.0
  • v1.296.0
  • v1.295.0
  • v1.294.0
  • v1.293.0
  • v1.292.0
  • v1.291.0
  • v1.290.0
  • v1.289.0
  • v1.288.0
  • v1.287.0
  • v1.286.0
  • v1.285.0
  • v1.284.0
  • v1.283.0
  • v1.282.0
  • v1.281.0
  • v1.280.0
  • v1.279.0
  • v1.278.0
31 results

utils.go

Blame
  • format.go 3.23 KiB
    package logger
    
    import (
    	"bytes"
    	"encoding/json"
    	"fmt"
    
    	"github.com/fatih/color"
    )
    
    var nextFormatID = 1
    
    type IFormatter interface {
    	Format(Entry) []byte
    	NextColor() IFormatter
    	Color() string
    }
    
    type formatterJSON struct{}
    
    func (f formatterJSON) Format(entry Entry) []byte {
    	jsonEntry, err := json.Marshal(entry)
    	if err != nil {
    		return []byte(fmt.Sprintf("failed to marshal entry: %v: %+v\n", err, entry))
    	}
    	return append(jsonEntry, []byte("\n")...)
    }
    
    func (f formatterJSON) NextColor() IFormatter {
    	return f //do not select colors for JSON (only used in console)
    }
    
    func (f formatterJSON) Color() string {
    	return "default"
    }
    
    func NewConsole() IFormatter {
    	nextFormatID++
    	return formatterConsole{
    		id: nextFormatID,
    		fg: 1, //color.FgWhite,
    		bg: 0, //color.BgBlack,
    	}
    }
    
    type formatterConsole struct {
    	id int
    	fg int //color.Attribute
    	bg int //color.Attribute
    }
    
    func (f formatterConsole) Format(entry Entry) []byte {
    	source := fmt.Sprintf("%s/%s:%d", entry.Caller.Package, entry.Caller.File, entry.Caller.Line)
    	if len(source) > 40 {
    		source = source[len(source)-40:]
    	}
    
    	buffer := bytes.NewBuffer(nil)
    
    	red := color.New(color.FgRed).FprintfFunc()
    	magenta := color.New(color.FgMagenta).FprintfFunc()
    	yellow := color.New(color.FgYellow).FprintfFunc()
    	green := color.New(color.FgGreen).FprintfFunc()
    	cyan := color.New(color.FgCyan).FprintfFunc()
    
    	cyan(buffer, entry.Timestamp.Format("2006-01-02 15:04:05"))
    
    	levelString := fmt.Sprintf(" %5.5s", entry.Level)
    	switch entry.Level {
    	case LevelFatal: