github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/framework/logs/logformatter.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  	"fmt"
    21  	"path"
    22  	"strconv"
    23  )
    24  
    25  var formatterMap = make(map[string]LogFormatter, 4)
    26  
    27  type LogFormatter interface {
    28  	Format(lm *LogMsg) string
    29  }
    30  
    31  // PatternLogFormatter provides a quick format method
    32  // for example:
    33  // tes := &PatternLogFormatter{Pattern: "%F:%n|%w %t>> %m", WhenFormat: "2006-01-02"}
    34  // RegisterFormatter("tes", tes)
    35  // SetGlobalFormatter("tes")
    36  type PatternLogFormatter struct {
    37  	Pattern    string
    38  	WhenFormat string
    39  }
    40  
    41  func (p *PatternLogFormatter) getWhenFormatter() string {
    42  	s := p.WhenFormat
    43  	if s == "" {
    44  		s = "2006/01/02 15:04:05.123" // default style
    45  	}
    46  	return s
    47  }
    48  
    49  func (p *PatternLogFormatter) Format(lm *LogMsg) string {
    50  	return p.ToString(lm)
    51  }
    52  
    53  // RegisterFormatter register an formatter. Usually you should use this to extend your custom formatter
    54  // for example:
    55  // RegisterFormatter("my-fmt", &MyFormatter{})
    56  // logs.SetFormatter(Console, `{"formatter": "my-fmt"}`)
    57  func RegisterFormatter(name string, fmtr LogFormatter) {
    58  	formatterMap[name] = fmtr
    59  }
    60  
    61  func GetFormatter(name string) (LogFormatter, bool) {
    62  	res, ok := formatterMap[name]
    63  	return res, ok
    64  }
    65  
    66  // ToString 'w' when, 'm' msg,'f' filename,'F' full path,'n' line number
    67  // 'l' level number, 't' prefix of level type, 'T' full name of level type
    68  func (p *PatternLogFormatter) ToString(lm *LogMsg) string {
    69  	s := []rune(p.Pattern)
    70  	msg := fmt.Sprintf(lm.Msg, lm.Args...)
    71  	m := map[rune]string{
    72  		'w': lm.When.Format(p.getWhenFormatter()),
    73  		'm': msg,
    74  		'n': strconv.Itoa(lm.LineNumber),
    75  		'l': strconv.Itoa(lm.Level),
    76  		't': levelPrefix[lm.Level],
    77  		'T': levelNames[lm.Level],
    78  		'F': lm.FilePath,
    79  	}
    80  	_, m['f'] = path.Split(lm.FilePath)
    81  	res := ""
    82  	for i := 0; i < len(s)-1; i++ {
    83  		if s[i] == '%' {
    84  			if k, ok := m[s[i+1]]; ok {
    85  				res += k
    86  				i++
    87  				continue
    88  			}
    89  		}
    90  		res += string(s[i])
    91  	}
    92  	return res
    93  }