multifile_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package logs
  15. import (
  16. "bufio"
  17. "os"
  18. "strconv"
  19. "strings"
  20. "testing"
  21. )
  22. func TestFiles_1(t *testing.T) {
  23. log := NewLogger(10000)
  24. log.SetLogger("multifile", `{"filename":"test.log","separate":["emergency", "alert", "critical", "error", "warning", "notice", "info", "debug"]}`)
  25. log.Debug("debug")
  26. log.Informational("info")
  27. log.Notice("notice")
  28. log.Warning("warning")
  29. log.Error("error")
  30. log.Alert("alert")
  31. log.Critical("critical")
  32. log.Emergency("emergency")
  33. fns := []string{""}
  34. fns = append(fns, levelNames[0:]...)
  35. name := "test"
  36. suffix := ".log"
  37. for _, fn := range fns {
  38. file := name + suffix
  39. if fn != "" {
  40. file = name + "." + fn + suffix
  41. }
  42. f, err := os.Open(file)
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. b := bufio.NewReader(f)
  47. lineNum := 0
  48. lastLine := ""
  49. for {
  50. line, _, err := b.ReadLine()
  51. if err != nil {
  52. break
  53. }
  54. if len(line) > 0 {
  55. lastLine = string(line)
  56. lineNum++
  57. }
  58. }
  59. var expected = 1
  60. if fn == "" {
  61. expected = LevelDebug + 1
  62. }
  63. if lineNum != expected {
  64. t.Fatal(file, "has", lineNum, "lines not "+strconv.Itoa(expected)+" lines")
  65. }
  66. if lineNum == 1 {
  67. if !strings.Contains(lastLine, fn) {
  68. t.Fatal(file + " " + lastLine + " not contains the log msg " + fn)
  69. }
  70. }
  71. os.Remove(file)
  72. }
  73. }