github.com/matrixorigin/matrixone@v0.7.0/pkg/logutil/api.go (about) 1 // Copyright 2021 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package logutil 16 17 import ( 18 "fmt" 19 20 "go.uber.org/zap" 21 "go.uber.org/zap/zapcore" 22 ) 23 24 func Debug(msg string, fields ...zap.Field) { 25 GetSkip1Logger().Debug(msg, fields...) 26 } 27 28 func Info(msg string, fields ...zap.Field) { 29 GetSkip1Logger().Info(msg, fields...) 30 } 31 32 func Warn(msg string, fields ...zap.Field) { 33 GetSkip1Logger().Warn(msg, fields...) 34 } 35 36 func Error(msg string, fields ...zap.Field) { 37 GetErrorLogger().Error(msg, fields...) 38 } 39 40 func Panic(msg string, fields ...zap.Field) { 41 GetSkip1Logger().Panic(msg, fields...) 42 } 43 44 func Fatal(msg string, fields ...zap.Field) { 45 GetSkip1Logger().Fatal(msg, fields...) 46 } 47 48 // Debugf only use in develop mode 49 func Debugf(msg string, fields ...interface{}) { 50 logger := GetSkip1Logger() 51 if logger.Core().Enabled(zap.DebugLevel) { 52 logger.Debug(fmt.Sprintf(msg, fields...)) 53 } 54 } 55 56 // Infof only use in develop mode 57 func Infof(msg string, fields ...interface{}) { 58 GetSkip1Logger().Info(fmt.Sprintf(msg, fields...)) 59 } 60 61 // Warnf only use in develop mode 62 func Warnf(msg string, fields ...interface{}) { 63 GetSkip1Logger().Warn(fmt.Sprintf(msg, fields...)) 64 } 65 66 // Errorf only use in develop mode 67 func Errorf(msg string, fields ...interface{}) { 68 if len(fields) == 0 { 69 GetErrorLogger().Error(msg) 70 } else { 71 GetErrorLogger().Error(fmt.Sprintf(msg, fields...)) 72 } 73 } 74 75 // Panicf only use in develop mode 76 func Panicf(msg string, fields ...interface{}) { 77 GetSkip1Logger().Panic(fmt.Sprintf(msg, fields...)) 78 } 79 80 // Fatalf only use in develop mode 81 func Fatalf(msg string, fields ...interface{}) { 82 GetSkip1Logger().Fatal(fmt.Sprintf(msg, fields...)) 83 } 84 85 // TODO: uncomment the function when changing log level at runtime is required 86 //func handleLevelChange(port string, pattern string, level zap.AtomicLevel) { 87 // http.HandleFunc(pattern, level.ServeHTTP) 88 // go func() { 89 // if err := http.ListenAndServe(port, nil); err != nil { 90 // panic(err) 91 // } 92 // }() 93 //} 94 95 type GoettyLogger struct{} 96 97 func (l *GoettyLogger) Infof(msg string, fields ...interface{}) { 98 Infof(msg, fields...) 99 } 100 101 func (l *GoettyLogger) Debugf(msg string, fields ...interface{}) { 102 Debugf(msg, fields...) 103 } 104 105 func (l *GoettyLogger) Errorf(msg string, fields ...interface{}) { 106 Errorf(msg, fields...) 107 } 108 109 func (l *GoettyLogger) Fatalf(msg string, fields ...interface{}) { 110 Fatalf(msg, fields...) 111 } 112 113 // Adjust returns default logger if logger is nil 114 func Adjust(logger *zap.Logger, options ...zap.Option) *zap.Logger { 115 if logger != nil { 116 return logger 117 } 118 return GetLogger(options...) 119 } 120 121 // GetLoggerWithOptions get default zap logger 122 func GetLoggerWithOptions(level zapcore.LevelEnabler, encoder zapcore.Encoder, syncer zapcore.WriteSyncer, options ...zap.Option) *zap.Logger { 123 var cores []zapcore.Core 124 options = append(options, zap.AddStacktrace(zapcore.FatalLevel), zap.AddCaller()) 125 if syncer == nil { 126 syncer = getConsoleSyncer() 127 } 128 if encoder == nil { 129 encoder = getLoggerEncoder("") 130 } 131 cores = append(cores, zapcore.NewCore(encoder, syncer, level)) 132 133 if EnableStoreDB() { 134 encoder, syncer := getTraceLogSinks() 135 cores = append(cores, zapcore.NewCore(encoder, syncer, level)) 136 } 137 138 return zap.New(zapcore.NewTee(cores...), options...) 139 } 140 141 // GetLogger get default zap logger 142 func GetLogger(options ...zap.Option) *zap.Logger { 143 return GetLoggerWithOptions(zapcore.DebugLevel, nil, nil, options...) 144 } 145 146 // GetPanicLogger returns a zap logger which will panic on Fatal(). The 147 // returned zap logger should only be used in tests. 148 func GetPanicLogger(options ...zap.Option) *zap.Logger { 149 return GetPanicLoggerWithLevel(zapcore.DebugLevel, options...) 150 } 151 152 // GetPanicLoggerWithLevel returns a zap logger which will panic on Fatal(). The 153 // returned zap logger should only be used in tests. 154 func GetPanicLoggerWithLevel(level zapcore.Level, options ...zap.Option) *zap.Logger { 155 return GetLoggerWithOptions(level, nil, nil, 156 zap.OnFatal(zapcore.WriteThenPanic)) 157 }