khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [diff] [blame^] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2017 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 |
| 20 | |
| 21 | import ( |
| 22 | "io" |
| 23 | "io/ioutil" |
| 24 | "log" |
| 25 | "os" |
| 26 | "strconv" |
| 27 | |
| 28 | "google.golang.org/grpc/internal/grpclog" |
| 29 | ) |
| 30 | |
| 31 | // LoggerV2 does underlying logging work for grpclog. |
| 32 | type LoggerV2 interface { |
| 33 | // Info logs to INFO log. Arguments are handled in the manner of fmt.Print. |
| 34 | Info(args ...interface{}) |
| 35 | // Infoln logs to INFO log. Arguments are handled in the manner of fmt.Println. |
| 36 | Infoln(args ...interface{}) |
| 37 | // Infof logs to INFO log. Arguments are handled in the manner of fmt.Printf. |
| 38 | Infof(format string, args ...interface{}) |
| 39 | // Warning logs to WARNING log. Arguments are handled in the manner of fmt.Print. |
| 40 | Warning(args ...interface{}) |
| 41 | // Warningln logs to WARNING log. Arguments are handled in the manner of fmt.Println. |
| 42 | Warningln(args ...interface{}) |
| 43 | // Warningf logs to WARNING log. Arguments are handled in the manner of fmt.Printf. |
| 44 | Warningf(format string, args ...interface{}) |
| 45 | // Error logs to ERROR log. Arguments are handled in the manner of fmt.Print. |
| 46 | Error(args ...interface{}) |
| 47 | // Errorln logs to ERROR log. Arguments are handled in the manner of fmt.Println. |
| 48 | Errorln(args ...interface{}) |
| 49 | // Errorf logs to ERROR log. Arguments are handled in the manner of fmt.Printf. |
| 50 | Errorf(format string, args ...interface{}) |
| 51 | // Fatal logs to ERROR log. Arguments are handled in the manner of fmt.Print. |
| 52 | // gRPC ensures that all Fatal logs will exit with os.Exit(1). |
| 53 | // Implementations may also call os.Exit() with a non-zero exit code. |
| 54 | Fatal(args ...interface{}) |
| 55 | // Fatalln logs to ERROR log. Arguments are handled in the manner of fmt.Println. |
| 56 | // gRPC ensures that all Fatal logs will exit with os.Exit(1). |
| 57 | // Implementations may also call os.Exit() with a non-zero exit code. |
| 58 | Fatalln(args ...interface{}) |
| 59 | // Fatalf logs to ERROR log. Arguments are handled in the manner of fmt.Printf. |
| 60 | // gRPC ensures that all Fatal logs will exit with os.Exit(1). |
| 61 | // Implementations may also call os.Exit() with a non-zero exit code. |
| 62 | Fatalf(format string, args ...interface{}) |
| 63 | // V reports whether verbosity level l is at least the requested verbose level. |
| 64 | V(l int) bool |
| 65 | } |
| 66 | |
| 67 | // SetLoggerV2 sets logger that is used in grpc to a V2 logger. |
| 68 | // Not mutex-protected, should be called before any gRPC functions. |
| 69 | func SetLoggerV2(l LoggerV2) { |
| 70 | if _, ok := l.(*componentData); ok { |
| 71 | panic("cannot use component logger as grpclog logger") |
| 72 | } |
| 73 | grpclog.Logger = l |
| 74 | grpclog.DepthLogger, _ = l.(grpclog.DepthLoggerV2) |
| 75 | } |
| 76 | |
| 77 | const ( |
| 78 | // infoLog indicates Info severity. |
| 79 | infoLog int = iota |
| 80 | // warningLog indicates Warning severity. |
| 81 | warningLog |
| 82 | // errorLog indicates Error severity. |
| 83 | errorLog |
| 84 | // fatalLog indicates Fatal severity. |
| 85 | fatalLog |
| 86 | ) |
| 87 | |
| 88 | // severityName contains the string representation of each severity. |
| 89 | var severityName = []string{ |
| 90 | infoLog: "INFO", |
| 91 | warningLog: "WARNING", |
| 92 | errorLog: "ERROR", |
| 93 | fatalLog: "FATAL", |
| 94 | } |
| 95 | |
| 96 | // loggerT is the default logger used by grpclog. |
| 97 | type loggerT struct { |
| 98 | m []*log.Logger |
| 99 | v int |
| 100 | } |
| 101 | |
| 102 | // NewLoggerV2 creates a loggerV2 with the provided writers. |
| 103 | // Fatal logs will be written to errorW, warningW, infoW, followed by exit(1). |
| 104 | // Error logs will be written to errorW, warningW and infoW. |
| 105 | // Warning logs will be written to warningW and infoW. |
| 106 | // Info logs will be written to infoW. |
| 107 | func NewLoggerV2(infoW, warningW, errorW io.Writer) LoggerV2 { |
| 108 | return NewLoggerV2WithVerbosity(infoW, warningW, errorW, 0) |
| 109 | } |
| 110 | |
| 111 | // NewLoggerV2WithVerbosity creates a loggerV2 with the provided writers and |
| 112 | // verbosity level. |
| 113 | func NewLoggerV2WithVerbosity(infoW, warningW, errorW io.Writer, v int) LoggerV2 { |
| 114 | var m []*log.Logger |
| 115 | m = append(m, log.New(infoW, severityName[infoLog]+": ", log.LstdFlags)) |
| 116 | m = append(m, log.New(io.MultiWriter(infoW, warningW), severityName[warningLog]+": ", log.LstdFlags)) |
| 117 | ew := io.MultiWriter(infoW, warningW, errorW) // ew will be used for error and fatal. |
| 118 | m = append(m, log.New(ew, severityName[errorLog]+": ", log.LstdFlags)) |
| 119 | m = append(m, log.New(ew, severityName[fatalLog]+": ", log.LstdFlags)) |
| 120 | return &loggerT{m: m, v: v} |
| 121 | } |
| 122 | |
| 123 | // newLoggerV2 creates a loggerV2 to be used as default logger. |
| 124 | // All logs are written to stderr. |
| 125 | func newLoggerV2() LoggerV2 { |
| 126 | errorW := ioutil.Discard |
| 127 | warningW := ioutil.Discard |
| 128 | infoW := ioutil.Discard |
| 129 | |
| 130 | logLevel := os.Getenv("GRPC_GO_LOG_SEVERITY_LEVEL") |
| 131 | switch logLevel { |
| 132 | case "", "ERROR", "error": // If env is unset, set level to ERROR. |
| 133 | errorW = os.Stderr |
| 134 | case "WARNING", "warning": |
| 135 | warningW = os.Stderr |
| 136 | case "INFO", "info": |
| 137 | infoW = os.Stderr |
| 138 | } |
| 139 | |
| 140 | var v int |
| 141 | vLevel := os.Getenv("GRPC_GO_LOG_VERBOSITY_LEVEL") |
| 142 | if vl, err := strconv.Atoi(vLevel); err == nil { |
| 143 | v = vl |
| 144 | } |
| 145 | return NewLoggerV2WithVerbosity(infoW, warningW, errorW, v) |
| 146 | } |
| 147 | |
| 148 | func (g *loggerT) Info(args ...interface{}) { |
| 149 | g.m[infoLog].Print(args...) |
| 150 | } |
| 151 | |
| 152 | func (g *loggerT) Infoln(args ...interface{}) { |
| 153 | g.m[infoLog].Println(args...) |
| 154 | } |
| 155 | |
| 156 | func (g *loggerT) Infof(format string, args ...interface{}) { |
| 157 | g.m[infoLog].Printf(format, args...) |
| 158 | } |
| 159 | |
| 160 | func (g *loggerT) Warning(args ...interface{}) { |
| 161 | g.m[warningLog].Print(args...) |
| 162 | } |
| 163 | |
| 164 | func (g *loggerT) Warningln(args ...interface{}) { |
| 165 | g.m[warningLog].Println(args...) |
| 166 | } |
| 167 | |
| 168 | func (g *loggerT) Warningf(format string, args ...interface{}) { |
| 169 | g.m[warningLog].Printf(format, args...) |
| 170 | } |
| 171 | |
| 172 | func (g *loggerT) Error(args ...interface{}) { |
| 173 | g.m[errorLog].Print(args...) |
| 174 | } |
| 175 | |
| 176 | func (g *loggerT) Errorln(args ...interface{}) { |
| 177 | g.m[errorLog].Println(args...) |
| 178 | } |
| 179 | |
| 180 | func (g *loggerT) Errorf(format string, args ...interface{}) { |
| 181 | g.m[errorLog].Printf(format, args...) |
| 182 | } |
| 183 | |
| 184 | func (g *loggerT) Fatal(args ...interface{}) { |
| 185 | g.m[fatalLog].Fatal(args...) |
| 186 | // No need to call os.Exit() again because log.Logger.Fatal() calls os.Exit(). |
| 187 | } |
| 188 | |
| 189 | func (g *loggerT) Fatalln(args ...interface{}) { |
| 190 | g.m[fatalLog].Fatalln(args...) |
| 191 | // No need to call os.Exit() again because log.Logger.Fatal() calls os.Exit(). |
| 192 | } |
| 193 | |
| 194 | func (g *loggerT) Fatalf(format string, args ...interface{}) { |
| 195 | g.m[fatalLog].Fatalf(format, args...) |
| 196 | // No need to call os.Exit() again because log.Logger.Fatal() calls os.Exit(). |
| 197 | } |
| 198 | |
| 199 | func (g *loggerT) V(l int) bool { |
| 200 | return l <= g.v |
| 201 | } |
| 202 | |
| 203 | // DepthLoggerV2 logs at a specified call frame. If a LoggerV2 also implements |
| 204 | // DepthLoggerV2, the below functions will be called with the appropriate stack |
| 205 | // depth set for trivial functions the logger may ignore. |
| 206 | // |
| 207 | // Experimental |
| 208 | // |
| 209 | // Notice: This type is EXPERIMENTAL and may be changed or removed in a |
| 210 | // later release. |
| 211 | type DepthLoggerV2 interface { |
| 212 | LoggerV2 |
| 213 | // InfoDepth logs to INFO log at the specified depth. Arguments are handled in the manner of fmt.Print. |
| 214 | InfoDepth(depth int, args ...interface{}) |
| 215 | // WarningDepth logs to WARNING log at the specified depth. Arguments are handled in the manner of fmt.Print. |
| 216 | WarningDepth(depth int, args ...interface{}) |
| 217 | // ErrorDetph logs to ERROR log at the specified depth. Arguments are handled in the manner of fmt.Print. |
| 218 | ErrorDepth(depth int, args ...interface{}) |
| 219 | // FatalDepth logs to FATAL log at the specified depth. Arguments are handled in the manner of fmt.Print. |
| 220 | FatalDepth(depth int, args ...interface{}) |
| 221 | } |