grpclog.go (4982B)
1 /* 2 * 3 * Copyright 2020 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * 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 19 // Package grpclog (internal) defines depth logging for grpc. 20 package grpclog 21 22 import ( 23 "os" 24 ) 25 26 // Logger is the logger used for the non-depth log functions. 27 var Logger LoggerV2 28 29 // DepthLogger is the logger used for the depth log functions. 30 var DepthLogger DepthLoggerV2 31 32 // InfoDepth logs to the INFO log at the specified depth. 33 func InfoDepth(depth int, args ...interface{}) { 34 if DepthLogger != nil { 35 DepthLogger.InfoDepth(depth, args...) 36 } else { 37 Logger.Infoln(args...) 38 } 39 } 40 41 // WarningDepth logs to the WARNING log at the specified depth. 42 func WarningDepth(depth int, args ...interface{}) { 43 if DepthLogger != nil { 44 DepthLogger.WarningDepth(depth, args...) 45 } else { 46 Logger.Warningln(args...) 47 } 48 } 49 50 // ErrorDepth logs to the ERROR log at the specified depth. 51 func ErrorDepth(depth int, args ...interface{}) { 52 if DepthLogger != nil { 53 DepthLogger.ErrorDepth(depth, args...) 54 } else { 55 Logger.Errorln(args...) 56 } 57 } 58 59 // FatalDepth logs to the FATAL log at the specified depth. 60 func FatalDepth(depth int, args ...interface{}) { 61 if DepthLogger != nil { 62 DepthLogger.FatalDepth(depth, args...) 63 } else { 64 Logger.Fatalln(args...) 65 } 66 os.Exit(1) 67 } 68 69 // LoggerV2 does underlying logging work for grpclog. 70 // This is a copy of the LoggerV2 defined in the external grpclog package. It 71 // is defined here to avoid a circular dependency. 72 type LoggerV2 interface { 73 // Info logs to INFO log. Arguments are handled in the manner of fmt.Print. 74 Info(args ...interface{}) 75 // Infoln logs to INFO log. Arguments are handled in the manner of fmt.Println. 76 Infoln(args ...interface{}) 77 // Infof logs to INFO log. Arguments are handled in the manner of fmt.Printf. 78 Infof(format string, args ...interface{}) 79 // Warning logs to WARNING log. Arguments are handled in the manner of fmt.Print. 80 Warning(args ...interface{}) 81 // Warningln logs to WARNING log. Arguments are handled in the manner of fmt.Println. 82 Warningln(args ...interface{}) 83 // Warningf logs to WARNING log. Arguments are handled in the manner of fmt.Printf. 84 Warningf(format string, args ...interface{}) 85 // Error logs to ERROR log. Arguments are handled in the manner of fmt.Print. 86 Error(args ...interface{}) 87 // Errorln logs to ERROR log. Arguments are handled in the manner of fmt.Println. 88 Errorln(args ...interface{}) 89 // Errorf logs to ERROR log. Arguments are handled in the manner of fmt.Printf. 90 Errorf(format string, args ...interface{}) 91 // Fatal logs to ERROR log. Arguments are handled in the manner of fmt.Print. 92 // gRPC ensures that all Fatal logs will exit with os.Exit(1). 93 // Implementations may also call os.Exit() with a non-zero exit code. 94 Fatal(args ...interface{}) 95 // Fatalln logs to ERROR log. Arguments are handled in the manner of fmt.Println. 96 // gRPC ensures that all Fatal logs will exit with os.Exit(1). 97 // Implementations may also call os.Exit() with a non-zero exit code. 98 Fatalln(args ...interface{}) 99 // Fatalf logs to ERROR log. Arguments are handled in the manner of fmt.Printf. 100 // gRPC ensures that all Fatal logs will exit with os.Exit(1). 101 // Implementations may also call os.Exit() with a non-zero exit code. 102 Fatalf(format string, args ...interface{}) 103 // V reports whether verbosity level l is at least the requested verbose level. 104 V(l int) bool 105 } 106 107 // DepthLoggerV2 logs at a specified call frame. If a LoggerV2 also implements 108 // DepthLoggerV2, the below functions will be called with the appropriate stack 109 // depth set for trivial functions the logger may ignore. 110 // This is a copy of the DepthLoggerV2 defined in the external grpclog package. 111 // It is defined here to avoid a circular dependency. 112 // 113 // # Experimental 114 // 115 // Notice: This type is EXPERIMENTAL and may be changed or removed in a 116 // later release. 117 type DepthLoggerV2 interface { 118 // InfoDepth logs to INFO log at the specified depth. Arguments are handled in the manner of fmt.Println. 119 InfoDepth(depth int, args ...interface{}) 120 // WarningDepth logs to WARNING log at the specified depth. Arguments are handled in the manner of fmt.Println. 121 WarningDepth(depth int, args ...interface{}) 122 // ErrorDepth logs to ERROR log at the specified depth. Arguments are handled in the manner of fmt.Println. 123 ErrorDepth(depth int, args ...interface{}) 124 // FatalDepth logs to FATAL log at the specified depth. Arguments are handled in the manner of fmt.Println. 125 FatalDepth(depth int, args ...interface{}) 126 }