Debian Codegraph @

handler.go

raw symbols SCIP
1 package humanlog
2
3 import (
4 "github.com/humanlogio/humanlog/internal/pkg/config"
5 "github.com/kr/logfmt"
6 )
7
8 // Handler can recognize it's log lines, parse them and prettify them.
9 type Handler interface {
10 CanHandle(line []byte) bool
11 Prettify(skipUnchanged bool) []byte
12 logfmt.Handler
13 }
14
15 var DefaultOptions = func() *HandlerOptions {
16 opts := &HandlerOptions{
17 TimeFields: []string{"time", "ts", "@timestamp", "timestamp", "Timestamp"},
18 MessageFields: []string{"message", "msg", "Body"},
19 LevelFields: []string{"level", "lvl", "loglevel", "severity", "SeverityText"},
20 }
21 return opts
22 }
23
24 type HandlerOptions struct {
25 TimeFields []string
26 MessageFields []string
27 LevelFields []string
28 }
29
30 var _ = HandlerOptionsFrom(config.DefaultConfig) // ensure it's valid
31
32 func HandlerOptionsFrom(cfg config.Config) *HandlerOptions {
33 opts := DefaultOptions()
34 if cfg.TimeFields != nil {
35 opts.TimeFields = appendUnique(opts.TimeFields, *cfg.TimeFields)
36 }
37 if cfg.MessageFields != nil {
38 opts.MessageFields = appendUnique(opts.MessageFields, *cfg.MessageFields)
39 }
40 if cfg.LevelFields != nil {
41 opts.LevelFields = appendUnique(opts.LevelFields, *cfg.LevelFields)
42 }
43 return opts
44 }
45
46 func appendUnique(a []string, b []string) []string {
47 // init with `len(b)` because usually `a` will be
48 // nil at first, but `b` wont be
49 seen := make(map[string]struct{}, len(b))
50 out := make([]string, 0, len(b))
51 for _, aa := range a {
52 if _, ok := seen[aa]; ok {
53 continue
54 }
55 seen[aa] = struct{}{}
56 out = append(out, aa)
57 }
58 for _, bb := range b {
59 if _, ok := seen[bb]; ok {
60 continue
61 }
62 seen[bb] = struct{}{}
63 out = append(out, bb)
64 }
65 return out
66 }