| khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [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" |
| khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 23 | "os" |
| 24 | "strconv" |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 25 | "strings" |
| 26 | |
| 27 | "google.golang.org/grpc/grpclog/internal" |
| khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | // LoggerV2 does underlying logging work for grpclog. |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 31 | type LoggerV2 internal.LoggerV2 |
| khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 32 | |
| 33 | // SetLoggerV2 sets logger that is used in grpc to a V2 logger. |
| 34 | // Not mutex-protected, should be called before any gRPC functions. |
| 35 | func SetLoggerV2(l LoggerV2) { |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 36 | if _, ok := l.(*componentData); ok { |
| 37 | panic("cannot use component logger as grpclog logger") |
| 38 | } |
| 39 | internal.LoggerV2Impl = l |
| 40 | internal.DepthLoggerV2Impl, _ = l.(internal.DepthLoggerV2) |
| khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | // NewLoggerV2 creates a loggerV2 with the provided writers. |
| 44 | // Fatal logs will be written to errorW, warningW, infoW, followed by exit(1). |
| 45 | // Error logs will be written to errorW, warningW and infoW. |
| 46 | // Warning logs will be written to warningW and infoW. |
| 47 | // Info logs will be written to infoW. |
| 48 | func NewLoggerV2(infoW, warningW, errorW io.Writer) LoggerV2 { |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 49 | return internal.NewLoggerV2(infoW, warningW, errorW, internal.LoggerV2Config{}) |
| khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | // NewLoggerV2WithVerbosity creates a loggerV2 with the provided writers and |
| 53 | // verbosity level. |
| 54 | func NewLoggerV2WithVerbosity(infoW, warningW, errorW io.Writer, v int) LoggerV2 { |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 55 | return internal.NewLoggerV2(infoW, warningW, errorW, internal.LoggerV2Config{Verbosity: v}) |
| khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | // newLoggerV2 creates a loggerV2 to be used as default logger. |
| 59 | // All logs are written to stderr. |
| 60 | func newLoggerV2() LoggerV2 { |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 61 | errorW := io.Discard |
| 62 | warningW := io.Discard |
| 63 | infoW := io.Discard |
| khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 64 | |
| 65 | logLevel := os.Getenv("GRPC_GO_LOG_SEVERITY_LEVEL") |
| 66 | switch logLevel { |
| 67 | case "", "ERROR", "error": // If env is unset, set level to ERROR. |
| 68 | errorW = os.Stderr |
| 69 | case "WARNING", "warning": |
| 70 | warningW = os.Stderr |
| 71 | case "INFO", "info": |
| 72 | infoW = os.Stderr |
| 73 | } |
| 74 | |
| 75 | var v int |
| 76 | vLevel := os.Getenv("GRPC_GO_LOG_VERBOSITY_LEVEL") |
| 77 | if vl, err := strconv.Atoi(vLevel); err == nil { |
| 78 | v = vl |
| 79 | } |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 80 | |
| 81 | jsonFormat := strings.EqualFold(os.Getenv("GRPC_GO_LOG_FORMATTER"), "json") |
| 82 | |
| 83 | return internal.NewLoggerV2(infoW, warningW, errorW, internal.LoggerV2Config{ |
| 84 | Verbosity: v, |
| 85 | FormatJSON: jsonFormat, |
| 86 | }) |
| khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 87 | } |
| 88 | |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 89 | // DepthLoggerV2 logs at a specified call frame. If a LoggerV2 also implements |
| 90 | // DepthLoggerV2, the below functions will be called with the appropriate stack |
| 91 | // depth set for trivial functions the logger may ignore. |
| 92 | // |
| 93 | // # Experimental |
| 94 | // |
| 95 | // Notice: This type is EXPERIMENTAL and may be changed or removed in a |
| 96 | // later release. |
| 97 | type DepthLoggerV2 internal.DepthLoggerV2 |