dubbo.apache.org/dubbo-go/v3@v3.1.1/logger/zap/zap.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. 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 18 package zap 19 20 import ( 21 "os" 22 "strings" 23 ) 24 25 import ( 26 "github.com/dubbogo/gost/log/logger" 27 28 "github.com/mattn/go-colorable" 29 30 "go.uber.org/zap" 31 "go.uber.org/zap/zapcore" 32 ) 33 34 import ( 35 "dubbo.apache.org/dubbo-go/v3/common" 36 "dubbo.apache.org/dubbo-go/v3/common/constant" 37 "dubbo.apache.org/dubbo-go/v3/common/extension" 38 . "dubbo.apache.org/dubbo-go/v3/logger" 39 ) 40 41 func init() { 42 extension.SetLogger("zap", instantiate) 43 } 44 45 func instantiate(config *common.URL) (log logger.Logger, err error) { 46 var ( 47 level string 48 lv zapcore.Level 49 sync []zapcore.WriteSyncer 50 encoder zapcore.Encoder 51 appender []string 52 ) 53 54 level = config.GetParam(constant.LoggerLevelKey, constant.LoggerLevel) 55 if lv, err = zapcore.ParseLevel(level); err != nil { 56 return nil, err 57 } 58 59 appender = strings.Split(config.GetParam(constant.LoggerAppenderKey, constant.LoggerAppender), ",") 60 for _, apt := range appender { 61 switch apt { 62 case "console": 63 sync = append(sync, zapcore.AddSync(os.Stdout)) 64 case "file": 65 file := FileConfig(config) 66 sync = append(sync, zapcore.AddSync(colorable.NewNonColorable(file))) 67 } 68 } 69 70 format := config.GetParam(constant.LoggerFormatKey, constant.LoggerFormat) 71 switch strings.ToLower(format) { 72 case "text": 73 encoder = zapcore.NewConsoleEncoder(encoderConfig()) 74 case "json": 75 ec := encoderConfig() 76 ec.EncodeLevel = zapcore.CapitalLevelEncoder 77 encoder = zapcore.NewJSONEncoder(ec) 78 default: 79 encoder = zapcore.NewConsoleEncoder(encoderConfig()) 80 } 81 82 log = zap.New(zapcore.NewCore(encoder, zapcore.NewMultiWriteSyncer(sync...), lv), 83 zap.AddCaller(), zap.AddCallerSkip(1)).Sugar() 84 return log, nil 85 } 86 87 type Logger struct { 88 lg *zap.SugaredLogger 89 } 90 91 func NewDefault() *Logger { 92 var ( 93 lv zapcore.Level 94 lg *zap.SugaredLogger 95 err error 96 ) 97 if lv, err = zapcore.ParseLevel("info"); err != nil { 98 lv = zapcore.InfoLevel 99 } 100 encoder := zapcore.NewConsoleEncoder(encoderConfig()) 101 lg = zap.New(zapcore.NewCore(encoder, zapcore.AddSync(os.Stdout), lv), 102 zap.AddCaller(), zap.AddCallerSkip(1)).Sugar() 103 return &Logger{lg: lg} 104 } 105 106 func (l *Logger) Debug(args ...interface{}) { 107 l.lg.Debug(args) 108 } 109 110 func (l *Logger) Debugf(template string, args ...interface{}) { 111 l.lg.Debugf(template, args) 112 } 113 114 func (l *Logger) Info(args ...interface{}) { 115 l.lg.Info(args) 116 } 117 118 func (l *Logger) Infof(template string, args ...interface{}) { 119 l.lg.Infof(template, args) 120 } 121 122 func (l *Logger) Warn(args ...interface{}) { 123 l.lg.Warn(args) 124 } 125 126 func (l *Logger) Warnf(template string, args ...interface{}) { 127 l.lg.Warnf(template, args) 128 } 129 130 func (l *Logger) Error(args ...interface{}) { 131 l.lg.Error(args) 132 } 133 134 func (l *Logger) Errorf(template string, args ...interface{}) { 135 l.lg.Errorf(template, args) 136 } 137 138 func (l *Logger) Fatal(args ...interface{}) { 139 l.lg.Fatal(args) 140 } 141 142 func (l *Logger) Fatalf(fmt string, args ...interface{}) { 143 l.lg.Fatalf(fmt, args) 144 } 145 146 func encoderConfig() zapcore.EncoderConfig { 147 return zapcore.EncoderConfig{ 148 MessageKey: "msg", 149 LevelKey: "level", 150 TimeKey: "time", 151 CallerKey: "line", 152 NameKey: "logger", 153 StacktraceKey: "stacktrace", 154 EncodeLevel: zapcore.CapitalColorLevelEncoder, 155 EncodeTime: zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05"), 156 EncodeDuration: zapcore.SecondsDurationEncoder, 157 EncodeCaller: zapcore.ShortCallerEncoder, 158 } 159 }