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