multifile.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. "encoding/json"
  17. "time"
  18. )
  19. // A filesLogWriter manages several fileLogWriter
  20. // filesLogWriter will write logs to the file in json configuration and write the same level log to correspond file
  21. // means if the file name in configuration is project.log filesLogWriter will create project.error.log/project.debug.log
  22. // and write the error-level logs to project.error.log and write the debug-level logs to project.debug.log
  23. // the rotate attribute also acts like fileLogWriter
  24. type multiFileLogWriter struct {
  25. writers [LevelDebug + 1 + 1]*fileLogWriter // the last one for fullLogWriter
  26. fullLogWriter *fileLogWriter
  27. Separate []string `json:"separate"`
  28. }
  29. var levelNames = [...]string{"emergency", "alert", "critical", "error", "warning", "notice", "info", "debug"}
  30. // Init file logger with json config.
  31. // jsonConfig like:
  32. // {
  33. // "filename":"logs/beego.log",
  34. // "maxLines":0,
  35. // "maxsize":0,
  36. // "daily":true,
  37. // "maxDays":15,
  38. // "rotate":true,
  39. // "perm":0600,
  40. // "separate":["emergency", "alert", "critical", "error", "warning", "notice", "info", "debug"],
  41. // }
  42. func (f *multiFileLogWriter) Init(config string) error {
  43. writer := newFileWriter().(*fileLogWriter)
  44. err := writer.Init(config)
  45. if err != nil {
  46. return err
  47. }
  48. f.fullLogWriter = writer
  49. f.writers[LevelDebug+1] = writer
  50. //unmarshal "separate" field to f.Separate
  51. json.Unmarshal([]byte(config), f)
  52. jsonMap := map[string]interface{}{}
  53. json.Unmarshal([]byte(config), &jsonMap)
  54. for i := LevelEmergency; i < LevelDebug+1; i++ {
  55. for _, v := range f.Separate {
  56. if v == levelNames[i] {
  57. jsonMap["filename"] = f.fullLogWriter.fileNameOnly + "." + levelNames[i] + f.fullLogWriter.suffix
  58. jsonMap["level"] = i
  59. bs, _ := json.Marshal(jsonMap)
  60. writer = newFileWriter().(*fileLogWriter)
  61. writer.Init(string(bs))
  62. f.writers[i] = writer
  63. }
  64. }
  65. }
  66. return nil
  67. }
  68. func (f *multiFileLogWriter) Destroy() {
  69. for i := 0; i < len(f.writers); i++ {
  70. if f.writers[i] != nil {
  71. f.writers[i].Destroy()
  72. }
  73. }
  74. }
  75. func (f *multiFileLogWriter) WriteMsg(when time.Time, msg string, level int) error {
  76. if f.fullLogWriter != nil {
  77. f.fullLogWriter.WriteMsg(when, msg, level)
  78. }
  79. for i := 0; i < len(f.writers)-1; i++ {
  80. if f.writers[i] != nil {
  81. if level == f.writers[i].Level {
  82. f.writers[i].WriteMsg(when, msg, level)
  83. }
  84. }
  85. }
  86. return nil
  87. }
  88. func (f *multiFileLogWriter) Flush() {
  89. for i := 0; i < len(f.writers); i++ {
  90. if f.writers[i] != nil {
  91. f.writers[i].Flush()
  92. }
  93. }
  94. }
  95. // newFilesWriter create a FileLogWriter returning as LoggerInterface.
  96. func newFilesWriter() Logger {
  97. return &multiFileLogWriter{}
  98. }
  99. func init() {
  100. Register(AdapterMultiFile, newFilesWriter)
  101. }