mirror of
https://github.com/astaxie/beego.git
synced 2024-11-04 21:50:54 +00:00
134 lines
2.9 KiB
Go
134 lines
2.9 KiB
Go
// Copyright 2014 beego Author. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package logs
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestFile(t *testing.T) {
|
|
log := NewLogger(10000)
|
|
log.SetLogger("file", `{"filename":"test.log"}`)
|
|
log.Debug("debug")
|
|
log.Informational("info")
|
|
log.Notice("notice")
|
|
log.Warning("warning")
|
|
log.Error("error")
|
|
log.Alert("alert")
|
|
log.Critical("critical")
|
|
log.Emergency("emergency")
|
|
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++
|
|
}
|
|
}
|
|
var expected = LevelDebug + 1
|
|
if linenum != expected {
|
|
t.Fatal(linenum, "not "+strconv.Itoa(expected)+" lines")
|
|
}
|
|
os.Remove("test.log")
|
|
}
|
|
|
|
func TestFile2(t *testing.T) {
|
|
log := NewLogger(10000)
|
|
log.SetLogger("file", fmt.Sprintf(`{"filename":"test2.log","level":%d}`, LevelError))
|
|
log.Debug("debug")
|
|
log.Info("info")
|
|
log.Notice("notice")
|
|
log.Warning("warning")
|
|
log.Error("error")
|
|
log.Alert("alert")
|
|
log.Critical("critical")
|
|
log.Emergency("emergency")
|
|
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++
|
|
}
|
|
}
|
|
var expected = LevelError + 1
|
|
if linenum != expected {
|
|
t.Fatal(linenum, "not "+strconv.Itoa(expected)+" lines")
|
|
}
|
|
os.Remove("test2.log")
|
|
}
|
|
|
|
func TestFileRotate(t *testing.T) {
|
|
log := NewLogger(10000)
|
|
log.SetLogger("file", `{"filename":"test3.log","maxlines":4}`)
|
|
log.Debug("debug")
|
|
log.Info("info")
|
|
log.Notice("notice")
|
|
log.Warning("warning")
|
|
log.Error("error")
|
|
log.Alert("alert")
|
|
log.Critical("critical")
|
|
log.Emergency("emergency")
|
|
time.Sleep(time.Second * 4)
|
|
rotatename := "test3" + fmt.Sprintf(".%s.%03d", time.Now().Format("2006-01-02"), 1) + ".log"
|
|
b, err := exists(rotatename)
|
|
if !b || err != nil {
|
|
t.Fatal("rotate not generated")
|
|
}
|
|
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++ {
|
|
log.Debug("debug")
|
|
}
|
|
os.Remove("test4.log")
|
|
}
|