github.com/matrixorigin/matrixone@v1.2.0/pkg/logutil/dragonboat.go (about) 1 // Copyright 2022 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 "github.com/lni/dragonboat/v4/logger" 19 "go.uber.org/zap" 20 "go.uber.org/zap/zapcore" 21 ) 22 23 var _ logger.ILogger = (*DragonboatAdaptLogger)(nil) 24 25 type DragonboatAdaptLogger struct { 26 logger *zap.SugaredLogger 27 atom *zap.AtomicLevel 28 pkgName string 29 } 30 31 func (d *DragonboatAdaptLogger) SetLevel(level logger.LogLevel) { 32 switch level { 33 case logger.CRITICAL: 34 d.atom.SetLevel(zapcore.PanicLevel) 35 case logger.ERROR: 36 d.atom.SetLevel(zapcore.ErrorLevel) 37 case logger.WARNING: 38 d.atom.SetLevel(zapcore.WarnLevel) 39 case logger.INFO: 40 d.atom.SetLevel(zapcore.InfoLevel) 41 case logger.DEBUG: 42 d.atom.SetLevel(zapcore.DebugLevel) 43 default: 44 d.atom.SetLevel(zapcore.DebugLevel) 45 } 46 } 47 48 func (d DragonboatAdaptLogger) Debugf(format string, args ...interface{}) { 49 d.logger.Debugf(format, args...) 50 } 51 52 func (d DragonboatAdaptLogger) Infof(format string, args ...interface{}) { 53 d.logger.Infof(format, args...) 54 } 55 56 func (d DragonboatAdaptLogger) Warningf(format string, args ...interface{}) { 57 d.logger.Warnf(format, args...) 58 } 59 60 func (d DragonboatAdaptLogger) Errorf(format string, args ...interface{}) { 61 d.logger.Errorf(format, args...) 62 } 63 64 func (d DragonboatAdaptLogger) Panicf(format string, args ...interface{}) { 65 d.logger.Panicf(format, args...) 66 } 67 68 // DragonboatFactory implement logger.Factory for logger.SetLoggerFactory 69 // create DragonboatAdaptLogger intance 70 func DragonboatFactory(name string) logger.ILogger { 71 var cores = make([]zapcore.Core, 0, 2) 72 cfg := getGlobalLogConfig() 73 atom := cfg.getLevel() 74 sinks := cfg.getSinks() 75 for _, sink := range sinks { 76 cores = append(cores, zapcore.NewCore(sink.enc, sink.out, atom)) 77 } 78 options := cfg.getOptions() 79 options = append(options, zap.AddCallerSkip(2)) 80 return &DragonboatAdaptLogger{ 81 logger: zap.New(zapcore.NewTee(cores...), options...).Named(name).Sugar(), 82 atom: &atom, 83 pkgName: name, 84 } 85 } 86 87 func init() { 88 logger.SetLoggerFactory(DragonboatFactory) 89 }