github.com/matrixorigin/matrixone@v1.2.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( 123 level zapcore.LevelEnabler, 124 encoder zapcore.Encoder, 125 syncer zapcore.WriteSyncer, 126 options ...zap.Option) *zap.Logger { 127 var cores []zapcore.Core 128 if EnableLog() { 129 if syncer == nil { 130 syncer = getConsoleSyncer() 131 } 132 if encoder == nil { 133 encoder = getLoggerEncoder("console") 134 } 135 cores = append(cores, zapcore.NewCore(encoder, syncer, level)) 136 } 137 138 if EnableStoreDB() { 139 encoder, syncer := getTraceLogSinks() 140 cores = append(cores, zapcore.NewCore(encoder, syncer, level)) 141 } 142 143 return zap.New(zapcore.NewTee(cores...), options...) 144 } 145 146 // GetLogger get default zap logger 147 func GetLogger(options ...zap.Option) *zap.Logger { 148 return GetLoggerWithOptions(zapcore.DebugLevel, nil, nil, options...) 149 } 150 151 // GetPanicLogger returns a zap logger which will panic on Fatal(). The 152 // returned zap logger should only be used in tests. 153 func GetPanicLogger(options ...zap.Option) *zap.Logger { 154 return GetPanicLoggerWithLevel(zapcore.DebugLevel, options...) 155 } 156 157 // GetPanicLoggerWithLevel returns a zap logger which will panic on Fatal(). The 158 // returned zap logger should only be used in tests. 159 func GetPanicLoggerWithLevel(level zapcore.Level, options ...zap.Option) *zap.Logger { 160 return GetLoggerWithOptions(level, nil, nil, 161 zap.WithFatalHook(zapcore.WriteThenPanic)) 162 }