1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-30 03:31:30 +00:00
Beego/logs/file_test.go

129 lines
2.7 KiB
Go
Raw Normal View History

2014-04-12 05:18:18 +00:00
// Beego (http://beego.me/)
2014-07-03 15:40:21 +00:00
//
2014-04-12 05:18:18 +00:00
// @description beego is an open-source, high-performance web framework for the Go programming language.
2014-07-03 15:40:21 +00:00
//
2014-04-12 05:18:18 +00:00
// @link http://github.com/astaxie/beego for the canonical source repository
2014-07-03 15:40:21 +00:00
//
2014-04-12 05:18:18 +00:00
// @license http://github.com/astaxie/beego/blob/master/LICENSE
2014-07-03 15:40:21 +00:00
//
2014-04-12 05:18:18 +00:00
// @authors astaxie
2013-08-27 15:48:58 +00:00
package logs
import (
"bufio"
"fmt"
"os"
2014-07-12 01:57:43 +00:00
"strconv"
2013-08-27 15:48:58 +00:00
"testing"
"time"
)
func TestFile(t *testing.T) {
log := NewLogger(10000)
log.SetLogger("file", `{"filename":"test.log"}`)
log.Debug("debug")
2014-07-12 01:57:43 +00:00
log.Informational("info")
log.Notice("notice")
log.Warning("warning")
2013-08-27 15:48:58 +00:00
log.Error("error")
2014-07-12 01:57:43 +00:00
log.Alert("alert")
2013-08-27 15:48:58 +00:00
log.Critical("critical")
2014-07-12 01:57:43 +00:00
log.Emergency("emergency")
2013-08-27 15:48:58 +00:00
time.Sleep(time.Second * 4)
f, err := os.Open("test.log")
if err != nil {
t.Fatal(err)
}
b := bufio.NewReader(f)
linenum := 0
for {
line, _, err := b.ReadLine()
if err != nil {
break
}
if len(line) > 0 {
linenum++
}
}
2014-07-12 01:57:43 +00:00
var expected = LevelDebug + 1
if linenum != expected {
t.Fatal(linenum, "not "+strconv.Itoa(expected)+" lines")
2013-08-27 15:48:58 +00:00
}
os.Remove("test.log")
}
func TestFile2(t *testing.T) {
log := NewLogger(10000)
2014-07-12 01:57:43 +00:00
log.SetLogger("file", fmt.Sprintf(`{"filename":"test2.log","level":%d}`, LevelError))
2013-08-27 15:48:58 +00:00
log.Debug("debug")
2014-07-12 01:57:43 +00:00
log.Info("info")
log.Notice("notice")
log.Warning("warning")
2013-08-27 15:48:58 +00:00
log.Error("error")
2014-07-12 01:57:43 +00:00
log.Alert("alert")
2013-08-27 15:48:58 +00:00
log.Critical("critical")
2014-07-12 01:57:43 +00:00
log.Emergency("emergency")
2013-08-27 15:48:58 +00:00
time.Sleep(time.Second * 4)
f, err := os.Open("test2.log")
if err != nil {
t.Fatal(err)
}
b := bufio.NewReader(f)
linenum := 0
for {
line, _, err := b.ReadLine()
if err != nil {
break
}
if len(line) > 0 {
linenum++
}
}
2014-07-12 01:57:43 +00:00
var expected = LevelError + 1
if linenum != expected {
t.Fatal(linenum, "not "+strconv.Itoa(expected)+" lines")
2013-08-27 15:48:58 +00:00
}
os.Remove("test2.log")
}
func TestFileRotate(t *testing.T) {
log := NewLogger(10000)
log.SetLogger("file", `{"filename":"test3.log","maxlines":4}`)
log.Debug("debug")
2014-07-12 01:57:43 +00:00
log.Info("info")
log.Notice("notice")
log.Warning("warning")
2013-08-27 15:48:58 +00:00
log.Error("error")
2014-07-12 01:57:43 +00:00
log.Alert("alert")
2013-08-27 15:48:58 +00:00
log.Critical("critical")
2014-07-12 01:57:43 +00:00
log.Emergency("emergency")
2013-08-27 15:48:58 +00:00
time.Sleep(time.Second * 4)
rotatename := "test3.log" + fmt.Sprintf(".%s.%03d", time.Now().Format("2006-01-02"), 1)
b, err := exists(rotatename)
if !b || err != nil {
2014-07-12 01:57:43 +00:00
t.Fatal("rotate not generated")
2013-08-27 15:48:58 +00:00
}
os.Remove(rotatename)
os.Remove("test3.log")
}
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func BenchmarkFile(b *testing.B) {
log := NewLogger(100000)
log.SetLogger("file", `{"filename":"test4.log"}`)
for i := 0; i < b.N; i++ {
2014-07-12 01:57:43 +00:00
log.Debug("debug")
}
os.Remove("test4.log")
}