diff --git a/logger.go b/logger.go index 52d72b8..de2f8a6 100644 --- a/logger.go +++ b/logger.go @@ -46,10 +46,12 @@ var ( ) type LogFile struct { - handle *os.File - logger *log.Logger - path string - fileLock sync.Mutex + handle *os.File + logger *log.Logger + path string + fileLock sync.Mutex + canCompress bool + maxSize string } type LogFileRec struct { @@ -139,9 +141,7 @@ func compressOldFile(fname string) error { return nil } -func (lf *LogFile) rotate(canCompress bool) error { - lf.fileLock.Lock() - defer lf.fileLock.Unlock() +func (lf *LogFile) rotate() error { // new file newFilePrefix := fmt.Sprintf("%s.%s", lf.handle.Name(), time.Now().Format("2006-01-02")) @@ -159,7 +159,7 @@ func (lf *LogFile) rotate(canCompress bool) error { } // compress the new log file - if canCompress { + if lf.canCompress { err = compressOldFile(newFilePrefix) if err != nil { return err @@ -181,7 +181,9 @@ func (lf *LogFile) rotate(canCompress bool) error { return nil } -func (lf *LogFile) open(maxSize string, canCompress bool) error { +func (lf *LogFile) open() error { + lf.fileLock.Lock() + defer lf.fileLock.Unlock() f, err := os.OpenFile(lf.path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) if err != nil { return err @@ -191,8 +193,8 @@ func (lf *LogFile) open(maxSize string, canCompress bool) error { return err } curSize := prettyByteSize(finfo.Size()) - if len(strings.TrimSpace(maxSize)) != 0 && curSize > maxSize { - err = lf.rotate(canCompress) + if len(strings.TrimSpace(lf.maxSize)) != 0 && curSize > lf.maxSize { + err = lf.rotate() if err != nil { return err } @@ -210,9 +212,11 @@ func newFileLogger(path string, maxSize string, canCompress bool) (*LogFile, err return nil, err } lf := &LogFile{ - path: path, + path: path, + canCompress: canCompress, + maxSize: maxSize, } - err = lf.open(maxSize, canCompress) + err = lf.open() return lf, err // f, err := os.OpenFile(requestedFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) // if err != nil { @@ -294,12 +298,13 @@ func getCurrentDate() string { return dt.Format(time.RFC3339Nano) } -func (f *LogFile) WriteLog(r *http.Request) error { - if f == nil { +func (lf *LogFile) WriteLog(r *http.Request) error { + if lf == nil { return nil } - f.fileLock.Lock() - defer f.fileLock.Unlock() + lf.fileLock.Lock() + defer lf.fileLock.Unlock() + var rec = make(map[string]string) rec["method"] = r.Method rec["requestUri"] = r.RequestURI @@ -317,6 +322,18 @@ func (f *LogFile) WriteLog(r *http.Request) error { if err != nil { return err } - f.logger.Println(string(b)) + lf.logger.Println(string(b)) + + finfo, err := lf.handle.Stat() + if err != nil { + return err + } + curSize := prettyByteSize(finfo.Size()) + if len(strings.TrimSpace(lf.maxSize)) != 0 && curSize > lf.maxSize { + err = lf.rotate() + if err != nil { + return err + } + } return nil } diff --git a/logger_test.go b/logger_test.go index 08073c4..774fe91 100644 --- a/logger_test.go +++ b/logger_test.go @@ -113,7 +113,8 @@ func TestRotate(t *testing.T) { cpFileForTest(t, rulesJsonFp, tmpLf) lf := &LogFile{ - path: tmpLf, + path: tmpLf, + canCompress: true, } fd, _ := os.Open(tmpLf) @@ -121,7 +122,7 @@ func TestRotate(t *testing.T) { isEmpty := isFileEmpty(t, tmpLf) assert.Equal(t, isEmpty, false) - err := lf.rotate(true) + err := lf.rotate() assert.Equal(t, err, nil) exists := doesFileExist(tmpLf) assert.Equal(t, exists, true) @@ -134,3 +135,40 @@ func TestRotate(t *testing.T) { assert.NotEqual(t, lf.handle, fd) } + +func TestNewLogger(t *testing.T) { + t.Run("load logging file - do not rotate", func(t *testing.T) { + + tmpDir := t.TempDir() + + mkDirForTest(t, fmt.Sprintf("%s/tmp", tmpDir)) + rulesJsonFp := "testData/app_over_size.log" + + tmpLf := fmt.Sprintf("%s/tmp/app.log", tmpDir) + }) + + + t.Run("load logging file - rotate", func(t *testing.T) { + + tmpDir := t.TempDir() + + mkDirForTest(t, fmt.Sprintf("%s/tmp", tmpDir)) + rulesJsonFp := "testData/app_over_size.log" + + tmpLf := fmt.Sprintf("%s/tmp/app.log", tmpDir) + }) + + + + t.Run("create new logging file", func(t *testing.T) { + + tmpDir := t.TempDir() + + mkDirForTest(t, fmt.Sprintf("%s/tmp", tmpDir)) + rulesJsonFp := "testData/app_over_size.log" + + tmpLf := fmt.Sprintf("%s/tmp/app.log", tmpDir) + }) +} +} +}