|
| 1 | +package logging |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "runtime" |
| 6 | + "strconv" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +type Level int |
| 11 | + |
| 12 | +const ( |
| 13 | + DebugLevel Level = -4 |
| 14 | + InfoLevel Level = 0 |
| 15 | + WarnLevel Level = 4 |
| 16 | + ErrorLevel Level = 8 |
| 17 | +) |
| 18 | + |
| 19 | +func (l Level) String() string { |
| 20 | + switch { |
| 21 | + case l < DebugLevel: |
| 22 | + return "DEBUG-" + strconv.Itoa(int(DebugLevel-l)) |
| 23 | + case l == DebugLevel: |
| 24 | + return "DEBUG" |
| 25 | + case l < InfoLevel: |
| 26 | + return "DEBUG+" + strconv.Itoa(int(l-DebugLevel)) |
| 27 | + case l == InfoLevel: |
| 28 | + return "INFO" |
| 29 | + case l < WarnLevel: |
| 30 | + return "INFO+" + strconv.Itoa(int(l-InfoLevel)) |
| 31 | + case l == WarnLevel: |
| 32 | + return "WARN" |
| 33 | + case l < ErrorLevel: |
| 34 | + return "WARN+" + strconv.Itoa(int(l-WarnLevel)) |
| 35 | + case l == ErrorLevel: |
| 36 | + return "ERROR" |
| 37 | + default: |
| 38 | + return "ERROR+" + strconv.Itoa(int(l-ErrorLevel)) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +type Attr struct { |
| 43 | + Key string |
| 44 | + Value any |
| 45 | +} |
| 46 | + |
| 47 | +type Record struct { |
| 48 | + Time time.Time |
| 49 | + Message string |
| 50 | + Level Level |
| 51 | + Context context.Context |
| 52 | + |
| 53 | + pc uintptr |
| 54 | + attrs []Attr |
| 55 | +} |
| 56 | + |
| 57 | +func NewRecord(t time.Time, level Level, msg string, ctx context.Context, attrs []Attr) Record { |
| 58 | + return Record{ |
| 59 | + Time: t, |
| 60 | + Message: msg, |
| 61 | + Level: level, |
| 62 | + Context: ctx, |
| 63 | + pc: pc(3), |
| 64 | + attrs: attrs, |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func (r Record) Attrs(fn func(Attr)) { |
| 69 | + for _, attr := range r.attrs { |
| 70 | + fn(attr) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +type Handler interface { |
| 75 | + Enabled(Level) bool |
| 76 | + Handle(Record) error |
| 77 | + WithAttrs([]Attr) Handler |
| 78 | +} |
| 79 | + |
| 80 | +type Logger interface { |
| 81 | + Debug(msg string, args ...any) |
| 82 | + Enabled(level Level) bool |
| 83 | + Error(msg string, err error, args ...any) |
| 84 | + Info(msg string, args ...any) |
| 85 | + Log(level Level, msg string, args ...any) |
| 86 | + Warn(msg string, args ...any) |
| 87 | + With(args ...any) Logger |
| 88 | + WithContext(ctx context.Context) Logger |
| 89 | +} |
| 90 | + |
| 91 | +type defaultLogger struct { |
| 92 | + handler Handler |
| 93 | + ctx context.Context |
| 94 | +} |
| 95 | + |
| 96 | +func NewLogger(handler Handler) Logger { |
| 97 | + return &defaultLogger{ |
| 98 | + handler: handler, |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func (l defaultLogger) Debug(msg string, args ...any) { |
| 103 | + l.Log(DebugLevel, msg, args...) |
| 104 | +} |
| 105 | + |
| 106 | +func (l defaultLogger) Enabled(level Level) bool { |
| 107 | + return l.handler.Enabled(level) |
| 108 | +} |
| 109 | + |
| 110 | +func (l defaultLogger) Warn(msg string, args ...any) { |
| 111 | + l.Log(WarnLevel, msg, args...) |
| 112 | +} |
| 113 | + |
| 114 | +func (l defaultLogger) Error(msg string, err error, args ...any) { |
| 115 | + if err != nil { |
| 116 | + args = append(args, Attr{Key: "err", Value: err}) |
| 117 | + } |
| 118 | + l.Log(ErrorLevel, msg, args...) |
| 119 | +} |
| 120 | + |
| 121 | +func (l defaultLogger) Info(msg string, args ...any) { |
| 122 | + l.Log(InfoLevel, msg, args...) |
| 123 | +} |
| 124 | + |
| 125 | +func (l defaultLogger) Log(level Level, msg string, args ...any) { |
| 126 | + if !l.Enabled(level) { |
| 127 | + return |
| 128 | + } |
| 129 | + record := NewRecord(time.Now(), level, msg, l.ctx, argsToAttrs(args)) |
| 130 | + _ = l.handler.Handle(record) |
| 131 | +} |
| 132 | + |
| 133 | +func (l defaultLogger) With(args ...any) Logger { |
| 134 | + attrs := argsToAttrs(args) |
| 135 | + return NewLogger(l.handler.WithAttrs(attrs)) |
| 136 | +} |
| 137 | + |
| 138 | +func (l defaultLogger) WithContext(ctx context.Context) Logger { |
| 139 | + l.ctx = ctx |
| 140 | + return l |
| 141 | +} |
| 142 | + |
| 143 | +// pc returns the program counter at the given stack depth. |
| 144 | +func pc(depth int) uintptr { |
| 145 | + var pcs [1]uintptr |
| 146 | + runtime.Callers(depth, pcs[:]) |
| 147 | + return pcs[0] |
| 148 | +} |
| 149 | + |
| 150 | +func argsToAttrs(args []any) (attrs []Attr) { |
| 151 | + for len(args) > 0 { |
| 152 | + switch arg := args[0].(type) { |
| 153 | + case string: |
| 154 | + if len(args) == 1 { |
| 155 | + attrs = append(attrs, Attr{Key: "BADKEY", Value: arg}) |
| 156 | + args = args[1:] |
| 157 | + } else { |
| 158 | + attrs = append(attrs, Attr{Key: arg, Value: args[1]}) |
| 159 | + args = args[2:] |
| 160 | + } |
| 161 | + case Attr: |
| 162 | + attrs = append(attrs, arg) |
| 163 | + args = args[1:] |
| 164 | + default: |
| 165 | + attrs = append(attrs, Attr{Key: "BADKEY", Value: arg}) |
| 166 | + args = args[1:] |
| 167 | + } |
| 168 | + } |
| 169 | + return attrs |
| 170 | +} |
0 commit comments