github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/framework/logs/log_multifiles.go (about)

     1  // the package is exported from github.com/beego/beego/v2/core/logs
     2  
     3  // Copyright 2023. All Rights Reserved.
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //      http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package logs
    18  
    19  import (
    20  	"encoding/json"
    21  )
    22  
    23  // A filesLogWriter manages several fileLogWriter
    24  // filesLogWriter will write logs to the file in json configuration  and write the same level log to correspond file
    25  // means if the file name in configuration is project.log filesLogWriter will create project.error.log/project.debug.log
    26  // and write the error-level logs to project.error.log and write the debug-level logs to project.debug.log
    27  // the rotate attribute also  acts like fileLogWriter
    28  type multiFileLogWriter struct {
    29  	writers       [LevelDebug + 1 + 1 + 1]*fileLogWriter // the last one for fullLogWriter
    30  	fullLogWriter *fileLogWriter
    31  	Separate      []string `json:"separate"`
    32  }
    33  
    34  var levelNames = [...]string{"emergency", "alert", "critical", "error", "warning", "notice", "info", "debug", "performance"}
    35  
    36  // Init file logger with json config.
    37  // jsonConfig like:
    38  //	{
    39  //	"filename":"logs/iac.log",
    40  //	"maxLines":0,
    41  //	"maxsize":0,
    42  //	"daily":true,
    43  //	"maxDays":15,
    44  //	"rotate":true,
    45  //  	"perm":0600,
    46  //	"separate":["emergency", "alert", "critical", "error", "warning", "notice", "info", "debug"],
    47  //	}
    48  
    49  func (f *multiFileLogWriter) Init(config string) error {
    50  	writer := newFileWriter().(*fileLogWriter)
    51  	err := writer.Init(config)
    52  	if err != nil {
    53  		return err
    54  	}
    55  	f.fullLogWriter = writer
    56  	f.writers[LevelDebug+1+1] = writer
    57  
    58  	// unmarshal "separate" field to f.Separate
    59  	err = json.Unmarshal([]byte(config), f)
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	jsonMap := map[string]interface{}{}
    65  	err = json.Unmarshal([]byte(config), &jsonMap)
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    70  	for i := LevelEmergency; i < LevelDebug+1; i++ {
    71  		for _, v := range f.Separate {
    72  			if v == levelNames[i] {
    73  				jsonMap["filename"] = f.fullLogWriter.fileNameOnly + "." + levelNames[i] + f.fullLogWriter.suffix
    74  				jsonMap["level"] = i
    75  				bs, _ := json.Marshal(jsonMap)
    76  				writer = newFileWriter().(*fileLogWriter)
    77  				err := writer.Init(string(bs))
    78  				if err != nil {
    79  					return err
    80  				}
    81  				f.writers[i] = writer
    82  			}
    83  		}
    84  	}
    85  	return nil
    86  }
    87  
    88  func (*multiFileLogWriter) Format(lm *LogMsg) string {
    89  	return lm.OldStyleFormat()
    90  }
    91  
    92  func (f *multiFileLogWriter) SetFormatter(fmt LogFormatter) {
    93  	f.fullLogWriter.SetFormatter(fmt)
    94  }
    95  
    96  func (f *multiFileLogWriter) Destroy() {
    97  	for i := 0; i < len(f.writers); i++ {
    98  		if f.writers[i] != nil {
    99  			f.writers[i].Destroy()
   100  		}
   101  	}
   102  }
   103  
   104  func (f *multiFileLogWriter) WriteMsg(lm *LogMsg) error {
   105  	if f.fullLogWriter != nil {
   106  		f.fullLogWriter.WriteMsg(lm)
   107  	}
   108  	for i := 0; i < len(f.writers)-1; i++ {
   109  		if f.writers[i] != nil {
   110  			if lm.Level == f.writers[i].Level {
   111  				f.writers[i].WriteMsg(lm)
   112  			}
   113  		}
   114  	}
   115  	return nil
   116  }
   117  
   118  func (f *multiFileLogWriter) Flush() {
   119  	for i := 0; i < len(f.writers); i++ {
   120  		if f.writers[i] != nil {
   121  			f.writers[i].Flush()
   122  		}
   123  	}
   124  }
   125  
   126  // newFilesWriter create a FileLogWriter returning as LoggerInterface.
   127  func newFilesWriter() Logger {
   128  	res := &multiFileLogWriter{}
   129  	return res
   130  }
   131  
   132  func init() {
   133  	Register(AdapterMultiFile, newFilesWriter)
   134  }