Flush forces the buffer to be written to the underlying writer.
()
| 47 | |
| 48 | // Flush forces the buffer to be written to the underlying writer. |
| 49 | func (l *BufferedLogger) Flush() error { |
| 50 | // reset the counter |
| 51 | sz := l.entries.Swap(0) |
| 52 | if sz <= 0 { |
| 53 | return nil |
| 54 | } |
| 55 | |
| 56 | // WriteTo() calls Reset() on the underlying buffer, so it's not needed here |
| 57 | _, err := l.buf.WriteTo(l.w) |
| 58 | |
| 59 | // only call OnFlush callback if write was successful |
| 60 | if err == nil && l.onFlush != nil { |
| 61 | l.onFlush(sz) |
| 62 | } |
| 63 | |
| 64 | return err |
| 65 | } |
| 66 | |
| 67 | type BufferedLoggerOption func(*BufferedLogger) |
| 68 |