mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 14:20:54 +00:00
add test files and bug fixed
This commit is contained in:
parent
f8c4b3aa4c
commit
1f716dda3e
@ -17,6 +17,7 @@ package logs
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"time"
|
"time"
|
||||||
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A filesLogWriter manages several fileLogWriter
|
// A filesLogWriter manages several fileLogWriter
|
||||||
@ -56,14 +57,20 @@ func (f *filesLogWriter) Init(config string) error {
|
|||||||
|
|
||||||
json.Unmarshal([]byte(config), f)
|
json.Unmarshal([]byte(config), f)
|
||||||
|
|
||||||
|
jsonMap := map[string]interface{}{}
|
||||||
|
|
||||||
|
json.Unmarshal([]byte(config), &jsonMap)
|
||||||
|
|
||||||
for i := LevelEmergency; i < LevelDebug+1; i++ {
|
for i := LevelEmergency; i < LevelDebug+1; i++ {
|
||||||
for _, v := range f.Separate {
|
for _, v := range f.Separate {
|
||||||
if v == levelNames[i] {
|
if v == levelNames[i] {
|
||||||
|
jsonMap["filename"] = f.fullLogWriter.fileNameOnly + "." + levelNames[i] + f.fullLogWriter.suffix
|
||||||
|
jsonMap["level"] = i
|
||||||
|
bs, _ := json.Marshal(jsonMap)
|
||||||
writer = newFileWriter().(*fileLogWriter)
|
writer = newFileWriter().(*fileLogWriter)
|
||||||
writer.Init(config)
|
writer.Init(string(bs))
|
||||||
writer.Level = i
|
|
||||||
writer.fileNameOnly += "." + levelNames[i]
|
|
||||||
f.writers[i] = writer
|
f.writers[i] = writer
|
||||||
|
fmt.Println(writer.Filename)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -107,5 +114,5 @@ func newFilesWriter() Logger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
Register("files", NewConn)
|
Register("files", newFilesWriter)
|
||||||
}
|
}
|
||||||
|
78
logs/files_test.go
Normal file
78
logs/files_test.go
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
// 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"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFiles_1(t *testing.T) {
|
||||||
|
log := NewLogger(10000)
|
||||||
|
log.SetLogger("files", `{"filename":"test.log","separate":["emergency", "alert", "critical", "error", "warning", "notice", "info", "debug"]}`)
|
||||||
|
log.Debug("debug")
|
||||||
|
log.Informational("info")
|
||||||
|
log.Notice("notice")
|
||||||
|
log.Warning("warning")
|
||||||
|
log.Error("error")
|
||||||
|
log.Alert("alert")
|
||||||
|
log.Critical("critical")
|
||||||
|
log.Emergency("emergency")
|
||||||
|
fns := []string{""}
|
||||||
|
fns = append(fns, levelNames[0:]...)
|
||||||
|
name := "test"
|
||||||
|
suffix := ".log"
|
||||||
|
for _, fn := range fns {
|
||||||
|
|
||||||
|
file := name + suffix
|
||||||
|
if fn != "" {
|
||||||
|
file = name + "." + fn + suffix
|
||||||
|
}
|
||||||
|
f, err := os.Open(file)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
b := bufio.NewReader(f)
|
||||||
|
lineNum := 0
|
||||||
|
lastLine := ""
|
||||||
|
for {
|
||||||
|
line, _, err := b.ReadLine()
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if len(line) > 0 {
|
||||||
|
lastLine = string(line)
|
||||||
|
lineNum++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var expected = 1
|
||||||
|
if fn == "" {
|
||||||
|
expected = LevelDebug + 1
|
||||||
|
}
|
||||||
|
if lineNum != expected {
|
||||||
|
t.Fatal(file, "has", lineNum, "lines not "+strconv.Itoa(expected)+" lines")
|
||||||
|
}
|
||||||
|
if lineNum == 1 {
|
||||||
|
if !strings.Contains(lastLine, fn) {
|
||||||
|
t.Fatal(file + " " + lastLine + " not contains the log msg " + fn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
os.Remove(file)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user