|
| 1 | +package teapot |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "os" |
| 6 | + "runtime" |
| 7 | + "strconv" |
| 8 | + "sync" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +// Option |
| 13 | +type Option func(*Logger) |
| 14 | + |
| 15 | +// WithLevel |
| 16 | +func WithLevel(level Level) Option { return func(l *Logger) { l.lvl = level } } |
| 17 | + |
| 18 | +// WithWriter |
| 19 | +func WithWriter(w io.Writer) Option { return func(l *Logger) { l.w = w } } |
| 20 | + |
| 21 | +// Logger |
| 22 | +type Logger struct { |
| 23 | + mu sync.Mutex |
| 24 | + p *sync.Pool |
| 25 | + |
| 26 | + w io.Writer |
| 27 | + |
| 28 | + lvl Level |
| 29 | + f Format |
| 30 | +} |
| 31 | + |
| 32 | +// New |
| 33 | +func New(opts ...Option) *Logger { |
| 34 | + l := &Logger{ |
| 35 | + w: os.Stdout, |
| 36 | + lvl: DEBUG, |
| 37 | + f: JSON, |
| 38 | + mu: sync.Mutex{}, |
| 39 | + p: &sync.Pool{ |
| 40 | + New: func() any { |
| 41 | + buf := make([]byte, 0, 1024) |
| 42 | + return &buf |
| 43 | + }, |
| 44 | + }, |
| 45 | + } |
| 46 | + for _, o := range opts { |
| 47 | + o(l) |
| 48 | + } |
| 49 | + return l |
| 50 | +} |
| 51 | + |
| 52 | +// Debug |
| 53 | +func (l *Logger) Debug(format string, attrs ...Attr) { |
| 54 | + l.printf(format, DEBUG, attrs...) |
| 55 | +} |
| 56 | + |
| 57 | +// Debugf |
| 58 | +func (l *Logger) Debugf(format string, attrs ...Attr) { |
| 59 | + l.printf(format, INFO, attrs...) |
| 60 | +} |
| 61 | + |
| 62 | +// Info |
| 63 | +func (l *Logger) Info(format string) { |
| 64 | + l.printf(format, INFO) |
| 65 | +} |
| 66 | + |
| 67 | +// Infof |
| 68 | +func (l *Logger) Infof(format string, attrs ...Attr) { |
| 69 | + l.printf(format, INFO, attrs...) |
| 70 | +} |
| 71 | + |
| 72 | +// Error |
| 73 | +func (l *Logger) Error(format string, attrs ...Attr) { |
| 74 | + l.printf(format, ERROR, attrs...) |
| 75 | +} |
| 76 | + |
| 77 | +// Fatal |
| 78 | +func (l *Logger) Fatal(format string, attrs ...Attr) { |
| 79 | + l.printf(format, FATAL, attrs...) |
| 80 | + os.Exit(1) |
| 81 | +} |
| 82 | + |
| 83 | +func (l *Logger) printf(format string, level Level, attrs ...Attr) { |
| 84 | + if level < l.lvl { |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + ptr := l.p.Get().(*[]byte) |
| 89 | + buf := (*ptr)[:0] |
| 90 | + |
| 91 | + switch l.f { |
| 92 | + // TODO |
| 93 | + // implement text logger |
| 94 | + // case TEXT: |
| 95 | + case JSON: |
| 96 | + buf = append(buf, `{"time":"`...) |
| 97 | + buf = time.Now().UTC().AppendFormat(buf, time.RFC3339Nano) |
| 98 | + buf = append(buf, `", "level":"`...) |
| 99 | + buf = append(buf, levelName[l.lvl]...) |
| 100 | + buf = append(buf, `", "message":"`...) |
| 101 | + buf = append(buf, format...) |
| 102 | + buf = append(buf, `"`...) |
| 103 | + for _, attr := range attrs { |
| 104 | + buf = append(buf, `,"`...) |
| 105 | + buf = append(buf, attr.key...) |
| 106 | + buf = append(buf, `":`...) |
| 107 | + |
| 108 | + switch attr.kfield { |
| 109 | + case kint: |
| 110 | + buf = strconv.AppendInt(buf, int64(attr.intValue), 10) |
| 111 | + case kint16: |
| 112 | + buf = strconv.AppendInt(buf, int64(attr.int16Value), 10) |
| 113 | + case kint32: |
| 114 | + buf = strconv.AppendInt(buf, int64(attr.int32Value), 10) |
| 115 | + case kint64: |
| 116 | + buf = strconv.AppendInt(buf, attr.int64Value, 10) |
| 117 | + case kbool: |
| 118 | + buf = strconv.AppendBool(buf, attr.boolValue) |
| 119 | + case kstring: |
| 120 | + buf = strconv.AppendQuote(buf, attr.stringValue) |
| 121 | + case kerr: |
| 122 | + buf = strconv.AppendQuote(buf, attr.errValue.Error()) |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + if level >= ERROR { |
| 127 | + buf = append(buf, `, "stacktrace":`...) |
| 128 | + |
| 129 | + var sb = make([]byte, 2048) |
| 130 | + n := runtime.Stack(sb, false) |
| 131 | + buf = strconv.AppendQuote(buf, string(sb[:n])) |
| 132 | + } |
| 133 | + |
| 134 | + buf = append(buf, "}"...) |
| 135 | + default: |
| 136 | + return |
| 137 | + } |
| 138 | + |
| 139 | + buf = append(buf, '\n') |
| 140 | + |
| 141 | + l.mu.Lock() |
| 142 | + l.w.Write(buf) |
| 143 | + l.mu.Unlock() |
| 144 | + |
| 145 | + *ptr = buf |
| 146 | + l.p.Put(ptr) |
| 147 | +} |
0 commit comments