| package golog |
| |
| import ( |
| "io/ioutil" |
| "os" |
| "path/filepath" |
| "strings" |
| "testing" |
| |
| "github.com/go-playground/log" |
| ) |
| |
| const logFilePath = "logs/golog.txt" |
| |
| func init() { |
| dir := filepath.Dir(logFilePath) |
| n := filepath.Base(logFilePath) |
| files, err := ioutil.ReadDir(dir) |
| if err != nil { |
| return |
| } |
| |
| for _, f := range files { |
| if f.IsDir() { |
| continue |
| } |
| if strings.HasPrefix(f.Name(), n) { |
| os.Remove(filepath.Join(dir, f.Name())) |
| } |
| } |
| } |
| |
| func TestFileLog(t *testing.T) { |
| RegisterHandler(NewFileLogHandler(logFilePath), log.DebugLevel) |
| |
| log.Debugf("Debug Message") |
| log.Infof("Info Message") |
| log.Errorf("Error message") |
| } |
| |
| func TestRotateFileLog(t *testing.T) { |
| h := NewRotateFileLogHandler(logFilePath, 1, 3) |
| RegisterHandler(h, log.DebugLevel) |
| for i := 0; i < 100000; i++ { |
| log.Infof("Info Message") |
| } |
| } |