This commit is contained in:
iratusmachina 2024-06-28 00:48:27 -04:00
parent 6806a81588
commit 53b77b7f8a
2 changed files with 75 additions and 20 deletions

View File

@ -46,10 +46,12 @@ var (
) )
type LogFile struct { type LogFile struct {
handle *os.File handle *os.File
logger *log.Logger logger *log.Logger
path string path string
fileLock sync.Mutex fileLock sync.Mutex
canCompress bool
maxSize string
} }
type LogFileRec struct { type LogFileRec struct {
@ -139,9 +141,7 @@ func compressOldFile(fname string) error {
return nil return nil
} }
func (lf *LogFile) rotate(canCompress bool) error { func (lf *LogFile) rotate() error {
lf.fileLock.Lock()
defer lf.fileLock.Unlock()
// new file // new file
newFilePrefix := fmt.Sprintf("%s.%s", lf.handle.Name(), time.Now().Format("2006-01-02")) 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 // compress the new log file
if canCompress { if lf.canCompress {
err = compressOldFile(newFilePrefix) err = compressOldFile(newFilePrefix)
if err != nil { if err != nil {
return err return err
@ -181,7 +181,9 @@ func (lf *LogFile) rotate(canCompress bool) error {
return nil 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) f, err := os.OpenFile(lf.path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil { if err != nil {
return err return err
@ -191,8 +193,8 @@ func (lf *LogFile) open(maxSize string, canCompress bool) error {
return err return err
} }
curSize := prettyByteSize(finfo.Size()) curSize := prettyByteSize(finfo.Size())
if len(strings.TrimSpace(maxSize)) != 0 && curSize > maxSize { if len(strings.TrimSpace(lf.maxSize)) != 0 && curSize > lf.maxSize {
err = lf.rotate(canCompress) err = lf.rotate()
if err != nil { if err != nil {
return err return err
} }
@ -210,9 +212,11 @@ func newFileLogger(path string, maxSize string, canCompress bool) (*LogFile, err
return nil, err return nil, err
} }
lf := &LogFile{ lf := &LogFile{
path: path, path: path,
canCompress: canCompress,
maxSize: maxSize,
} }
err = lf.open(maxSize, canCompress) err = lf.open()
return lf, err return lf, err
// f, err := os.OpenFile(requestedFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) // f, err := os.OpenFile(requestedFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
// if err != nil { // if err != nil {
@ -294,12 +298,13 @@ func getCurrentDate() string {
return dt.Format(time.RFC3339Nano) return dt.Format(time.RFC3339Nano)
} }
func (f *LogFile) WriteLog(r *http.Request) error { func (lf *LogFile) WriteLog(r *http.Request) error {
if f == nil { if lf == nil {
return nil return nil
} }
f.fileLock.Lock() lf.fileLock.Lock()
defer f.fileLock.Unlock() defer lf.fileLock.Unlock()
var rec = make(map[string]string) var rec = make(map[string]string)
rec["method"] = r.Method rec["method"] = r.Method
rec["requestUri"] = r.RequestURI rec["requestUri"] = r.RequestURI
@ -317,6 +322,18 @@ func (f *LogFile) WriteLog(r *http.Request) error {
if err != nil { if err != nil {
return err 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 return nil
} }

View File

@ -113,7 +113,8 @@ func TestRotate(t *testing.T) {
cpFileForTest(t, rulesJsonFp, tmpLf) cpFileForTest(t, rulesJsonFp, tmpLf)
lf := &LogFile{ lf := &LogFile{
path: tmpLf, path: tmpLf,
canCompress: true,
} }
fd, _ := os.Open(tmpLf) fd, _ := os.Open(tmpLf)
@ -121,7 +122,7 @@ func TestRotate(t *testing.T) {
isEmpty := isFileEmpty(t, tmpLf) isEmpty := isFileEmpty(t, tmpLf)
assert.Equal(t, isEmpty, false) assert.Equal(t, isEmpty, false)
err := lf.rotate(true) err := lf.rotate()
assert.Equal(t, err, nil) assert.Equal(t, err, nil)
exists := doesFileExist(tmpLf) exists := doesFileExist(tmpLf)
assert.Equal(t, exists, true) assert.Equal(t, exists, true)
@ -134,3 +135,40 @@ func TestRotate(t *testing.T) {
assert.NotEqual(t, lf.handle, fd) 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)
})
}
}
}