dubbo.apache.org/dubbo-go/v3@v3.1.1/logger/logrus/logrus.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 logrus 19 20 import ( 21 "io" 22 "os" 23 "strings" 24 ) 25 26 import ( 27 "github.com/dubbogo/gost/log/logger" 28 29 "github.com/mattn/go-colorable" 30 31 "github.com/sirupsen/logrus" 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("logrus", instantiate) 43 } 44 45 type Logger struct { 46 lg *logrus.Logger 47 } 48 49 func instantiate(config *common.URL) (log logger.Logger, err error) { 50 var ( 51 level string 52 writer []io.Writer 53 lv logrus.Level 54 appender []string 55 formatter logrus.Formatter 56 lg *logrus.Logger 57 ) 58 lg = logrus.New() 59 level = config.GetParam(constant.LoggerLevelKey, constant.LoggerLevel) 60 61 if lv, err = logrus.ParseLevel(level); err != nil { 62 lg.SetLevel(logrus.InfoLevel) 63 } else { 64 lg.SetLevel(lv) 65 } 66 67 appender = strings.Split(config.GetParam(constant.LoggerAppenderKey, constant.LoggerAppender), ",") 68 for _, apt := range appender { 69 switch apt { 70 case "console": 71 writer = append(writer, os.Stdout) 72 case "file": 73 file := FileConfig(config) 74 writer = append(writer, colorable.NewNonColorable(file)) 75 } 76 } 77 lg.SetOutput(io.MultiWriter(writer...)) 78 79 format := config.GetParam(constant.LoggerFormatKey, constant.LoggerFormat) 80 switch strings.ToLower(format) { 81 case "text": 82 formatter = &logrus.TextFormatter{} 83 case "json": 84 formatter = &logrus.JSONFormatter{} 85 default: 86 formatter = &logrus.TextFormatter{} 87 } 88 lg.SetFormatter(formatter) 89 return &Logger{lg: lg}, err 90 } 91 92 func (l *Logger) Debug(args ...interface{}) { 93 l.lg.Debug(args...) 94 } 95 96 func (l *Logger) Debugf(template string, args ...interface{}) { 97 l.lg.Debugf(template, args...) 98 } 99 100 func (l *Logger) Info(args ...interface{}) { 101 l.lg.Info(args...) 102 } 103 104 func (l *Logger) Infof(template string, args ...interface{}) { 105 l.lg.Infof(template, args...) 106 } 107 108 func (l *Logger) Warn(args ...interface{}) { 109 l.lg.Warn(args...) 110 } 111 112 func (l *Logger) Warnf(template string, args ...interface{}) { 113 l.lg.Warnf(template, args...) 114 } 115 116 func (l *Logger) Error(args ...interface{}) { 117 l.lg.Error(args...) 118 } 119 120 func (l *Logger) Errorf(template string, args ...interface{}) { 121 l.lg.Errorf(template, args...) 122 } 123 124 func (l *Logger) Fatal(args ...interface{}) { 125 l.lg.Fatal(args...) 126 } 127 128 func (l *Logger) Fatalf(fmt string, args ...interface{}) { 129 l.lg.Fatalf(fmt, args...) 130 }