github.com/amazechain/amc@v0.1.3/log/root.go (about) 1 // Copyright 2023 The AmazeChain Authors 2 // This file is part of the AmazeChain library. 3 // 4 // The AmazeChain library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The AmazeChain library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package log 18 19 import ( 20 "fmt" 21 "os" 22 "sync" 23 24 "github.com/amazechain/amc/conf" 25 prefixed "github.com/amazechain/amc/log/logrus-prefixed-formatter" 26 "github.com/sirupsen/logrus" 27 28 lumberjack "gopkg.in/natefinch/lumberjack.v2" 29 ) 30 31 var ( 32 root = &logger{ctx: []interface{}{}, mapPool: sync.Pool{ 33 New: func() any { 34 return map[string]interface{}{} 35 }, 36 }} 37 terminal = logrus.New() 38 ) 39 40 type Lvl int 41 42 const skipLevel = 3 43 44 const ( 45 LvlCrit Lvl = iota 46 LvlFatal 47 LvlError 48 LvlWarn 49 LvlInfo 50 LvlDebug 51 LvlTrace 52 ) 53 54 func Init(nodeConfig conf.NodeConfig, config conf.LoggerConfig) { 55 formatter := new(prefixed.TextFormatter) 56 formatter.TimestampFormat = "2006-01-02 15:04:05" 57 formatter.FullTimestamp = true 58 formatter.DisableColors = false 59 logrus.SetFormatter(formatter) 60 lvl, _ := logrus.ParseLevel(config.Level) 61 logrus.SetLevel(lvl) 62 63 jsonFormatter := new(logrus.JSONFormatter) 64 jsonFormatter.TimestampFormat = "2006-01-02 15:04:05" 65 terminal.SetFormatter(jsonFormatter) 66 terminal.SetLevel(lvl) 67 terminal.SetOutput(&lumberjack.Logger{ 68 Filename: fmt.Sprintf("%s/log/%s", nodeConfig.DataDir, config.LogFile), 69 MaxSize: config.MaxSize, 70 MaxBackups: config.MaxBackups, 71 MaxAge: config.MaxAge, 72 Compress: config.Compress, 73 }) 74 } 75 76 func InitMobileLogger(filepath string, isDebug bool) { 77 if !isDebug { 78 return 79 } 80 formatter := new(prefixed.TextFormatter) 81 formatter.TimestampFormat = "2006-01-02 15:04:05" 82 formatter.FullTimestamp = true 83 formatter.DisableColors = false 84 logrus.SetFormatter(formatter) 85 if isDebug { 86 terminal.SetLevel(logrus.DebugLevel) 87 } else { 88 terminal.SetLevel(logrus.InfoLevel) 89 } 90 terminal.SetOutput(&lumberjack.Logger{ 91 Filename: filepath, 92 MaxSize: 10, //10MB 93 MaxBackups: 2, 94 LocalTime: false, 95 Compress: false, 96 }) 97 } 98 99 // New returns a new logger with the given context. 100 // New is a convenient alias for Root().New 101 func New(ctx ...interface{}) Logger { 102 return root.New(ctx...) 103 } 104 105 // Root returns the root logger 106 func Root() Logger { 107 return root 108 } 109 110 // Trace is a convenient alias for Root().Trace 111 func Trace(msg string, ctx ...interface{}) { 112 root.write(msg, LvlTrace, ctx, skipLevel) 113 } 114 115 func Tracef(msg string, ctx ...interface{}) { 116 root.write(fmt.Sprintf(msg, ctx...), LvlTrace, []interface{}{}, skipLevel) 117 } 118 119 // Debug is a convenient alias for Root().Debug 120 func Debug(msg string, ctx ...interface{}) { 121 root.write(msg, LvlDebug, ctx, skipLevel) 122 } 123 124 func Debugf(msg string, ctx ...interface{}) { 125 root.write(fmt.Sprintf(msg, ctx...), LvlDebug, []interface{}{}, skipLevel) 126 } 127 128 // Info is a convenient alias for Root().Info 129 func Info(msg string, ctx ...interface{}) { 130 root.write(msg, LvlInfo, ctx, skipLevel) 131 } 132 133 // Infof is a convenient alias for Root().Info 134 func Infof(msg string, ctx ...interface{}) { 135 root.write(fmt.Sprintf(msg, ctx...), LvlInfo, []interface{}{}, skipLevel) 136 } 137 138 // Warn is a convenient alias for Root().Warn 139 func Warn(msg string, ctx ...interface{}) { 140 root.write(msg, LvlWarn, ctx, skipLevel) 141 } 142 143 // Warnf is a convenient alias for Root().Warn 144 func Warnf(msg string, ctx ...interface{}) { 145 root.write(fmt.Sprintf(msg, ctx...), LvlWarn, []interface{}{}, skipLevel) 146 } 147 148 // Error is a convenient alias for Root().Error 149 func Error(msg string, ctx ...interface{}) { 150 root.write(msg, LvlError, ctx, skipLevel) 151 } 152 153 // Errorf is a convenient alias for Root().Error 154 func Errorf(msg string, ctx ...interface{}) { 155 root.write(fmt.Sprintf(msg, ctx...), LvlError, []interface{}{}, skipLevel) 156 } 157 158 // Crit is a convenient alias for Root().Crit 159 func Crit(msg string, ctx ...interface{}) { 160 root.write(msg, LvlCrit, ctx, skipLevel) 161 os.Exit(1) 162 } 163 164 // Critf is a convenient alias for Root().Crit 165 func Critf(msg string, ctx ...interface{}) { 166 root.write(fmt.Sprintf(msg, ctx...), LvlCrit, []interface{}{}, skipLevel) 167 os.Exit(1) 168 } 169 170 // A Logger writes key/value pairs to a Handler 171 type Logger interface { 172 // New returns a new Logger that has this logger's context plus the given context 173 New(ctx ...interface{}) Logger 174 175 // Log a message at the given level with context key/value pairs 176 Trace(msg string, ctx ...interface{}) 177 Debug(msg string, ctx ...interface{}) 178 Info(msg string, ctx ...interface{}) 179 Warn(msg string, ctx ...interface{}) 180 Error(msg string, ctx ...interface{}) 181 Crit(msg string, ctx ...interface{}) 182 } 183 184 // TerminalStringer is an analogous interface to the stdlib stringer, allowing 185 // own types to have custom shortened serialization formats when printed to the 186 // screen. 187 type TerminalStringer interface { 188 TerminalString() string 189 }