file_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. "fmt"
  18. "io/ioutil"
  19. "os"
  20. "strconv"
  21. "testing"
  22. "time"
  23. )
  24. func TestFilePerm(t *testing.T) {
  25. log := NewLogger(10000)
  26. // use 0666 as test perm cause the default umask is 022
  27. log.SetLogger("file", `{"filename":"test.log", "perm": "0666"}`)
  28. log.Debug("debug")
  29. log.Informational("info")
  30. log.Notice("notice")
  31. log.Warning("warning")
  32. log.Error("error")
  33. log.Alert("alert")
  34. log.Critical("critical")
  35. log.Emergency("emergency")
  36. file, err := os.Stat("test.log")
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. if file.Mode() != 0666 {
  41. t.Fatal("unexpected log file permission")
  42. }
  43. os.Remove("test.log")
  44. }
  45. func TestFile1(t *testing.T) {
  46. log := NewLogger(10000)
  47. log.SetLogger("file", `{"filename":"test.log"}`)
  48. log.Debug("debug")
  49. log.Informational("info")
  50. log.Notice("notice")
  51. log.Warning("warning")
  52. log.Error("error")
  53. log.Alert("alert")
  54. log.Critical("critical")
  55. log.Emergency("emergency")
  56. f, err := os.Open("test.log")
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. b := bufio.NewReader(f)
  61. lineNum := 0
  62. for {
  63. line, _, err := b.ReadLine()
  64. if err != nil {
  65. break
  66. }
  67. if len(line) > 0 {
  68. lineNum++
  69. }
  70. }
  71. var expected = LevelDebug + 1
  72. if lineNum != expected {
  73. t.Fatal(lineNum, "not "+strconv.Itoa(expected)+" lines")
  74. }
  75. os.Remove("test.log")
  76. }
  77. func TestFile2(t *testing.T) {
  78. log := NewLogger(10000)
  79. log.SetLogger("file", fmt.Sprintf(`{"filename":"test2.log","level":%d}`, LevelError))
  80. log.Debug("debug")
  81. log.Info("info")
  82. log.Notice("notice")
  83. log.Warning("warning")
  84. log.Error("error")
  85. log.Alert("alert")
  86. log.Critical("critical")
  87. log.Emergency("emergency")
  88. f, err := os.Open("test2.log")
  89. if err != nil {
  90. t.Fatal(err)
  91. }
  92. b := bufio.NewReader(f)
  93. lineNum := 0
  94. for {
  95. line, _, err := b.ReadLine()
  96. if err != nil {
  97. break
  98. }
  99. if len(line) > 0 {
  100. lineNum++
  101. }
  102. }
  103. var expected = LevelError + 1
  104. if lineNum != expected {
  105. t.Fatal(lineNum, "not "+strconv.Itoa(expected)+" lines")
  106. }
  107. os.Remove("test2.log")
  108. }
  109. func TestFileRotate_01(t *testing.T) {
  110. log := NewLogger(10000)
  111. log.SetLogger("file", `{"filename":"test3.log","maxlines":4}`)
  112. log.Debug("debug")
  113. log.Info("info")
  114. log.Notice("notice")
  115. log.Warning("warning")
  116. log.Error("error")
  117. log.Alert("alert")
  118. log.Critical("critical")
  119. log.Emergency("emergency")
  120. rotateName := "test3" + fmt.Sprintf(".%s.%03d", time.Now().Format("2006-01-02"), 1) + ".log"
  121. b, err := exists(rotateName)
  122. if !b || err != nil {
  123. os.Remove("test3.log")
  124. t.Fatal("rotate not generated")
  125. }
  126. os.Remove(rotateName)
  127. os.Remove("test3.log")
  128. }
  129. func TestFileRotate_02(t *testing.T) {
  130. fn1 := "rotate_day.log"
  131. fn2 := "rotate_day." + time.Now().Add(-24*time.Hour).Format("2006-01-02") + ".log"
  132. testFileRotate(t, fn1, fn2)
  133. }
  134. func TestFileRotate_03(t *testing.T) {
  135. fn1 := "rotate_day.log"
  136. fn := "rotate_day." + time.Now().Add(-24*time.Hour).Format("2006-01-02") + ".log"
  137. os.Create(fn)
  138. fn2 := "rotate_day." + time.Now().Add(-24*time.Hour).Format("2006-01-02") + ".001.log"
  139. testFileRotate(t, fn1, fn2)
  140. os.Remove(fn)
  141. }
  142. func TestFileRotate_04(t *testing.T) {
  143. fn1 := "rotate_day.log"
  144. fn2 := "rotate_day." + time.Now().Add(-24*time.Hour).Format("2006-01-02") + ".log"
  145. testFileDailyRotate(t, fn1, fn2)
  146. }
  147. func TestFileRotate_05(t *testing.T) {
  148. fn1 := "rotate_day.log"
  149. fn := "rotate_day." + time.Now().Add(-24*time.Hour).Format("2006-01-02") + ".log"
  150. os.Create(fn)
  151. fn2 := "rotate_day." + time.Now().Add(-24*time.Hour).Format("2006-01-02") + ".001.log"
  152. testFileDailyRotate(t, fn1, fn2)
  153. os.Remove(fn)
  154. }
  155. func testFileRotate(t *testing.T, fn1, fn2 string) {
  156. fw := &fileLogWriter{
  157. Daily: true,
  158. MaxDays: 7,
  159. Rotate: true,
  160. Level: LevelTrace,
  161. Perm: "0660",
  162. }
  163. fw.Init(fmt.Sprintf(`{"filename":"%v","maxdays":1}`, fn1))
  164. fw.dailyOpenTime = time.Now().Add(-24 * time.Hour)
  165. fw.dailyOpenDate = fw.dailyOpenTime.Day()
  166. fw.WriteMsg(time.Now(), "this is a msg for test", LevelDebug)
  167. for _, file := range []string{fn1, fn2} {
  168. _, err := os.Stat(file)
  169. if err != nil {
  170. t.FailNow()
  171. }
  172. os.Remove(file)
  173. }
  174. fw.Destroy()
  175. }
  176. func testFileDailyRotate(t *testing.T, fn1, fn2 string) {
  177. fw := &fileLogWriter{
  178. Daily: true,
  179. MaxDays: 7,
  180. Rotate: true,
  181. Level: LevelTrace,
  182. Perm: "0660",
  183. }
  184. fw.Init(fmt.Sprintf(`{"filename":"%v","maxdays":1}`, fn1))
  185. fw.dailyOpenTime = time.Now().Add(-24 * time.Hour)
  186. fw.dailyOpenDate = fw.dailyOpenTime.Day()
  187. today, _ := time.ParseInLocation("2006-01-02", time.Now().Format("2006-01-02"), fw.dailyOpenTime.Location())
  188. today = today.Add(-1 * time.Second)
  189. fw.dailyRotate(today)
  190. for _, file := range []string{fn1, fn2} {
  191. _, err := os.Stat(file)
  192. if err != nil {
  193. t.FailNow()
  194. }
  195. content, err := ioutil.ReadFile(file)
  196. if err != nil {
  197. t.FailNow()
  198. }
  199. if len(content) > 0 {
  200. t.FailNow()
  201. }
  202. os.Remove(file)
  203. }
  204. fw.Destroy()
  205. }
  206. func exists(path string) (bool, error) {
  207. _, err := os.Stat(path)
  208. if err == nil {
  209. return true, nil
  210. }
  211. if os.IsNotExist(err) {
  212. return false, nil
  213. }
  214. return false, err
  215. }
  216. func BenchmarkFile(b *testing.B) {
  217. log := NewLogger(100000)
  218. log.SetLogger("file", `{"filename":"test4.log"}`)
  219. for i := 0; i < b.N; i++ {
  220. log.Debug("debug")
  221. }
  222. os.Remove("test4.log")
  223. }
  224. func BenchmarkFileAsynchronous(b *testing.B) {
  225. log := NewLogger(100000)
  226. log.SetLogger("file", `{"filename":"test4.log"}`)
  227. log.Async()
  228. for i := 0; i < b.N; i++ {
  229. log.Debug("debug")
  230. }
  231. os.Remove("test4.log")
  232. }
  233. func BenchmarkFileCallDepth(b *testing.B) {
  234. log := NewLogger(100000)
  235. log.SetLogger("file", `{"filename":"test4.log"}`)
  236. log.EnableFuncCallDepth(true)
  237. log.SetLogFuncCallDepth(2)
  238. for i := 0; i < b.N; i++ {
  239. log.Debug("debug")
  240. }
  241. os.Remove("test4.log")
  242. }
  243. func BenchmarkFileAsynchronousCallDepth(b *testing.B) {
  244. log := NewLogger(100000)
  245. log.SetLogger("file", `{"filename":"test4.log"}`)
  246. log.EnableFuncCallDepth(true)
  247. log.SetLogFuncCallDepth(2)
  248. log.Async()
  249. for i := 0; i < b.N; i++ {
  250. log.Debug("debug")
  251. }
  252. os.Remove("test4.log")
  253. }
  254. func BenchmarkFileOnGoroutine(b *testing.B) {
  255. log := NewLogger(100000)
  256. log.SetLogger("file", `{"filename":"test4.log"}`)
  257. for i := 0; i < b.N; i++ {
  258. go log.Debug("debug")
  259. }
  260. os.Remove("test4.log")
  261. }