| 1 | package logg |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | stdlog "log" |
| 6 | "time" |
| 7 | |
| 8 | "github.com/bep/clocks" |
| 9 | ) |
| 10 | |
| 11 | // assert interface compliance. |
| 12 | var _ Logger = (*logger)(nil) |
| 13 | |
| 14 | // String implements fmt.Stringer and can be used directly in |
| 15 | // the log methods. |
| 16 | type String string |
| 17 | |
| 18 | // StringFunc is a function that returns a string. |
| 19 | // It also implements the fmt.Stringer interface and |
| 20 | // can therefore be used as argument to the log methods. |
| 21 | type StringFunc func() string |
| 22 | |
| 23 | func (f StringFunc) String() string { |
| 24 | return f() |
| 25 | } |
| 26 | |
| 27 | func (s String) String() string { |
| 28 | return string(s) |
| 29 | } |
| 30 | |
| 31 | // Fielder is an interface for providing fields to custom types. |
| 32 | type Fielder interface { |
| 33 | Fields() Fields |
| 34 | } |
| 35 | |
| 36 | func NewFieldsFunc(fn func() Fields) FieldsFunc { |
| 37 | return FieldsFunc(fn) |
| 38 | } |
| 39 | |
| 40 | type FieldsFunc func() Fields |
| 41 | |
| 42 | func (f FieldsFunc) Fields() Fields { |
| 43 | return f() |
| 44 | } |
| 45 | |
| 46 | // Field holds a named value. |
| 47 | type Field struct { |
| 48 | Name string `json:"name"` |
| 49 | Value any `json:"value"` |
| 50 | } |
| 51 | |
| 52 | // Fields represents a slice of entry level data used for structured logging. |
| 53 | type Fields []Field |
| 54 | |
| 55 | // Fields implements Fielder. |
| 56 | func (f Fields) Fields() Fields { |
| 57 | return f |
| 58 | } |
| 59 | |
| 60 | // Options is the set of options used to configure a logger. |
| 61 | type Options struct { |
| 62 | // Level is the minimum level to log at. |
| 63 | // If not set, defaults to InfoLevel. |
| 64 | Level Level |
| 65 | |
| 66 | // Handler is the log handler to use. |
| 67 | Handler Handler |
| 68 | |
| 69 | // Clock is the clock to use for timestamps. |
| 70 | // If not set, the system clock is used. |
| 71 | Clock Clock |
| 72 | } |
| 73 | |
| 74 | // New returns a new logger. |
| 75 | func New(cfg Options) Logger { |
| 76 | if cfg.Handler == nil { |
| 77 | panic("handler cannot be nil") |
| 78 | } |
| 79 | |
| 80 | if cfg.Level <= 0 || cfg.Level > LevelError { |
| 81 | panic("log level is out of range") |
| 82 | } |
| 83 | |
| 84 | if cfg.Clock == nil { |
| 85 | cfg.Clock = clocks.System() |
| 86 | } |
| 87 | |
| 88 | if cfg.Level == 0 { |
| 89 | cfg.Level = LevelInfo |
| 90 | } |
| 91 | |
| 92 | return &logger{ |
| 93 | Handler: cfg.Handler, |
| 94 | Level: cfg.Level, |
| 95 | Clock: cfg.Clock, |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // logger represents a logger with configurable Level and Handler. |
| 100 | type logger struct { |
| 101 | Handler Handler |
| 102 | Level Level |
| 103 | Clock Clock |
| 104 | } |
| 105 | |
| 106 | // Clock provides the current time. |
| 107 | type Clock interface { |
| 108 | Now() time.Time |
| 109 | } |
| 110 | |
| 111 | // WithLevel returns a new entry with `level` set. |
| 112 | func (l *logger) WithLevel(level Level) *Entry { |
| 113 | return NewEntry(l).WithLevel(level) |
| 114 | } |
| 115 | |
| 116 | // WithFields returns a new entry with `fields` set. |
| 117 | func (l *logger) WithFields(fields Fielder) *Entry { |
| 118 | return NewEntry(l).WithFields(fields.Fields()) |
| 119 | } |
| 120 | |
| 121 | // WithField returns a new entry with the `key` and `value` set. |
| 122 | // |
| 123 | // Note that the `key` should not have spaces in it - use camel |
| 124 | // case or underscores |
| 125 | func (l *logger) WithField(key string, value any) *Entry { |
| 126 | return NewEntry(l).WithField(key, value) |
| 127 | } |
| 128 | |
| 129 | // WithDuration returns a new entry with the "duration" field set |
| 130 | // to the given duration in milliseconds. |
| 131 | func (l *logger) WithDuration(d time.Duration) *Entry { |
| 132 | return NewEntry(l).WithDuration(d) |
| 133 | } |
| 134 | |
| 135 | // WithError returns a new entry with the "error" set to `err`. |
| 136 | func (l *logger) WithError(err error) *Entry { |
| 137 | return NewEntry(l).WithError(err) |
| 138 | } |
| 139 | |
| 140 | // ErrStopLogEntry is a sentinel error that can be returned from a |
| 141 | // handler to stop the entry from being passed to the next handler. |
| 142 | var ErrStopLogEntry = fmt.Errorf("stop log entry") |
| 143 | |
| 144 | // log the message, invoking the handler. |
| 145 | func (l *logger) log(e *Entry, s fmt.Stringer) { |
| 146 | if e.Level < l.Level { |
| 147 | return |
| 148 | } |
| 149 | |
| 150 | finalized := objectPools.GetEntry() |
| 151 | defer objectPools.PutEntry(finalized) |
| 152 | e.finalize(finalized, s.String()) |
| 153 | |
| 154 | if err := l.Handler.HandleLog(finalized); err != nil { |
| 155 | if err != ErrStopLogEntry { |
| 156 | stdlog.Printf("error logging: %s", err) |
| 157 | } |
| 158 | } |
| 159 | } |