2015-11-14 18:02:06 -05:00
|
|
|
package loggerutils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/pubsub"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RotateFileWriter is Logger implementation for default Docker logging.
|
|
|
|
type RotateFileWriter struct {
|
|
|
|
f *os.File // store for closing
|
|
|
|
mu sync.Mutex
|
|
|
|
capacity int64 //maximum size of each file
|
2016-02-23 06:28:04 -05:00
|
|
|
currentSize int64 // current size of the latest file
|
2015-11-14 18:02:06 -05:00
|
|
|
maxFiles int //maximum number of files
|
|
|
|
notifyRotate *pubsub.Publisher
|
|
|
|
}
|
|
|
|
|
|
|
|
//NewRotateFileWriter creates new RotateFileWriter
|
|
|
|
func NewRotateFileWriter(logPath string, capacity int64, maxFiles int) (*RotateFileWriter, error) {
|
|
|
|
log, err := os.OpenFile(logPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0640)
|
|
|
|
if err != nil {
|
2016-02-23 06:28:04 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
size, err := log.Seek(0, os.SEEK_END)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-11-14 18:02:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return &RotateFileWriter{
|
|
|
|
f: log,
|
|
|
|
capacity: capacity,
|
2016-02-23 06:28:04 -05:00
|
|
|
currentSize: size,
|
2015-11-14 18:02:06 -05:00
|
|
|
maxFiles: maxFiles,
|
|
|
|
notifyRotate: pubsub.NewPublisher(0, 1),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-12-13 11:00:39 -05:00
|
|
|
//WriteLog write log message to File
|
2015-11-14 18:02:06 -05:00
|
|
|
func (w *RotateFileWriter) Write(message []byte) (int, error) {
|
|
|
|
w.mu.Lock()
|
|
|
|
if err := w.checkCapacityAndRotate(); err != nil {
|
2016-02-23 06:28:04 -05:00
|
|
|
w.mu.Unlock()
|
2015-11-14 18:02:06 -05:00
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
2016-02-23 06:28:04 -05:00
|
|
|
n, err := w.f.Write(message)
|
|
|
|
if err == nil {
|
|
|
|
w.currentSize += int64(n)
|
|
|
|
}
|
|
|
|
w.mu.Unlock()
|
|
|
|
return n, err
|
2015-11-14 18:02:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *RotateFileWriter) checkCapacityAndRotate() error {
|
|
|
|
if w.capacity == -1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-23 06:28:04 -05:00
|
|
|
if w.currentSize >= w.capacity {
|
2015-11-14 18:02:06 -05:00
|
|
|
name := w.f.Name()
|
|
|
|
if err := w.f.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := rotate(name, w.maxFiles); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
file, err := os.OpenFile(name, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 06400)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.f = file
|
2016-02-23 06:28:04 -05:00
|
|
|
w.currentSize = 0
|
2015-11-14 18:02:06 -05:00
|
|
|
w.notifyRotate.Publish(struct{}{})
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func rotate(name string, maxFiles int) error {
|
|
|
|
if maxFiles < 2 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for i := maxFiles - 1; i > 1; i-- {
|
|
|
|
toPath := name + "." + strconv.Itoa(i)
|
|
|
|
fromPath := name + "." + strconv.Itoa(i-1)
|
2016-03-17 12:13:23 -04:00
|
|
|
if err := os.Rename(fromPath, toPath); err != nil && !os.IsNotExist(err) {
|
2015-11-14 18:02:06 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-17 12:13:23 -04:00
|
|
|
if err := os.Rename(name, name+".1"); err != nil && !os.IsNotExist(err) {
|
2015-11-14 18:02:06 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-13 11:00:39 -05:00
|
|
|
// LogPath returns the location the given writer logs to.
|
2015-11-14 18:02:06 -05:00
|
|
|
func (w *RotateFileWriter) LogPath() string {
|
|
|
|
return w.f.Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MaxFiles return maximum number of files
|
|
|
|
func (w *RotateFileWriter) MaxFiles() int {
|
|
|
|
return w.maxFiles
|
|
|
|
}
|
|
|
|
|
|
|
|
//NotifyRotate returns the new subscriber
|
|
|
|
func (w *RotateFileWriter) NotifyRotate() chan interface{} {
|
|
|
|
return w.notifyRotate.Subscribe()
|
|
|
|
}
|
|
|
|
|
|
|
|
//NotifyRotateEvict removes the specified subscriber from receiving any more messages.
|
|
|
|
func (w *RotateFileWriter) NotifyRotateEvict(sub chan interface{}) {
|
|
|
|
w.notifyRotate.Evict(sub)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes underlying file and signals all readers to stop.
|
|
|
|
func (w *RotateFileWriter) Close() error {
|
|
|
|
return w.f.Close()
|
|
|
|
}
|