github.com/cloudwego/kitex@v0.9.0/tool/internal_pkg/log/log.go (about) 1 // Copyright 2021 CloudWeGo Authors 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 log 16 17 import ( 18 "fmt" 19 "io" 20 "os" 21 ) 22 23 // Verbose decides whether the informatc logs should be output. 24 var Verbose bool 25 26 // Logger . 27 type Logger struct { 28 Println func(w io.Writer, a ...interface{}) (n int, err error) 29 Printf func(w io.Writer, format string, a ...interface{}) (n int, err error) 30 } 31 32 var defaultLogger = Logger{ 33 Println: fmt.Fprintln, 34 Printf: fmt.Fprintf, 35 } 36 37 // SetDefaultLogger sets the default logger. 38 func SetDefaultLogger(l Logger) { 39 defaultLogger = l 40 } 41 42 // Warn . 43 func Warn(v ...interface{}) { 44 defaultLogger.Println(os.Stderr, v...) 45 } 46 47 // Warnf . 48 func Warnf(format string, v ...interface{}) { 49 defaultLogger.Printf(os.Stderr, format, v...) 50 } 51 52 // Info . 53 func Info(v ...interface{}) { 54 if Verbose { 55 defaultLogger.Println(os.Stderr, v...) 56 } 57 } 58 59 // Infof . 60 func Infof(format string, v ...interface{}) { 61 if Verbose { 62 defaultLogger.Printf(os.Stderr, format, v...) 63 } 64 }