mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 18:40:55 +00:00
add file test
This commit is contained in:
parent
e81cca304b
commit
1a42154c64
14
logs/file.go
14
logs/file.go
@ -281,7 +281,6 @@ func (w *fileLogWriter) doRotate(logTime time.Time) error {
|
|||||||
// file exists
|
// file exists
|
||||||
// Find the next available number
|
// Find the next available number
|
||||||
num := 1
|
num := 1
|
||||||
fmt.Println("num :", num)
|
|
||||||
fName := ""
|
fName := ""
|
||||||
format := ""
|
format := ""
|
||||||
var openTime time.Time
|
var openTime time.Time
|
||||||
@ -312,13 +311,11 @@ func (w *fileLogWriter) doRotate(logTime time.Time) error {
|
|||||||
} else {
|
} else {
|
||||||
fName = fmt.Sprintf("%s.%s%s", w.fileNameOnly, openTime.Format(format), w.suffix)
|
fName = fmt.Sprintf("%s.%s%s", w.fileNameOnly, openTime.Format(format), w.suffix)
|
||||||
_, err = os.Lstat(fName)
|
_, err = os.Lstat(fName)
|
||||||
fmt.Println("befor fName:", fName)
|
|
||||||
for ; err == nil && num <= 999; num++ {
|
for ; err == nil && num <= 999; num++ {
|
||||||
fName = w.fileNameOnly + fmt.Sprintf(".%s.%03d%s", openTime.Format(format), num, w.suffix)
|
fName = w.fileNameOnly + fmt.Sprintf(".%s.%03d%s", openTime.Format(format), num, w.suffix)
|
||||||
_, err = os.Lstat(fName)
|
_, err = os.Lstat(fName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Println("after fName:", fName)
|
|
||||||
// return error if the last file checked still existed
|
// return error if the last file checked still existed
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return fmt.Errorf("Rotate: Cannot find free log number to rename %s", w.Filename)
|
return fmt.Errorf("Rotate: Cannot find free log number to rename %s", w.Filename)
|
||||||
@ -362,14 +359,21 @@ func (w *fileLogWriter) deleteOldLog() {
|
|||||||
if info == nil {
|
if info == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if w.Hourly {
|
||||||
|
if !info.IsDir() && info.ModTime().Add(1*time.Hour*time.Duration(w.MaxHours)).Before(time.Now()) {
|
||||||
|
if strings.HasPrefix(filepath.Base(path), filepath.Base(w.fileNameOnly)) &&
|
||||||
|
strings.HasSuffix(filepath.Base(path), w.suffix) {
|
||||||
|
os.Remove(path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if w.Daily {
|
||||||
if !info.IsDir() && info.ModTime().Add(24*time.Hour*time.Duration(w.MaxDays)).Before(time.Now()) {
|
if !info.IsDir() && info.ModTime().Add(24*time.Hour*time.Duration(w.MaxDays)).Before(time.Now()) {
|
||||||
if strings.HasPrefix(filepath.Base(path), filepath.Base(w.fileNameOnly)) &&
|
if strings.HasPrefix(filepath.Base(path), filepath.Base(w.fileNameOnly)) &&
|
||||||
strings.HasSuffix(filepath.Base(path), w.suffix) {
|
strings.HasSuffix(filepath.Base(path), w.suffix) {
|
||||||
os.Remove(path)
|
os.Remove(path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
os.Exit(0)
|
}
|
||||||
return
|
return
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -186,7 +186,7 @@ func TestFileDailyRotate_06(t *testing.T) { //test file mode
|
|||||||
|
|
||||||
func TestFileHourlyRotate_01(t *testing.T) {
|
func TestFileHourlyRotate_01(t *testing.T) {
|
||||||
log := NewLogger(10000)
|
log := NewLogger(10000)
|
||||||
log.SetLogger("file", `{"filename":"test3.log","maxlines":4}`)
|
log.SetLogger("file", `{"filename":"test3.log","hourly":true,"maxlines":4}`)
|
||||||
log.Debug("debug")
|
log.Debug("debug")
|
||||||
log.Info("info")
|
log.Info("info")
|
||||||
log.Notice("notice")
|
log.Notice("notice")
|
||||||
@ -220,12 +220,12 @@ func TestFileHourlyRotate_03(t *testing.T) {
|
|||||||
os.Remove(fn)
|
os.Remove(fn)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
func TestFileHourlyRotate_04(t *testing.T) {
|
func TestFileHourlyRotate_04(t *testing.T) {
|
||||||
fn1 := "rotate_hour.log"
|
fn1 := "rotate_hour.log"
|
||||||
fn2 := "rotate_hour." + time.Now().Add(-1*time.Hour).Format("2006010215") + ".log"
|
fn2 := "rotate_hour." + time.Now().Add(-1*time.Hour).Format("2006010215") + ".log"
|
||||||
testFileHourlyRotate(t, fn1, fn2)
|
testFileHourlyRotate(t, fn1, fn2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFileHourlyRotate_05(t *testing.T) {
|
func TestFileHourlyRotate_05(t *testing.T) {
|
||||||
fn1 := "rotate_hour.log"
|
fn1 := "rotate_hour.log"
|
||||||
fn := "rotate_hour." + time.Now().Add(-1*time.Hour).Format("2006010215") + ".log"
|
fn := "rotate_hour." + time.Now().Add(-1*time.Hour).Format("2006010215") + ".log"
|
||||||
@ -234,9 +234,10 @@ func TestFileHourlyRotate_05(t *testing.T) {
|
|||||||
testFileHourlyRotate(t, fn1, fn2)
|
testFileHourlyRotate(t, fn1, fn2)
|
||||||
os.Remove(fn)
|
os.Remove(fn)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFileHourlyRotate_06(t *testing.T) { //test file mode
|
func TestFileHourlyRotate_06(t *testing.T) { //test file mode
|
||||||
log := NewLogger(10000)
|
log := NewLogger(10000)
|
||||||
log.SetLogger("file", `{"filename":"test3.log","maxlines":4}`)
|
log.SetLogger("file", `{"filename":"test3.log", "hourly":true, "maxlines":4}`)
|
||||||
log.Debug("debug")
|
log.Debug("debug")
|
||||||
log.Info("info")
|
log.Info("info")
|
||||||
log.Notice("notice")
|
log.Notice("notice")
|
||||||
@ -255,7 +256,7 @@ func TestFileHourlyRotate_06(t *testing.T) { //test file mode
|
|||||||
os.Remove(rotateName)
|
os.Remove(rotateName)
|
||||||
os.Remove("test3.log")
|
os.Remove("test3.log")
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
func testFileRotate(t *testing.T, fn1, fn2 string, daily, hourly bool) {
|
func testFileRotate(t *testing.T, fn1, fn2 string, daily, hourly bool) {
|
||||||
fw := &fileLogWriter{
|
fw := &fileLogWriter{
|
||||||
Daily: daily,
|
Daily: daily,
|
||||||
@ -324,6 +325,37 @@ func testFileDailyRotate(t *testing.T, fn1, fn2 string) {
|
|||||||
fw.Destroy()
|
fw.Destroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testFileHourlyRotate(t *testing.T, fn1, fn2 string) {
|
||||||
|
fw := &fileLogWriter{
|
||||||
|
Hourly: true,
|
||||||
|
MaxHours: 168,
|
||||||
|
Rotate: true,
|
||||||
|
Level: LevelTrace,
|
||||||
|
Perm: "0660",
|
||||||
|
RotatePerm: "0440",
|
||||||
|
}
|
||||||
|
fw.Init(fmt.Sprintf(`{"filename":"%v","maxhours":1}`, fn1))
|
||||||
|
fw.hourlyOpenTime = time.Now().Add(-1 * time.Hour)
|
||||||
|
fw.hourlyOpenDate = fw.hourlyOpenTime.Hour()
|
||||||
|
hour, _ := time.ParseInLocation("2006010215", time.Now().Format("2006010215"), fw.hourlyOpenTime.Location())
|
||||||
|
hour = hour.Add(-1 * time.Second)
|
||||||
|
fw.hourlyRotate(hour)
|
||||||
|
for _, file := range []string{fn1, fn2} {
|
||||||
|
_, err := os.Stat(file)
|
||||||
|
if err != nil {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
content, err := ioutil.ReadFile(file)
|
||||||
|
if err != nil {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
if len(content) > 0 {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
os.Remove(file)
|
||||||
|
}
|
||||||
|
fw.Destroy()
|
||||||
|
}
|
||||||
func exists(path string) (bool, error) {
|
func exists(path string) (bool, error) {
|
||||||
_, err := os.Stat(path)
|
_, err := os.Stat(path)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user