From 1f716dda3e52653a48cdf9777ba965cd47feafa7 Mon Sep 17 00:00:00 2001 From: JessonChan Date: Wed, 3 Feb 2016 17:54:58 +0800 Subject: [PATCH] add test files and bug fixed --- logs/files.go | 15 ++++++--- logs/files_test.go | 78 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 logs/files_test.go diff --git a/logs/files.go b/logs/files.go index 617af1ec..816d42e3 100644 --- a/logs/files.go +++ b/logs/files.go @@ -17,6 +17,7 @@ package logs import ( "encoding/json" "time" + "fmt" ) // A filesLogWriter manages several fileLogWriter @@ -56,14 +57,20 @@ func (f *filesLogWriter) Init(config string) error { json.Unmarshal([]byte(config), f) + jsonMap := map[string]interface{}{} + + json.Unmarshal([]byte(config), &jsonMap) + for i := LevelEmergency; i < LevelDebug+1; i++ { for _, v := range f.Separate { 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.Init(config) - writer.Level = i - writer.fileNameOnly += "." + levelNames[i] + writer.Init(string(bs)) f.writers[i] = writer + fmt.Println(writer.Filename) } } } @@ -107,5 +114,5 @@ func newFilesWriter() Logger { } func init() { - Register("files", NewConn) + Register("files", newFilesWriter) } diff --git a/logs/files_test.go b/logs/files_test.go new file mode 100644 index 00000000..058ba902 --- /dev/null +++ b/logs/files_test.go @@ -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) + } + +}