package ripsawlogger import ( "bufio" "encoding/json" "fmt" "io" "sync" "time" ) type writerLogger struct { w io.Writer mu sync.Mutex } // Creates a generic logger that writes log entries to any io.Writer as JSON. func NewWriterLogger(writer io.Writer) Logger { return writerLogger{w: writer} } func (rl writerLogger) Flush() { // if the underlying writer is buffered writer, flush it if w, ok := rl.w.(*bufio.Writer); ok { w.Flush() } } func (rl writerLogger) Log(data LogEntry) { timestamp := data.Time if timestamp.IsZero() { timestamp = time.Now() } rl.mu.Lock() defer rl.mu.Unlock() fmt.Fprintf(rl.w, "%s [%s] ", timestamp.UTC().Format(time.RFC3339Nano), data.Level) json.NewEncoder(rl.w).Encode(data.Contents) }