40 lines
610 B
Go
40 lines
610 B
Go
package manifest
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"sync"
|
|
)
|
|
|
|
type fileLogWriter struct {
|
|
mu sync.Mutex
|
|
f *os.File
|
|
}
|
|
|
|
func NewFileLogWriter(path string) (LogWriter, error) {
|
|
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &fileLogWriter{f: f}, nil
|
|
}
|
|
|
|
func (w *fileLogWriter) Log(e LogEntry) {
|
|
data, _ := json.Marshal(e)
|
|
w.mu.Lock()
|
|
defer w.mu.Unlock()
|
|
if w.f != nil {
|
|
w.f.Write(data)
|
|
w.f.Write([]byte("\n"))
|
|
}
|
|
}
|
|
|
|
func (w *fileLogWriter) Close() {
|
|
w.mu.Lock()
|
|
defer w.mu.Unlock()
|
|
if w.f != nil {
|
|
w.f.Close()
|
|
w.f = nil
|
|
}
|
|
}
|