1
0
mirror of https://github.com/astaxie/beego.git synced 2024-06-02 13:03:29 +00:00
Beego/pkg/infrastructure/logs/multifile.go

135 lines
3.6 KiB
Go
Raw Normal View History

2020-07-22 14:50:08 +00:00
// 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 (
"encoding/json"
2020-08-28 17:00:45 +00:00
2020-09-10 15:31:49 +00:00
"github.com/astaxie/beego/pkg/infrastructure/utils"
2020-07-22 14:50:08 +00:00
)
// A filesLogWriter manages several fileLogWriter
// filesLogWriter will write logs to the file in json configuration and write the same level log to correspond file
// means if the file name in configuration is project.log filesLogWriter will create project.error.log/project.debug.log
// and write the error-level logs to project.error.log and write the debug-level logs to project.debug.log
// the rotate attribute also acts like fileLogWriter
type multiFileLogWriter struct {
2020-08-28 17:18:28 +00:00
writers [LevelDebug + 1 + 1]*fileLogWriter // the last one for fullLogWriter
fullLogWriter *fileLogWriter
Separate []string `json:"separate"`
customFormatter func(*LogMsg) string
2020-07-22 14:50:08 +00:00
}
var levelNames = [...]string{"emergency", "alert", "critical", "error", "warning", "notice", "info", "debug"}
// Init file logger with json config.
// jsonConfig like:
// {
// "filename":"logs/beego.log",
// "maxLines":0,
// "maxsize":0,
// "daily":true,
// "maxDays":15,
// "rotate":true,
// "perm":0600,
// "separate":["emergency", "alert", "critical", "error", "warning", "notice", "info", "debug"],
// }
2020-09-10 15:31:49 +00:00
func (f *multiFileLogWriter) Init(jsonConfig string, opts ...utils.KV) error {
2020-08-28 17:18:28 +00:00
for _, elem := range opts {
2020-09-10 15:31:49 +00:00
if elem.GetKey() == "formatter" {
2020-08-28 17:18:28 +00:00
formatter, err := GetFormatter(elem)
if err != nil {
return err
}
f.customFormatter = formatter
}
}
2020-07-22 14:50:08 +00:00
writer := newFileWriter().(*fileLogWriter)
2020-08-28 17:00:45 +00:00
err := writer.Init(jsonConfig)
2020-07-22 14:50:08 +00:00
if err != nil {
return err
}
f.fullLogWriter = writer
f.writers[LevelDebug+1] = writer
//unmarshal "separate" field to f.Separate
2020-08-28 17:00:45 +00:00
json.Unmarshal([]byte(jsonConfig), f)
2020-07-22 14:50:08 +00:00
jsonMap := map[string]interface{}{}
2020-08-28 17:00:45 +00:00
json.Unmarshal([]byte(jsonConfig), &jsonMap)
2020-07-22 14:50:08 +00:00
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)
err := writer.Init(string(bs))
if err != nil {
return err
}
f.writers[i] = writer
}
}
}
return nil
}
2020-08-20 18:00:35 +00:00
func (f *multiFileLogWriter) Format(lm *LogMsg) string {
return lm.Msg
}
2020-07-22 14:50:08 +00:00
func (f *multiFileLogWriter) Destroy() {
for i := 0; i < len(f.writers); i++ {
if f.writers[i] != nil {
f.writers[i].Destroy()
}
}
}
func (f *multiFileLogWriter) WriteMsg(lm *LogMsg) error {
2020-07-22 14:50:08 +00:00
if f.fullLogWriter != nil {
f.fullLogWriter.WriteMsg(lm)
2020-07-22 14:50:08 +00:00
}
for i := 0; i < len(f.writers)-1; i++ {
if f.writers[i] != nil {
if lm.Level == f.writers[i].Level {
f.writers[i].WriteMsg(lm)
2020-07-22 14:50:08 +00:00
}
}
}
return nil
}
func (f *multiFileLogWriter) Flush() {
for i := 0; i < len(f.writers); i++ {
if f.writers[i] != nil {
f.writers[i].Flush()
}
}
}
// newFilesWriter create a FileLogWriter returning as LoggerInterface.
func newFilesWriter() Logger {
return &multiFileLogWriter{}
}
func init() {
Register(AdapterMultiFile, newFilesWriter)
}