blob: 892dc13d164b9ab60a6eea1d38934a789a19fadf [file] [log] [blame]
khenaidooac637102019-01-14 15:44:34 -05001/*
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
19package grpclog
20
21import (
22 "io"
khenaidooac637102019-01-14 15:44:34 -050023 "os"
24 "strconv"
Abhay Kumara2ae5992025-11-10 14:02:24 +000025 "strings"
26
27 "google.golang.org/grpc/grpclog/internal"
khenaidooac637102019-01-14 15:44:34 -050028)
29
30// LoggerV2 does underlying logging work for grpclog.
Abhay Kumara2ae5992025-11-10 14:02:24 +000031type LoggerV2 internal.LoggerV2
khenaidooac637102019-01-14 15:44:34 -050032
33// SetLoggerV2 sets logger that is used in grpc to a V2 logger.
34// Not mutex-protected, should be called before any gRPC functions.
35func SetLoggerV2(l LoggerV2) {
Abhay Kumara2ae5992025-11-10 14:02:24 +000036 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)
khenaidooac637102019-01-14 15:44:34 -050041}
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.
48func NewLoggerV2(infoW, warningW, errorW io.Writer) LoggerV2 {
Abhay Kumara2ae5992025-11-10 14:02:24 +000049 return internal.NewLoggerV2(infoW, warningW, errorW, internal.LoggerV2Config{})
khenaidooac637102019-01-14 15:44:34 -050050}
51
52// NewLoggerV2WithVerbosity creates a loggerV2 with the provided writers and
53// verbosity level.
54func NewLoggerV2WithVerbosity(infoW, warningW, errorW io.Writer, v int) LoggerV2 {
Abhay Kumara2ae5992025-11-10 14:02:24 +000055 return internal.NewLoggerV2(infoW, warningW, errorW, internal.LoggerV2Config{Verbosity: v})
khenaidooac637102019-01-14 15:44:34 -050056}
57
58// newLoggerV2 creates a loggerV2 to be used as default logger.
59// All logs are written to stderr.
60func newLoggerV2() LoggerV2 {
Abhay Kumara2ae5992025-11-10 14:02:24 +000061 errorW := io.Discard
62 warningW := io.Discard
63 infoW := io.Discard
khenaidooac637102019-01-14 15:44:34 -050064
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 Kumara2ae5992025-11-10 14:02:24 +000080
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 })
khenaidooac637102019-01-14 15:44:34 -050087}
88
Abhay Kumara2ae5992025-11-10 14:02:24 +000089// 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.
97type DepthLoggerV2 internal.DepthLoggerV2