Debian Codegraph @

compact_printer.go

raw symbols SCIP
1 package jl
2
3 import (
4 "fmt"
5 "io"
6 "strings"
7 "unicode"
8 )
9
10 // CompactPrinter can print logs in a variety of compact formats, specified by FieldFormats.
11 type CompactPrinter struct {
12 Out io.Writer
13 // Disable colors disables adding color to fields.
14 DisableColor bool
15 // Disable truncate disables the Ellipsize and Truncate transforms.
16 DisableTruncate bool
17 // FieldFormats specifies the format the printer should use for logs. It defaults to DefaultCompactPrinterFieldFmt. Fields
18 // are formatted in the order they are provided. If a FieldFmt produces a field that does not end with a whitespace,
19 // a space character is automatically appended.
20 FieldFormats []FieldFmt
21 }
22
23 // FieldFmt specifies a single field formatted by the CompactPrinter.
24 type FieldFmt struct {
25 // Name of the field. This is used to find the field by key name if Finders is not set.
26 Name string
27 // List of FieldFinders to use to locate the field. Finders are executed in order until the first one that returns
28 // non-nil.
29 Finders []FieldFinder
30 // Takes the output of the Finder and turns it into a string. If not set, DefaultStringer is used.
31 Stringer Stringer
32 // List of transformers to run on the field found to format the field.
33 Transformers []Transformer
34 }
35
36 // DefaultCompactPrinterFieldFmt is a format for the CompactPrinter that tries to present logs in an easily skimmable manner
37 // for most types of logs.
38 var DefaultCompactPrinterFieldFmt = []FieldFmt{{
39 Name: "level",
40 Finders: []FieldFinder{ByNames("level", "severity", "logLevel")},
41 Transformers: []Transformer{Truncate(4), UpperCase, ColorMap(LevelColors)},
42 }, {
43 Name: "time",
44 Finders: []FieldFinder{ByNames("timestamp", "time", "ts")},
45 }, {
46 Name: "thread",
47 Transformers: []Transformer{Ellipsize(16), Format("[%s]"), RightPad(18), ColorSequence(AllColors)},
48 }, {
49 Name: "logger",
50 Finders: []FieldFinder{ByNames("logger", "caller")},
51 Transformers: []Transformer{Ellipsize(20), Format("%s|"), LeftPad(21), ColorSequence(AllColors)},
52 }, {
53 Name: "traceId",
54 Transformers: []Transformer{Format("%s|"), ColorSequence(AllColors)},
55 }, {
56 Name: "message",
57 Finders: []FieldFinder{ByNames("message", "msg", "textPayload", "jsonPayload.message")},
58 }, {
59 Name: "errors",
60 Finders: []FieldFinder{LogrusErrorFinder, ByNames("exceptions", "exception", "error")},
61 Stringer: ErrorStringer,
62 }}
63
64 // NewCompactPrinter allocates and returns a new compact printer.
65 func NewCompactPrinter(w io.Writer) *CompactPrinter {
66 return &CompactPrinter{
67 Out: w,
68 FieldFormats: DefaultCompactPrinterFieldFmt,
69 }
70 }
71
72 func (p *CompactPrinter) Print(entry *Entry) {
73 if entry.Partials == nil {
74 fmt.Fprintln(p.Out, string(entry.Raw))
75 return
76 }
77 for i, fieldFmt := range p.FieldFormats {
78 ctx := Context{
79 DisableColor: p.DisableColor,
80 DisableTruncate: p.DisableTruncate,
81 }
82 formattedField := fieldFmt.format(&ctx, entry)
83 if formattedField != "" {
84 if i != 0 && !strings.HasPrefix(formattedField, "\n") {
85 p.Out.Write([]byte(" "))
86 }
87 p.Out.Write([]byte(formattedField))
88 }
89 }
90 p.Out.Write([]byte("\n"))
91 }
92
93 func (f *FieldFmt) format(ctx *Context, entry *Entry) string {
94 var v interface{}
95 // Find the value
96 if len(f.Finders) > 0 {
97 for _, finder := range f.Finders {
98 if v = finder(entry); v != nil {
99 break
100 } else {
101 }
102 }
103 } else {
104 v = entry.Partials[f.Name]
105 }
106 if v == nil {
107 return ""
108 }
109
110 // Stringify the value
111 var s string
112 if f.Stringer != nil {
113 s = f.Stringer(ctx, v)
114 } else {
115 s = DefaultStringer(ctx, v)
116 }
117 s = strings.TrimRightFunc(s, unicode.IsSpace)
118
119 if s == "" {
120 return ""
121 }
122
123 original := s
124 ctx.Original = original
125 // Apply transforms
126 for _, transform := range f.Transformers {
127 s = transform.Transform(ctx, s)
128 }
129
130 if s == "" {
131 return ""
132 }
133
134 return s
135 }