Signed-off-by: Shijiang Wei <mountkin@gmail.com>
| ... | ... |
@@ -94,7 +94,6 @@ func (l *JSONFileLogger) Log(msg *logger.Message) error {
|
| 94 | 94 |
return err |
| 95 | 95 |
} |
| 96 | 96 |
l.mu.Lock() |
| 97 |
- defer l.mu.Unlock() |
|
| 98 | 97 |
err = (&jsonlog.JSONLogs{
|
| 99 | 98 |
Log: append(msg.Line, '\n'), |
| 100 | 99 |
Stream: msg.Source, |
| ... | ... |
@@ -102,6 +101,7 @@ func (l *JSONFileLogger) Log(msg *logger.Message) error {
|
| 102 | 102 |
RawAttrs: l.extra, |
| 103 | 103 |
}).MarshalJSONBuf(l.buf) |
| 104 | 104 |
if err != nil {
|
| 105 |
+ l.mu.Unlock() |
|
| 105 | 106 |
return err |
| 106 | 107 |
} |
| 107 | 108 |
|
| ... | ... |
@@ -109,6 +109,7 @@ func (l *JSONFileLogger) Log(msg *logger.Message) error {
|
| 109 | 109 |
_, err = l.writer.Write(l.buf.Bytes()) |
| 110 | 110 |
l.writeNotifier.Publish(struct{}{})
|
| 111 | 111 |
l.buf.Reset() |
| 112 |
+ l.mu.Unlock() |
|
| 112 | 113 |
|
| 113 | 114 |
return err |
| 114 | 115 |
} |
| ... | ... |
@@ -13,6 +13,7 @@ type RotateFileWriter struct {
|
| 13 | 13 |
f *os.File // store for closing |
| 14 | 14 |
mu sync.Mutex |
| 15 | 15 |
capacity int64 //maximum size of each file |
| 16 |
+ currentSize int64 // current size of the latest file |
|
| 16 | 17 |
maxFiles int //maximum number of files |
| 17 | 18 |
notifyRotate *pubsub.Publisher |
| 18 | 19 |
} |
| ... | ... |
@@ -21,12 +22,18 @@ type RotateFileWriter struct {
|
| 21 | 21 |
func NewRotateFileWriter(logPath string, capacity int64, maxFiles int) (*RotateFileWriter, error) {
|
| 22 | 22 |
log, err := os.OpenFile(logPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0640) |
| 23 | 23 |
if err != nil {
|
| 24 |
- return &RotateFileWriter{}, err
|
|
| 24 |
+ return nil, err |
|
| 25 |
+ } |
|
| 26 |
+ |
|
| 27 |
+ size, err := log.Seek(0, os.SEEK_END) |
|
| 28 |
+ if err != nil {
|
|
| 29 |
+ return nil, err |
|
| 25 | 30 |
} |
| 26 | 31 |
|
| 27 | 32 |
return &RotateFileWriter{
|
| 28 | 33 |
f: log, |
| 29 | 34 |
capacity: capacity, |
| 35 |
+ currentSize: size, |
|
| 30 | 36 |
maxFiles: maxFiles, |
| 31 | 37 |
notifyRotate: pubsub.NewPublisher(0, 1), |
| 32 | 38 |
}, nil |
| ... | ... |
@@ -35,12 +42,17 @@ func NewRotateFileWriter(logPath string, capacity int64, maxFiles int) (*RotateF |
| 35 | 35 |
//WriteLog write log message to File |
| 36 | 36 |
func (w *RotateFileWriter) Write(message []byte) (int, error) {
|
| 37 | 37 |
w.mu.Lock() |
| 38 |
- defer w.mu.Unlock() |
|
| 39 | 38 |
if err := w.checkCapacityAndRotate(); err != nil {
|
| 39 |
+ w.mu.Unlock() |
|
| 40 | 40 |
return -1, err |
| 41 | 41 |
} |
| 42 | 42 |
|
| 43 |
- return w.f.Write(message) |
|
| 43 |
+ n, err := w.f.Write(message) |
|
| 44 |
+ if err == nil {
|
|
| 45 |
+ w.currentSize += int64(n) |
|
| 46 |
+ } |
|
| 47 |
+ w.mu.Unlock() |
|
| 48 |
+ return n, err |
|
| 44 | 49 |
} |
| 45 | 50 |
|
| 46 | 51 |
func (w *RotateFileWriter) checkCapacityAndRotate() error {
|
| ... | ... |
@@ -48,12 +60,7 @@ func (w *RotateFileWriter) checkCapacityAndRotate() error {
|
| 48 | 48 |
return nil |
| 49 | 49 |
} |
| 50 | 50 |
|
| 51 |
- meta, err := w.f.Stat() |
|
| 52 |
- if err != nil {
|
|
| 53 |
- return err |
|
| 54 |
- } |
|
| 55 |
- |
|
| 56 |
- if meta.Size() >= w.capacity {
|
|
| 51 |
+ if w.currentSize >= w.capacity {
|
|
| 57 | 52 |
name := w.f.Name() |
| 58 | 53 |
if err := w.f.Close(); err != nil {
|
| 59 | 54 |
return err |
| ... | ... |
@@ -66,6 +73,7 @@ func (w *RotateFileWriter) checkCapacityAndRotate() error {
|
| 66 | 66 |
return err |
| 67 | 67 |
} |
| 68 | 68 |
w.f = file |
| 69 |
+ w.currentSize = 0 |
|
| 69 | 70 |
w.notifyRotate.Publish(struct{}{})
|
| 70 | 71 |
} |
| 71 | 72 |
|