2014-08-18 08:41:43 +00:00
|
|
|
// Copyright 2014 beego Author. All Rights Reserved.
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// 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
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// 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.
|
|
|
|
|
2013-08-27 15:48:58 +00:00
|
|
|
package logs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2014-07-11 09:15:34 +00:00
|
|
|
"strconv"
|
2013-08-27 15:48:58 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2016-01-13 01:21:32 +00:00
|
|
|
func TestFile1(t *testing.T) {
|
2013-08-27 15:48:58 +00:00
|
|
|
log := NewLogger(10000)
|
|
|
|
log.SetLogger("file", `{"filename":"test.log"}`)
|
|
|
|
log.Debug("debug")
|
2014-07-11 09:15:34 +00:00
|
|
|
log.Informational("info")
|
|
|
|
log.Notice("notice")
|
|
|
|
log.Warning("warning")
|
2013-08-27 15:48:58 +00:00
|
|
|
log.Error("error")
|
2014-07-11 09:15:34 +00:00
|
|
|
log.Alert("alert")
|
2013-08-27 15:48:58 +00:00
|
|
|
log.Critical("critical")
|
2014-07-11 09:15:34 +00:00
|
|
|
log.Emergency("emergency")
|
2013-08-27 15:48:58 +00:00
|
|
|
f, err := os.Open("test.log")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
b := bufio.NewReader(f)
|
2016-01-13 01:21:32 +00:00
|
|
|
lineNum := 0
|
2013-08-27 15:48:58 +00:00
|
|
|
for {
|
|
|
|
line, _, err := b.ReadLine()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if len(line) > 0 {
|
2016-01-13 01:21:32 +00:00
|
|
|
lineNum++
|
2013-08-27 15:48:58 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-11 09:15:34 +00:00
|
|
|
var expected = LevelDebug + 1
|
2016-01-13 01:21:32 +00:00
|
|
|
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-11 09:15:34 +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-11 09:15:34 +00:00
|
|
|
log.Info("info")
|
|
|
|
log.Notice("notice")
|
|
|
|
log.Warning("warning")
|
2013-08-27 15:48:58 +00:00
|
|
|
log.Error("error")
|
2014-07-11 09:15:34 +00:00
|
|
|
log.Alert("alert")
|
2013-08-27 15:48:58 +00:00
|
|
|
log.Critical("critical")
|
2014-07-11 09:15:34 +00:00
|
|
|
log.Emergency("emergency")
|
2013-08-27 15:48:58 +00:00
|
|
|
f, err := os.Open("test2.log")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
b := bufio.NewReader(f)
|
2016-01-13 01:21:32 +00:00
|
|
|
lineNum := 0
|
2013-08-27 15:48:58 +00:00
|
|
|
for {
|
|
|
|
line, _, err := b.ReadLine()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if len(line) > 0 {
|
2016-01-13 01:21:32 +00:00
|
|
|
lineNum++
|
2013-08-27 15:48:58 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-11 09:15:34 +00:00
|
|
|
var expected = LevelError + 1
|
2016-01-13 01:21:32 +00:00
|
|
|
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-11 09:15:34 +00:00
|
|
|
log.Info("info")
|
|
|
|
log.Notice("notice")
|
|
|
|
log.Warning("warning")
|
2013-08-27 15:48:58 +00:00
|
|
|
log.Error("error")
|
2014-07-11 09:15:34 +00:00
|
|
|
log.Alert("alert")
|
2013-08-27 15:48:58 +00:00
|
|
|
log.Critical("critical")
|
2014-07-11 09:15:34 +00:00
|
|
|
log.Emergency("emergency")
|
2016-01-13 01:21:32 +00:00
|
|
|
rotateName := "test3" + fmt.Sprintf(".%s.%03d", time.Now().Format("2006-01-02"), 1) + ".log"
|
|
|
|
b, err := exists(rotateName)
|
2013-08-27 15:48:58 +00:00
|
|
|
if !b || err != nil {
|
2016-01-13 01:21:32 +00:00
|
|
|
os.Remove("test3.log")
|
2014-07-11 09:15:34 +00:00
|
|
|
t.Fatal("rotate not generated")
|
2013-08-27 15:48:58 +00:00
|
|
|
}
|
2016-01-13 01:21:32 +00:00
|
|
|
os.Remove(rotateName)
|
2013-08-27 15:48:58 +00:00
|
|
|
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
|
|
|
|
}
|
2013-08-28 07:48:43 +00:00
|
|
|
|
|
|
|
func BenchmarkFile(b *testing.B) {
|
|
|
|
log := NewLogger(100000)
|
|
|
|
log.SetLogger("file", `{"filename":"test4.log"}`)
|
|
|
|
for i := 0; i < b.N; i++ {
|
2014-07-11 09:15:34 +00:00
|
|
|
log.Debug("debug")
|
2013-08-28 07:48:43 +00:00
|
|
|
}
|
|
|
|
os.Remove("test4.log")
|
|
|
|
}
|
2016-01-12 09:59:23 +00:00
|
|
|
|
2016-01-13 01:21:32 +00:00
|
|
|
func BenchmarkFileAsynchronous(b *testing.B) {
|
2016-01-12 09:59:23 +00:00
|
|
|
log := NewLogger(100000)
|
|
|
|
log.SetLogger("file", `{"filename":"test4.log"}`)
|
|
|
|
log.Async()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
log.Debug("debug")
|
|
|
|
}
|
|
|
|
os.Remove("test4.log")
|
|
|
|
}
|
2016-01-12 11:09:00 +00:00
|
|
|
|
2016-01-13 01:21:32 +00:00
|
|
|
func BenchmarkFileCallDepth(b *testing.B) {
|
2016-01-12 11:09:00 +00:00
|
|
|
log := NewLogger(100000)
|
|
|
|
log.SetLogger("file", `{"filename":"test4.log"}`)
|
|
|
|
log.EnableFuncCallDepth(true)
|
|
|
|
log.SetLogFuncCallDepth(2)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
log.Debug("debug")
|
|
|
|
}
|
|
|
|
os.Remove("test4.log")
|
|
|
|
}
|
|
|
|
|
2016-01-13 01:21:32 +00:00
|
|
|
func BenchmarkFileAsynchronousCallDepth(b *testing.B) {
|
2016-01-12 11:09:00 +00:00
|
|
|
log := NewLogger(100000)
|
|
|
|
log.SetLogger("file", `{"filename":"test4.log"}`)
|
|
|
|
log.EnableFuncCallDepth(true)
|
|
|
|
log.SetLogFuncCallDepth(2)
|
|
|
|
log.Async()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
log.Debug("debug")
|
|
|
|
}
|
|
|
|
os.Remove("test4.log")
|
|
|
|
}
|
2016-01-13 01:21:32 +00:00
|
|
|
|
|
|
|
func BenchmarkFileOnGoroutine(b *testing.B) {
|
|
|
|
log := NewLogger(100000)
|
|
|
|
log.SetLogger("file", `{"filename":"test4.log"}`)
|
|
|
|
for i := 0; i < b.N; i++ {
|
2016-01-17 15:57:07 +00:00
|
|
|
go log.Debug("debug")
|
2016-01-13 01:21:32 +00:00
|
|
|
}
|
|
|
|
os.Remove("test4.log")
|
|
|
|
}
|