github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/framework/logs/log_console.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 "fmt" 22 "os" 23 "strings" 24 25 "github.com/pkg/errors" 26 "github.com/shiena/ansicolor" 27 ) 28 29 // brush is a color join function 30 type brush func(string) string 31 32 // newBrush returns a fix color Brush 33 func newBrush(color string) brush { 34 pre := "\033[" 35 reset := "\033[0m" 36 return func(text string) string { 37 return pre + color + "m" + text + reset 38 } 39 } 40 41 var colors = []brush{ 42 newBrush("1;37"), // Emergency white 43 newBrush("1;36"), // Alert cyan 44 newBrush("1;35"), // Critical magenta 45 newBrush("1;31"), // Error red 46 newBrush("1;33"), // Warning yellow 47 newBrush("1;32"), // Notice green 48 newBrush("1;34"), // Informational blue 49 newBrush("1;44"), // Debug Background blue 50 newBrush("1;32"), // Performance Background blue 51 } 52 53 // consoleWriter implements LoggerInterface and writes messages to terminal. 54 type consoleWriter struct { 55 lg *logWriter 56 formatter LogFormatter 57 Formatter string `json:"formatter"` 58 Level int `json:"level"` 59 Colorful bool `json:"color"` // this filed is useful only when system's terminal supports color 60 } 61 62 func (c *consoleWriter) Format(lm *LogMsg) string { 63 msg := lm.OldStyleFormat() 64 if c.Colorful { 65 msg = strings.Replace(msg, levelPrefix[lm.Level], colors[lm.Level](levelPrefix[lm.Level]), 1) 66 } 67 h, _, _ := formatTimeHeader(lm.When) 68 return string(append(h, msg...)) 69 } 70 71 func (c *consoleWriter) SetFormatter(f LogFormatter) { 72 c.formatter = f 73 } 74 75 // NewConsole creates ConsoleWriter returning as LoggerInterface. 76 func NewConsole() Logger { 77 return newConsole() 78 } 79 80 func newConsole() *consoleWriter { 81 cw := &consoleWriter{ 82 lg: newLogWriter(ansicolor.NewAnsiColorWriter(os.Stdout)), 83 Level: LevelDebug, 84 Colorful: true, 85 } 86 cw.formatter = cw 87 return cw 88 } 89 90 // Init initianlizes the console logger. 91 // jsonConfig must be in the format '{"level":LevelTrace}' 92 func (c *consoleWriter) Init(config string) error { 93 if len(config) == 0 { 94 return nil 95 } 96 97 res := json.Unmarshal([]byte(config), c) 98 99 if res == nil && len(c.Formatter) > 0 { 100 fmtr, ok := GetFormatter(c.Formatter) 101 if !ok { 102 return errors.New(fmt.Sprintf("the formatter with name: %s not found", c.Formatter)) 103 } 104 c.formatter = fmtr 105 } else { 106 c.Level = LevelDebug 107 c.Colorful = true 108 } 109 return res 110 } 111 112 // WriteMsg writes message in console. 113 func (c *consoleWriter) WriteMsg(lm *LogMsg) error { 114 if lm.Level > c.Level && lm.Level != LeverPerformance { 115 return nil 116 } 117 msg := c.formatter.Format(lm) 118 c.lg.writeln(msg) 119 return nil 120 } 121 122 // Destroy implementing method. empty. 123 func (c *consoleWriter) Destroy() { 124 } 125 126 // Flush implementing method. empty. 127 func (c *consoleWriter) Flush() { 128 } 129 130 func init() { 131 Register(AdapterConsole, NewConsole) 132 }