Skip to content

Commit

Permalink
feat: support single and rotate handler
Browse files Browse the repository at this point in the history
  • Loading branch information
leeqvip committed Feb 7, 2023
1 parent 9acfd03 commit 05a2cbf
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
15 changes: 13 additions & 2 deletions log/handler/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type FileHandler struct {
filenameFormat string
dateFormat string
timedFilename string

rotate bool
}

func NewFileHandler(filename string, level record.Level) *FileHandler {
Expand Down Expand Up @@ -60,12 +62,21 @@ func (h *FileHandler) SetLevel(level record.Level) {
func (h *FileHandler) write(r record.Record) {
h.Lock()
defer h.Unlock()
file, _ := os.OpenFile(h.GetTimedFilename(), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
file, _ := os.OpenFile(h.GetFilename(), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
defer file.Close()
file.Write([]byte(r.Formatted))
}

// GetTimedFilename Gets the Timed filename.
// GetFilename Gets the filename.
func (h *FileHandler) GetFilename() string {
if !h.rotate {
return h.filename
}

return h.GetTimedFilename()
}

// GetTimedFilename Gets the timed filename.
func (h *FileHandler) GetTimedFilename() string {
dirname := path.Dir(h.filename)
filename := path.Base(h.filename)
Expand Down
4 changes: 2 additions & 2 deletions log/handler/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestNewFileHandler(t *testing.T) {
filename := path.Join(os.TempDir(), "thinkgo.log")

h := NewFileHandler(filename, record.DEBUG)
filename = h.GetTimedFilename()
filename = h.GetFilename()

os.Remove(filename)

Expand All @@ -32,7 +32,7 @@ func TestNewFileHandler(t *testing.T) {
if !strings.Contains(content, message) {
t.Error("test FileHandler error")
}

t.Log(filename, content)
}

func getRecord(message string) record.Record {
Expand Down
13 changes: 13 additions & 0 deletions log/handler/rotate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package handler

import (
"github.com/forgoer/thinkgo/log/record"
)

func NewRotateHandler(filename string, level record.Level) *FileHandler {
// h.timedFilename = h.GetTimedFilename()
fileHandler := NewFileHandler(filename, level)
fileHandler.rotate = true

return fileHandler
}
34 changes: 34 additions & 0 deletions log/handler/rotate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package handler

import (
"github.com/forgoer/thinkgo/log/record"
"io/ioutil"
"os"
"path"
"strings"
"testing"
)

func TestRotateHandler(t *testing.T) {
filename := path.Join(os.TempDir(), "thinkgo.log")

h := NewRotateHandler(filename, record.DEBUG)
filename = h.GetFilename()

os.Remove(filename)

message := "Log write to file"
r := getRecord(message)
h.Handle(r)

b, err := ioutil.ReadFile(filename)
if err != nil {
t.Error(err)
}
content := string(b)

if !strings.Contains(content, message) {
t.Error("test FileHandler error")
}
t.Log(filename, content)
}

0 comments on commit 05a2cbf

Please sign in to comment.