|
| 1 | +package log |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + stdlog "log" |
| 8 | + "os" |
| 9 | + "strconv" |
| 10 | + |
| 11 | + "github.com/go-playground/ansi" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + space = byte(' ') |
| 16 | + equals = byte('=') |
| 17 | + newLine = byte('\n') |
| 18 | + base10 = 10 |
| 19 | + v = "%v" |
| 20 | +) |
| 21 | + |
| 22 | +var ( |
| 23 | + defaultLoggerWriter io.Writer = os.Stderr // here for tests only |
| 24 | + defaultLoggerTimeFormat = "2006-01-02 15:04:05.000000000Z07:00" // here for tests only |
| 25 | +) |
| 26 | + |
| 27 | +// console is an instance of the console logger |
| 28 | +type console struct { |
| 29 | + colors [8]ansi.EscSeq |
| 30 | + writer io.Writer |
| 31 | + timestampFormat string |
| 32 | + r *io.PipeReader |
| 33 | +} |
| 34 | + |
| 35 | +func newDefaultLogger() *console { |
| 36 | + c := &console{ |
| 37 | + colors: [...]ansi.EscSeq{ |
| 38 | + DebugLevel: ansi.Green, |
| 39 | + InfoLevel: ansi.Blue, |
| 40 | + NoticeLevel: ansi.LightCyan, |
| 41 | + WarnLevel: ansi.LightYellow, |
| 42 | + ErrorLevel: ansi.LightRed, |
| 43 | + PanicLevel: ansi.Red, |
| 44 | + AlertLevel: ansi.Red + ansi.Underline, |
| 45 | + FatalLevel: ansi.Red + ansi.Underline + ansi.Blink, |
| 46 | + }, |
| 47 | + writer: defaultLoggerWriter, |
| 48 | + timestampFormat: defaultLoggerTimeFormat, |
| 49 | + } |
| 50 | + done := make(chan struct{}) |
| 51 | + go c.handleStdLogger(done) |
| 52 | + <-done // have to wait, it was running too quickly and some messages can be lost |
| 53 | + return c |
| 54 | +} |
| 55 | + |
| 56 | +// this will redirect the output of |
| 57 | +func (c *console) handleStdLogger(done chan<- struct{}) { |
| 58 | + var w *io.PipeWriter |
| 59 | + c.r, w = io.Pipe() |
| 60 | + stdlog.SetOutput(w) |
| 61 | + |
| 62 | + scanner := bufio.NewScanner(c.r) |
| 63 | + go func() { |
| 64 | + done <- struct{}{} |
| 65 | + }() |
| 66 | + |
| 67 | + for scanner.Scan() { |
| 68 | + WithField("stdlog", true).Info(scanner.Text()) |
| 69 | + } |
| 70 | + _ = c.r.Close() |
| 71 | + _ = w.Close() |
| 72 | +} |
| 73 | + |
| 74 | +// Log handles the log entry |
| 75 | +func (c *console) Log(e Entry) { |
| 76 | + var i int |
| 77 | + |
| 78 | + b := BytePool().Get() |
| 79 | + lvl := e.Level.String() |
| 80 | + color := c.colors[e.Level] |
| 81 | + b = append(b, e.Timestamp.Format(c.timestampFormat)...) |
| 82 | + b = append(b, space) |
| 83 | + b = append(b, color...) |
| 84 | + |
| 85 | + for i = 0; i < 6-len(lvl); i++ { |
| 86 | + b = append(b, space) |
| 87 | + } |
| 88 | + b = append(b, lvl...) |
| 89 | + b = append(b, ansi.Reset...) |
| 90 | + b = append(b, space) |
| 91 | + b = append(b, e.Message...) |
| 92 | + |
| 93 | + for _, f := range e.Fields { |
| 94 | + b = append(b, space) |
| 95 | + b = append(b, color...) |
| 96 | + b = append(b, f.Key...) |
| 97 | + b = append(b, ansi.Reset...) |
| 98 | + b = append(b, equals) |
| 99 | + |
| 100 | + switch t := f.Value.(type) { |
| 101 | + case string: |
| 102 | + b = append(b, t...) |
| 103 | + case int: |
| 104 | + b = strconv.AppendInt(b, int64(t), base10) |
| 105 | + case int8: |
| 106 | + b = strconv.AppendInt(b, int64(t), base10) |
| 107 | + case int16: |
| 108 | + b = strconv.AppendInt(b, int64(t), base10) |
| 109 | + case int32: |
| 110 | + b = strconv.AppendInt(b, int64(t), base10) |
| 111 | + case int64: |
| 112 | + b = strconv.AppendInt(b, t, base10) |
| 113 | + case uint: |
| 114 | + b = strconv.AppendUint(b, uint64(t), base10) |
| 115 | + case uint8: |
| 116 | + b = strconv.AppendUint(b, uint64(t), base10) |
| 117 | + case uint16: |
| 118 | + b = strconv.AppendUint(b, uint64(t), base10) |
| 119 | + case uint32: |
| 120 | + b = strconv.AppendUint(b, uint64(t), base10) |
| 121 | + case uint64: |
| 122 | + b = strconv.AppendUint(b, t, base10) |
| 123 | + case float32: |
| 124 | + b = strconv.AppendFloat(b, float64(t), 'f', -1, 32) |
| 125 | + case float64: |
| 126 | + b = strconv.AppendFloat(b, t, 'f', -1, 64) |
| 127 | + case bool: |
| 128 | + b = strconv.AppendBool(b, t) |
| 129 | + default: |
| 130 | + b = append(b, fmt.Sprintf(v, f.Value)...) |
| 131 | + } |
| 132 | + } |
| 133 | + b = append(b, newLine) |
| 134 | + _, _ = c.writer.Write(b) |
| 135 | + BytePool().Put(b) |
| 136 | +} |
| 137 | + |
| 138 | +// Close cleans up any resources |
| 139 | +func (c *console) Close() error { |
| 140 | + // reset the output back to original |
| 141 | + stdlog.SetOutput(os.Stderr) |
| 142 | + // since we reset the output piror to closing we don't have to wait |
| 143 | + if c.r != nil { |
| 144 | + _ = c.r.Close() |
| 145 | + } |
| 146 | + return nil |
| 147 | +} |
0 commit comments