pax_global_header00006660000000000000000000000064140216105230014504gustar00rootroot0000000000000052 comment=d45de036a3c8949904561f02f208c80df6f3fd8d nested-logrus-formatter-1.3.1/000077500000000000000000000000001402161052300163025ustar00rootroot00000000000000nested-logrus-formatter-1.3.1/.gitignore000066400000000000000000000003371402161052300202750ustar00rootroot00000000000000# Binaries for programs and plugins *.exe *.exe~ *.dll *.so *.dylib # Test binary, build with `go test -c` *.test # Output of the go coverage tool, specifically when used with LiteIDE *.out # IDE *.code-workspace .idea/ nested-logrus-formatter-1.3.1/.travis.yml000066400000000000000000000005401402161052300204120ustar00rootroot00000000000000language: go go: - "1.10" - "1.11" - "1.12" - "1.13" - "1.14" - master before_script: - go get golang.org/x/tools/cmd/cover - go get github.com/mattn/goveralls - go get -t github.com/sirupsen/logrus after_success: - make cover - $GOPATH/bin/goveralls -coverprofile=coverage.out -service=travis-ci -repotoken "$COVERALLS_TOKEN" nested-logrus-formatter-1.3.1/LICENSE000066400000000000000000000020551402161052300173110ustar00rootroot00000000000000MIT License Copyright (c) 2018 Anton Fisher Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. nested-logrus-formatter-1.3.1/Makefile000066400000000000000000000003371402161052300177450ustar00rootroot00000000000000# nested-logrus-formatter .PHONY: all all: test demo .PHONY: test test: go test ./tests/* -v -count 1 cover: go test ./tests/* -v -covermode=count -coverprofile=coverage.out .PHONY: demo demo: go run example/main.go nested-logrus-formatter-1.3.1/README.md000066400000000000000000000042621402161052300175650ustar00rootroot00000000000000# nested-logrus-formatter [![Build Status](https://travis-ci.org/antonfisher/nested-logrus-formatter.svg?branch=master)](https://travis-ci.org/antonfisher/nested-logrus-formatter) [![Go Report Card](https://goreportcard.com/badge/github.com/antonfisher/nested-logrus-formatter)](https://goreportcard.com/report/github.com/antonfisher/nested-logrus-formatter) [![GoDoc](https://godoc.org/github.com/antonfisher/nested-logrus-formatter?status.svg)](https://godoc.org/github.com/antonfisher/nested-logrus-formatter) Human-readable log formatter, converts _logrus_ fields to a nested structure: ![Screenshot](https://raw.githubusercontent.com/antonfisher/nested-logrus-formatter/docs/images/demo.png) ## Configuration: ```go type Formatter struct { // FieldsOrder - default: fields sorted alphabetically FieldsOrder []string // TimestampFormat - default: time.StampMilli = "Jan _2 15:04:05.000" TimestampFormat string // HideKeys - show [fieldValue] instead of [fieldKey:fieldValue] HideKeys bool // NoColors - disable colors NoColors bool // NoFieldsColors - apply colors only to the level, default is level + fields NoFieldsColors bool // NoFieldsSpace - no space between fields NoFieldsSpace bool // ShowFullLevel - show a full level [WARNING] instead of [WARN] ShowFullLevel bool // NoUppercaseLevel - no upper case for level value NoUppercaseLevel bool // TrimMessages - trim whitespaces on messages TrimMessages bool // CallerFirst - print caller info first CallerFirst bool // CustomCallerFormatter - set custom formatter for caller info CustomCallerFormatter func(*runtime.Frame) string } ``` ## Usage ```go import ( nested "github.com/antonfisher/nested-logrus-formatter" "github.com/sirupsen/logrus" ) log := logrus.New() log.SetFormatter(&nested.Formatter{ HideKeys: true, FieldsOrder: []string{"component", "category"}, }) log.Info("just info message") // Output: Jan _2 15:04:05.000 [INFO] just info message log.WithField("component", "rest").Warn("warn message") // Output: Jan _2 15:04:05.000 [WARN] [rest] warn message ``` See more examples in the [tests](./tests/formatter_test.go) file. ## Development ```bash # run tests: make test # run demo: make demo ``` nested-logrus-formatter-1.3.1/example/000077500000000000000000000000001402161052300177355ustar00rootroot00000000000000nested-logrus-formatter-1.3.1/example/main.go000066400000000000000000000021571402161052300212150ustar00rootroot00000000000000package main import ( "fmt" "github.com/antonfisher/nested-logrus-formatter" "github.com/sirupsen/logrus" ) func main() { fmt.Print("\n--- nested-logrus-formatter ---\n\n") printDemo(&formatter.Formatter{ HideKeys: true, FieldsOrder: []string{"component", "category", "req"}, }, "nested-logrus-formatter") fmt.Print("\n--- default logrus formatter ---\n\n") printDemo(nil, "default logrus formatter") } func printDemo(f logrus.Formatter, title string) { l := logrus.New() l.SetLevel(logrus.DebugLevel) if f != nil { l.SetFormatter(f) } // enable/disable file/function name l.SetReportCaller(false) l.Infof("this is %v demo", title) lWebServer := l.WithField("component", "web-server") lWebServer.Info("starting...") lWebServerReq := lWebServer.WithFields(logrus.Fields{ "req": "GET /api/stats", "reqId": "#1", }) lWebServerReq.Info("params: startYear=2048") lWebServerReq.Error("response: 400 Bad Request") lDbConnector := l.WithField("category", "db-connector") lDbConnector.Info("connecting to db on 10.10.10.13...") lDbConnector.Warn("connection took 10s") l.Info("demo end.") } nested-logrus-formatter-1.3.1/formatter.go000066400000000000000000000103221402161052300206320ustar00rootroot00000000000000package formatter import ( "bytes" "fmt" "runtime" "sort" "strings" "time" "github.com/sirupsen/logrus" ) // Formatter - logrus formatter, implements logrus.Formatter type Formatter struct { // FieldsOrder - default: fields sorted alphabetically FieldsOrder []string // TimestampFormat - default: time.StampMilli = "Jan _2 15:04:05.000" TimestampFormat string // HideKeys - show [fieldValue] instead of [fieldKey:fieldValue] HideKeys bool // NoColors - disable colors NoColors bool // NoFieldsColors - apply colors only to the level, default is level + fields NoFieldsColors bool // NoFieldsSpace - no space between fields NoFieldsSpace bool // ShowFullLevel - show a full level [WARNING] instead of [WARN] ShowFullLevel bool // NoUppercaseLevel - no upper case for level value NoUppercaseLevel bool // TrimMessages - trim whitespaces on messages TrimMessages bool // CallerFirst - print caller info first CallerFirst bool // CustomCallerFormatter - set custom formatter for caller info CustomCallerFormatter func(*runtime.Frame) string } // Format an log entry func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error) { levelColor := getColorByLevel(entry.Level) timestampFormat := f.TimestampFormat if timestampFormat == "" { timestampFormat = time.StampMilli } // output buffer b := &bytes.Buffer{} // write time b.WriteString(entry.Time.Format(timestampFormat)) // write level var level string if f.NoUppercaseLevel { level = entry.Level.String() } else { level = strings.ToUpper(entry.Level.String()) } if f.CallerFirst { f.writeCaller(b, entry) } if !f.NoColors { fmt.Fprintf(b, "\x1b[%dm", levelColor) } b.WriteString(" [") if f.ShowFullLevel { b.WriteString(level) } else { b.WriteString(level[:4]) } b.WriteString("]") if !f.NoFieldsSpace { b.WriteString(" ") } if !f.NoColors && f.NoFieldsColors { b.WriteString("\x1b[0m") } // write fields if f.FieldsOrder == nil { f.writeFields(b, entry) } else { f.writeOrderedFields(b, entry) } if f.NoFieldsSpace { b.WriteString(" ") } if !f.NoColors && !f.NoFieldsColors { b.WriteString("\x1b[0m") } // write message if f.TrimMessages { b.WriteString(strings.TrimSpace(entry.Message)) } else { b.WriteString(entry.Message) } if !f.CallerFirst { f.writeCaller(b, entry) } b.WriteByte('\n') return b.Bytes(), nil } func (f *Formatter) writeCaller(b *bytes.Buffer, entry *logrus.Entry) { if entry.HasCaller() { if f.CustomCallerFormatter != nil { fmt.Fprintf(b, f.CustomCallerFormatter(entry.Caller)) } else { fmt.Fprintf( b, " (%s:%d %s)", entry.Caller.File, entry.Caller.Line, entry.Caller.Function, ) } } } func (f *Formatter) writeFields(b *bytes.Buffer, entry *logrus.Entry) { if len(entry.Data) != 0 { fields := make([]string, 0, len(entry.Data)) for field := range entry.Data { fields = append(fields, field) } sort.Strings(fields) for _, field := range fields { f.writeField(b, entry, field) } } } func (f *Formatter) writeOrderedFields(b *bytes.Buffer, entry *logrus.Entry) { length := len(entry.Data) foundFieldsMap := map[string]bool{} for _, field := range f.FieldsOrder { if _, ok := entry.Data[field]; ok { foundFieldsMap[field] = true length-- f.writeField(b, entry, field) } } if length > 0 { notFoundFields := make([]string, 0, length) for field := range entry.Data { if foundFieldsMap[field] == false { notFoundFields = append(notFoundFields, field) } } sort.Strings(notFoundFields) for _, field := range notFoundFields { f.writeField(b, entry, field) } } } func (f *Formatter) writeField(b *bytes.Buffer, entry *logrus.Entry, field string) { if f.HideKeys { fmt.Fprintf(b, "[%v]", entry.Data[field]) } else { fmt.Fprintf(b, "[%s:%v]", field, entry.Data[field]) } if !f.NoFieldsSpace { b.WriteString(" ") } } const ( colorRed = 31 colorYellow = 33 colorBlue = 36 colorGray = 37 ) func getColorByLevel(level logrus.Level) int { switch level { case logrus.DebugLevel, logrus.TraceLevel: return colorGray case logrus.WarnLevel: return colorYellow case logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel: return colorRed default: return colorBlue } } nested-logrus-formatter-1.3.1/tests/000077500000000000000000000000001402161052300174445ustar00rootroot00000000000000nested-logrus-formatter-1.3.1/tests/formatter_test.go000066400000000000000000000155601402161052300230440ustar00rootroot00000000000000package formatter_test import ( "bytes" "fmt" "os" "path" "regexp" "runtime" "strings" "testing" formatter "github.com/antonfisher/nested-logrus-formatter" "github.com/sirupsen/logrus" ) func ExampleFormatter_Format_default() { l := logrus.New() l.SetOutput(os.Stdout) l.SetLevel(logrus.DebugLevel) l.SetFormatter(&formatter.Formatter{ NoColors: true, TimestampFormat: "-", }) l.Debug("test1") l.Info("test2") l.Warn("test3") l.Error("test4") // Output: // - [DEBU] test1 // - [INFO] test2 // - [WARN] test3 // - [ERRO] test4 } func ExampleFormatter_Format_full_level() { l := logrus.New() l.SetOutput(os.Stdout) l.SetLevel(logrus.DebugLevel) l.SetFormatter(&formatter.Formatter{ NoColors: true, TimestampFormat: "-", ShowFullLevel: true, }) l.Debug("test1") l.Info("test2") l.Warn("test3") l.Error(" test4") // Output: // - [DEBUG] test1 // - [INFO] test2 // - [WARNING] test3 // - [ERROR] test4 } func ExampleFormatter_Format_show_keys() { l := logrus.New() l.SetOutput(os.Stdout) l.SetLevel(logrus.DebugLevel) l.SetFormatter(&formatter.Formatter{ NoColors: true, TimestampFormat: "-", HideKeys: false, }) ll := l.WithField("category", "rest") l.Info("test1") ll.Info("test2") // Output: // - [INFO] test1 // - [INFO] [category:rest] test2 } func ExampleFormatter_Format_hide_keys() { l := logrus.New() l.SetOutput(os.Stdout) l.SetLevel(logrus.DebugLevel) l.SetFormatter(&formatter.Formatter{ NoColors: true, TimestampFormat: "-", HideKeys: true, }) ll := l.WithField("category", "rest") l.Info("test1") ll.Info("test2") // Output: // - [INFO] test1 // - [INFO] [rest] test2 } func ExampleFormatter_Format_sort_order() { l := logrus.New() l.SetOutput(os.Stdout) l.SetLevel(logrus.DebugLevel) l.SetFormatter(&formatter.Formatter{ NoColors: true, TimestampFormat: "-", HideKeys: false, }) ll := l.WithField("component", "main") lll := ll.WithField("category", "rest") l.Info("test1") ll.Info("test2") lll.Info("test3") // Output: // - [INFO] test1 // - [INFO] [component:main] test2 // - [INFO] [category:rest] [component:main] test3 } func ExampleFormatter_Format_field_order() { l := logrus.New() l.SetOutput(os.Stdout) l.SetLevel(logrus.DebugLevel) l.SetFormatter(&formatter.Formatter{ NoColors: true, TimestampFormat: "-", FieldsOrder: []string{"component", "category"}, HideKeys: false, }) ll := l.WithField("component", "main") lll := ll.WithField("category", "rest") l.Info("test1") ll.Info("test2") lll.Info("test3") // Output: // - [INFO] test1 // - [INFO] [component:main] test2 // - [INFO] [component:main] [category:rest] test3 } func ExampleFormatter_Format_no_fields_space() { l := logrus.New() l.SetOutput(os.Stdout) l.SetLevel(logrus.DebugLevel) l.SetFormatter(&formatter.Formatter{ NoColors: true, TimestampFormat: "-", FieldsOrder: []string{"component", "category"}, HideKeys: false, NoFieldsSpace: true, }) ll := l.WithField("component", "main") lll := ll.WithField("category", "rest") l.Info("test1") ll.Info("test2") lll.Info("test3") // Output: // - [INFO] test1 // - [INFO][component:main] test2 // - [INFO][component:main][category:rest] test3 } func ExampleFormatter_Format_no_uppercase_level() { l := logrus.New() l.SetOutput(os.Stdout) l.SetLevel(logrus.DebugLevel) l.SetFormatter(&formatter.Formatter{ NoColors: true, TimestampFormat: "-", FieldsOrder: []string{"component", "category"}, NoUppercaseLevel: true, }) ll := l.WithField("component", "main") lll := ll.WithField("category", "rest") llll := ll.WithField("category", "other") l.Debug("test1") ll.Info("test2") lll.Warn("test3") llll.Error("test4") // Output: // - [debu] test1 // - [info] [component:main] test2 // - [warn] [component:main] [category:rest] test3 // - [erro] [component:main] [category:other] test4 } func ExampleFormatter_Format_trim_message() { l := logrus.New() l.SetOutput(os.Stdout) l.SetLevel(logrus.DebugLevel) l.SetFormatter(&formatter.Formatter{ TrimMessages: true, NoColors: true, TimestampFormat: "-", }) l.Debug(" test1 ") l.Info("test2 ") l.Warn(" test3") l.Error(" test4 ") // Output: // - [DEBU] test1 // - [INFO] test2 // - [WARN] test3 // - [ERRO] test4 } func TestFormatter_Format_with_report_caller(t *testing.T) { output := bytes.NewBuffer([]byte{}) l := logrus.New() l.SetOutput(output) l.SetLevel(logrus.DebugLevel) l.SetFormatter(&formatter.Formatter{ NoColors: true, TimestampFormat: "-", }) l.SetReportCaller(true) l.Debug("test1") line, err := output.ReadString('\n') if err != nil { t.Errorf("Cannot read log output: %v", err) } expectedRegExp := "- \\[DEBU\\] test1 \\(.+\\.go:[0-9]+ .+\\)\n$" match, err := regexp.MatchString( expectedRegExp, line, ) if err != nil { t.Errorf("Cannot check regexp: %v", err) } else if !match { t.Errorf( "logger.SetReportCaller(true) output doesn't match, expected: %s to find in: '%s'", expectedRegExp, line, ) } } func TestFormatter_Format_with_report_caller_and_CallerFirst_true(t *testing.T) { output := bytes.NewBuffer([]byte{}) l := logrus.New() l.SetOutput(output) l.SetLevel(logrus.DebugLevel) l.SetFormatter(&formatter.Formatter{ NoColors: true, TimestampFormat: "-", CallerFirst: true, }) l.SetReportCaller(true) l.Debug("test1") line, err := output.ReadString('\n') if err != nil { t.Errorf("Cannot read log output: %v", err) } expectedRegExp := "- \\(.+\\.go:[0-9]+ .+\\) \\[DEBU\\] test1\n$" match, err := regexp.MatchString( expectedRegExp, line, ) if err != nil { t.Errorf("Cannot check regexp: %v", err) } else if !match { t.Errorf( "logger.SetReportCaller(true) output doesn't match, expected: %s to find in: '%s'", expectedRegExp, line, ) } } func TestFormatter_Format_with_report_caller_and_CustomCallerFormatter(t *testing.T) { output := bytes.NewBuffer([]byte{}) l := logrus.New() l.SetOutput(output) l.SetLevel(logrus.DebugLevel) l.SetFormatter(&formatter.Formatter{ NoColors: true, TimestampFormat: "-", CallerFirst: true, CustomCallerFormatter: func(f *runtime.Frame) string { s := strings.Split(f.Function, ".") funcName := s[len(s)-1] return fmt.Sprintf(" [%s:%d][%s()]", path.Base(f.File), f.Line, funcName) }, }) l.SetReportCaller(true) l.Debug("test1") line, err := output.ReadString('\n') if err != nil { t.Errorf("Cannot read log output: %v", err) } expectedRegExp := "- \\[.+\\.go:[0-9]+\\]\\[.+\\(\\)\\] \\[DEBU\\] test1\n$" match, err := regexp.MatchString( expectedRegExp, line, ) if err != nil { t.Errorf("Cannot check regexp: %v", err) } else if !match { t.Errorf( "logger.SetReportCaller(true) output doesn't match, expected: %s to find in: '%s'", expectedRegExp, line, ) } }